-
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 13 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 |
|---|---|---|
|
|
@@ -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')) { | ||
|
||
| 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; | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.