-
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
Changes from 16 commits
071f1d5
d2069f0
50b804a
164f7b2
fd45002
44c074e
5ec1943
a366555
33dbb85
cbb7daf
5f94ec2
ed3cbce
4385242
375b405
19bc558
04fd685
f2ca623
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 |
---|---|---|
|
@@ -27,6 +27,8 @@ class GradleCheckCommand extends PackageLoopingCommand { | |
super.gitDir, | ||
}); | ||
|
||
static const int _minimumJavaVersion = 17; | ||
|
||
@override | ||
final String name = 'gradle-check'; | ||
|
||
|
@@ -124,12 +126,18 @@ class GradleCheckCommand extends PackageLoopingCommand { | |
// This is tracked as a variable rather than a sequence of &&s so that all | ||
// failures are reported at once, not just the first one. | ||
bool succeeded = true; | ||
if (!_validateJavaKotlinCompileOptionsAlignment(lines)) { | ||
succeeded = false; | ||
} | ||
if (!_validateNamespace(package, contents, isExample: false)) { | ||
succeeded = false; | ||
} | ||
if (!_validateCompatibilityVersions(lines)) { | ||
succeeded = false; | ||
} | ||
if (!_validateKotlinJvmCompatibility(lines)) { | ||
succeeded = false; | ||
} | ||
if (!_validateGradleDrivenLintConfig(package, lines)) { | ||
succeeded = false; | ||
} | ||
|
@@ -356,21 +364,18 @@ 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'; | ||
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 +384,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) | ||
} | ||
} | ||
|
||
|
@@ -399,11 +404,16 @@ for more details.'''; | |
'$indentation${javaErrorMessage.split('\n').join('\n$indentation')}'); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
bool _validateKotlinJvmCompatibility(List<String> gradleLines) { | ||
bool isKotlinOptions(String line) => | ||
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 +431,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: | ||
|
@@ -431,6 +441,45 @@ If build.gradle(.kts) sets jvmTarget then it must use JavaVersion syntax. | |
'$indentation${kotlinErrorMessage.split('\n').join('\n$indentation')}'); | ||
return false; | ||
} | ||
// No error condition. | ||
return true; | ||
} | ||
|
||
bool _validateJavaKotlinCompileOptionsAlignment(List<String> gradleLines) { | ||
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')) { | ||
|
||
const String javaVersionAlignmentError = ''' | ||
If build.gradle(.kts) uses JavaVersion.* versions must be the same. | ||
'''; | ||
printError( | ||
'$indentation${javaVersionAlignmentError.split('\n').join('\n$indentation')}'); | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.