-
Notifications
You must be signed in to change notification settings - Fork 3.5k
[tool] Change gradle-check logic to enforce alignment of java versions and a minimum (17) #10206
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
071f1d5
d2069f0
50b804a
164f7b2
fd45002
44c074e
5ec1943
a366555
33dbb85
cbb7daf
5f94ec2
ed3cbce
4385242
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -356,21 +356,19 @@ build.gradle "namespace" must match the "package" attribute in AndroidManifest.x | |
/// than using whatever the client's local toolchaing defaults to (which can | ||
/// lead to compile errors that show up for clients, but not in CI). | ||
bool _validateCompatibilityVersions(List<String> gradleLines) { | ||
const String requiredJavaVersion = '17'; | ||
const int minimumJavaVersion = 17; | ||
final bool hasLanguageVersion = gradleLines.any((String line) => | ||
line.contains('languageVersion') && !_isCommented(line)); | ||
final bool hasCompabilityVersions = gradleLines.any((String line) => | ||
line.contains( | ||
'sourceCompatibility = JavaVersion.VERSION_$requiredJavaVersion') && | ||
line.contains('sourceCompatibility = JavaVersion.VERSION_') && | ||
!_isCommented(line)) && | ||
// Newer toolchains default targetCompatibility to the same value as | ||
// sourceCompatibility, but older toolchains require it to be set | ||
// explicitly. The exact version cutoff (and of which piece of the | ||
// toolchain; likely AGP) is unknown; for context see | ||
// https://github.com/flutter/flutter/issues/125482 | ||
gradleLines.any((String line) => | ||
line.contains( | ||
'targetCompatibility = JavaVersion.VERSION_$requiredJavaVersion') && | ||
line.contains('targetCompatibility = JavaVersion.VERSION_') && | ||
!_isCommented(line)); | ||
if (!hasLanguageVersion && !hasCompabilityVersions) { | ||
const String javaErrorMessage = ''' | ||
|
@@ -379,15 +377,15 @@ build.gradle(.kts) must set an explicit Java compatibility version. | |
This can be done either via "sourceCompatibility"/"targetCompatibility": | ||
android { | ||
compileOptions { | ||
sourceCompatibility = JavaVersion.VERSION_$requiredJavaVersion | ||
targetCompatibility = JavaVersion.VERSION_$requiredJavaVersion | ||
sourceCompatibility = JavaVersion.VERSION_$minimumJavaVersion | ||
targetCompatibility = JavaVersion.VERSION_$minimumJavaVersion | ||
} | ||
} | ||
|
||
or "toolchain": | ||
java { | ||
toolchain { | ||
languageVersion = JavaLanguageVersion.of($requiredJavaVersion) | ||
languageVersion = JavaLanguageVersion.of($minimumJavaVersion) | ||
} | ||
} | ||
|
||
|
@@ -403,7 +401,7 @@ for more details.'''; | |
line.contains('kotlinOptions') && !_isCommented(line); | ||
final bool hasKotlinOptions = gradleLines.any(isKotlinOptions); | ||
final bool kotlinOptionsUsesJavaVersion = gradleLines.any((String line) => | ||
line.contains('jvmTarget = JavaVersion.VERSION_$requiredJavaVersion') && | ||
line.contains('jvmTarget = JavaVersion.VERSION_') && | ||
!_isCommented(line)); | ||
// Either does not set kotlinOptions or does and uses non-string based syntax. | ||
if (hasKotlinOptions && !kotlinOptionsUsesJavaVersion) { | ||
|
@@ -421,7 +419,7 @@ If build.gradle(.kts) sets jvmTarget then it must use JavaVersion syntax. | |
Good: | ||
android { | ||
kotlinOptions { | ||
jvmTarget = JavaVersion.VERSION_$requiredJavaVersion.toString() | ||
jvmTarget = JavaVersion.VERSION_$minimumJavaVersion.toString() | ||
} | ||
} | ||
BAD: | ||
|
@@ -432,6 +430,41 @@ If build.gradle(.kts) sets jvmTarget then it must use JavaVersion syntax. | |
return false; | ||
} | ||
|
||
final List<String> javaVersions = <String>[]; | ||
// Some java versions have the format VERSION_1_8 but we dont need to handle those | ||
// because they are below the minimum. | ||
final RegExp javaVersionMatcher = | ||
RegExp(r'JavaVersion.VERSION_(?<javaVersion>\d+)'); | ||
for (final String line in gradleLines) { | ||
final RegExpMatch? match = javaVersionMatcher.firstMatch(line); | ||
if (!_isCommented(line) && match != null) { | ||
final String? foundVersion = match.namedGroup('javaVersion'); | ||
if (foundVersion != null) { | ||
javaVersions.add(foundVersion); | ||
} | ||
} | ||
} | ||
if (javaVersions.isNotEmpty) { | ||
final int version = int.parse(javaVersions.first); | ||
if (version < minimumJavaVersion) { | ||
final String minimumJavaVersionError = ''' | ||
build.gradle(.kts) uses "JavaVersion.VERSION_$version". | ||
Which is below the minimum required. Use at least "JavaVersion.VERSION_$minimumJavaVersion". | ||
'''; | ||
printError( | ||
'$indentation${minimumJavaVersionError.split('\n').join('\n$indentation')}'); | ||
return false; | ||
} | ||
if (!javaVersions.every((String element) => element == '$version')) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would strongly prefer making this the first check instead of the second, since if you don't already know there's going to be a check that they all match, the check above that only the first element is the right version seems wrong. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I can make this change :) standby |
||
const String javaVersionAlignmentError = ''' | ||
If build.gradle(.kts) uses JavaVersion.* versions must be the same. | ||
'''; | ||
printError( | ||
'$indentation${javaVersionAlignmentError.split('\n').join('\n$indentation')}'); | ||
return false; | ||
} | ||
} | ||
reidbaker marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return true; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than tagging this on at the end, you could restructure the earlier somewhat to avoid having multiple passes over the same file looking for the same lines different ways, which I feel like makes the code harder to follow. You could start this whole method with something like (this is freehand so is probably wrong, but should give the idea):
Then all of the
gradleLines.any((String line) => line.contains(...)
checks above are just key checks in that map, and you can use the same map below for the version checks.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Organizationally what if I pulled each check into its own method then had
That might help with readability and make it clear when formatting cares about the values vs the structure?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you prefer the current code structure, breaking out helpers so it's not one big method SGTM.