From 87392c214b4e8860b5cf3bc7903d947c40915803 Mon Sep 17 00:00:00 2001 From: Camille Simon <43054281+camsim99@users.noreply.github.com> Date: Wed, 5 Apr 2023 14:39:55 -0700 Subject: [PATCH] [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. [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 Co-authored-by: Christopher Fujino Co-authored-by: Christopher Fujino --- .../lib/src/android/gradle_errors.dart | 40 +++++++++++++++++++ .../android/gradle_errors_test.dart | 35 ++++++++++++++++ 2 files changed, 75 insertions(+) diff --git a/packages/flutter_tools/lib/src/android/gradle_errors.dart b/packages/flutter_tools/lib/src/android/gradle_errors.dart index e07657d794..aacc83ff61 100644 --- a/packages/flutter_tools/lib/src/android/gradle_errors.dart +++ b/packages/flutter_tools/lib/src/android/gradle_errors.dart @@ -82,6 +82,7 @@ final List gradleErrors = [ 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', +); diff --git a/packages/flutter_tools/test/general.shard/android/gradle_errors_test.dart b/packages/flutter_tools/test/general.shard/android/gradle_errors_test.dart index 2b5f89de4b..2affa2f70c 100644 --- a/packages/flutter_tools/test/general.shard/android/gradle_errors_test.dart +++ b/packages/flutter_tools/test/general.shard/android/gradle_errors_test.dart @@ -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: { + GradleUtils: () => FakeGradleUtils(), + Platform: () => fakePlatform('android'), + FileSystem: () => fileSystem, + ProcessManager: () => processManager, + }); + }); } bool formatTestErrorMessage(String errorMessage, GradleHandledError error) {