[Android] Catch and rethrow Java/Gradle incompatibility error (#124084)
Catches and re-throws Gradle error indicating that the Gradle version is incompatible with the Java version that Flutter is using to tell users how to fix the issue. Related issue: https://github.com/flutter/flutter/issues/122376 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [ ] All existing and new tests are passing. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/wiki/Tree-hygiene#overview [Tree Hygiene]: https://github.com/flutter/flutter/wiki/Tree-hygiene [test-exempt]: https://github.com/flutter/flutter/wiki/Tree-hygiene#tests [Flutter Style Guide]: https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo [Features we expect every widget to implement]: https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/wiki/Tree-hygiene#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/wiki/Chat --------- Co-authored-by: Reid Baker <hamilton.reid.baker@gmail.com> Co-authored-by: Christopher Fujino <fujino@google.com> Co-authored-by: Christopher Fujino <christopherfujino@gmail.com>
This commit is contained in:
@@ -82,6 +82,7 @@ final List<GradleHandledError> gradleErrors = <GradleHandledError>[
|
||||
outdatedGradleHandler,
|
||||
sslExceptionHandler,
|
||||
zipExceptionHandler,
|
||||
incompatibleJavaAndGradleVersionsHandler,
|
||||
];
|
||||
|
||||
const String _boxTitle = 'Flutter Fix';
|
||||
@@ -669,3 +670,42 @@ final GradleHandledError sslExceptionHandler = GradleHandledError(
|
||||
},
|
||||
eventLabel: 'ssl-exception-tag-mismatch',
|
||||
);
|
||||
|
||||
/// If an incompatible Java and Gradle versions error is caught, we expect an
|
||||
/// error specifying that the Java major class file version, one of
|
||||
/// https://javaalmanac.io/bytecode/versions/, is unsupported by Gradle.
|
||||
final RegExp _unsupportedClassFileMajorVersionPattern = RegExp(r'Unsupported class file major version\s+\d+');
|
||||
|
||||
@visibleForTesting
|
||||
final GradleHandledError incompatibleJavaAndGradleVersionsHandler = GradleHandledError(
|
||||
test: (String line) {
|
||||
return _unsupportedClassFileMajorVersionPattern.hasMatch(line);
|
||||
},
|
||||
handler: ({
|
||||
required String line,
|
||||
required FlutterProject project,
|
||||
required bool usesAndroidX,
|
||||
required bool multidexEnabled,
|
||||
}) async {
|
||||
final File gradlePropertiesFile = project.directory
|
||||
.childDirectory('android')
|
||||
.childDirectory('gradle')
|
||||
.childDirectory('wrapper')
|
||||
.childFile('gradle-wrapper.properties');
|
||||
// TODO(reidbaker): Replace URL with constant defined in
|
||||
// https://github.com/flutter/flutter/pull/123916.
|
||||
globals.printBox(
|
||||
"${globals.logger.terminal.warningMark} Your project's Gradle version "
|
||||
'is incompatible with the Java version that Flutter is using for Gradle.\n\n'
|
||||
'To fix this issue, first, check the Java version used by Flutter by '
|
||||
'running `flutter doctor --verbose`.\n\n'
|
||||
'Then, update the Gradle version specified in ${gradlePropertiesFile.path} '
|
||||
'to be compatible with that Java version. '
|
||||
'See the link below for more information on compatible Java/Gradle versions:\n'
|
||||
'https://docs.gradle.org/current/userguide/compatibility.html#java\n\n',
|
||||
title: _boxTitle,
|
||||
);
|
||||
return GradleBuildStatus.exit;
|
||||
},
|
||||
eventLabel: 'incompatible-java-gradle-version',
|
||||
);
|
||||
|
||||
@@ -48,6 +48,7 @@ void main() {
|
||||
outdatedGradleHandler,
|
||||
sslExceptionHandler,
|
||||
zipExceptionHandler,
|
||||
incompatibleJavaAndGradleVersionsHandler,
|
||||
])
|
||||
);
|
||||
});
|
||||
@@ -1303,6 +1304,40 @@ at org.gradle.wrapper.GradleWrapperMain.main(GradleWrapperMain.java:61)'''
|
||||
BotDetector: () => const FakeBotDetector(false),
|
||||
});
|
||||
});
|
||||
|
||||
group('incompatible java and gradle versions error', () {
|
||||
const String errorMessage = '''
|
||||
Could not compile build file '…/example/android/build.gradle'.
|
||||
> startup failed:
|
||||
General error during conversion: Unsupported class file major version 61
|
||||
java.lang.IllegalArgumentException: Unsupported class file major version 61
|
||||
''';
|
||||
|
||||
testWithoutContext('pattern', () {
|
||||
expect(
|
||||
incompatibleJavaAndGradleVersionsHandler.test(errorMessage),
|
||||
isTrue,
|
||||
);
|
||||
});
|
||||
|
||||
testUsingContext('suggestion', () async {
|
||||
await incompatibleJavaAndGradleVersionsHandler.handler(
|
||||
line: errorMessage,
|
||||
project: FlutterProject.fromDirectoryTest(fileSystem.currentDirectory),
|
||||
usesAndroidX: true,
|
||||
multidexEnabled: true,
|
||||
);
|
||||
|
||||
// Ensure the error notes the incompatible Gradle/AGP/Java versions and links to related resources.
|
||||
expect(testLogger.statusText, contains('Gradle version is incompatible with the Java version'));
|
||||
expect(testLogger.statusText, contains('https://docs.gradle.org/current/userguide/compatibility.html#java'));
|
||||
}, overrides: <Type, Generator>{
|
||||
GradleUtils: () => FakeGradleUtils(),
|
||||
Platform: () => fakePlatform('android'),
|
||||
FileSystem: () => fileSystem,
|
||||
ProcessManager: () => processManager,
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
bool formatTestErrorMessage(String errorMessage, GradleHandledError error) {
|
||||
|
||||
Reference in New Issue
Block a user