-
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
Open
reidbaker
wants to merge
13
commits into
flutter:main
Choose a base branch
from
reidbaker:i176027_attempt_5_java_version_range_tooling_support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+131
−17
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
071f1d5
Update tooling to enforce java 17 compile options and kotlinOptions
reidbaker d2069f0
Update interactive_media_ads and camera_android
reidbaker 50b804a
Merge branch 'main' into i176027_attempt_4_non_examples
reidbaker 164f7b2
Update compileOptions to java 17
reidbaker fd45002
Update changelog and pubspec for modified packages
reidbaker 44c074e
Fix pigeon example
reidbaker 5ec1943
Update changelog and pubspec for versions that did not need flutter o…
reidbaker a366555
Format packages that were updated with new dart version
reidbaker 33dbb85
Format toolig
reidbaker cbb7daf
bump interactive_media_ads min flutter version
reidbaker 5f94ec2
Update gradle-check to support minimum java version and validating ja…
reidbaker ed3cbce
Merge branch 'main' into i176027_attempt_5_java_version_range_tooling…
reidbaker 4385242
Update CHANGELOG.md
reidbaker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
||
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; | ||
} | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.