Skip to content

Commit 05c454c

Browse files
authored
Agp Java Compatability Custom Error (#154142)
Catches gradle error and throws a helpful error message that indicates an incompatability between Java and AGP versions and how to fix the issue. Related issue: [128524](flutter/flutter#128524) ## 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]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [ ] All existing and new tests are passing.
1 parent f9351fa commit 05c454c

File tree

3 files changed

+49
-35
lines changed

3 files changed

+49
-35
lines changed

packages/flutter_tools/lib/src/android/gradle_errors.dart

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ final List<GradleHandledError> gradleErrors = <GradleHandledError>[
7272
transformInputIssueHandler,
7373
lockFileDepMissingHandler,
7474
minCompileSdkVersionHandler,
75-
jvm11RequiredHandler,
75+
incompatibleJavaAndAgpVersionsHandler,
7676
outdatedGradleHandler,
7777
sslExceptionHandler,
7878
zipExceptionHandler,
@@ -503,25 +503,38 @@ final GradleHandledError minCompileSdkVersionHandler = GradleHandledError(
503503
eventLabel: 'min-compile-sdk-version',
504504
);
505505

506+
final RegExp _agpJavaError = RegExp(r'Android Gradle plugin requires Java (\d+\.?\d*) to run');
507+
508+
// If an incompatible Java and Android Gradle Plugin error is caught,
509+
// Android Gradle Plugin throws the required Java version to fix the error.
510+
// Android Gradle Plugin handles the error here: http://shortn/_SgUWyRdywL.
511+
512+
// If we ever need to reference or check the thrown requirements,
513+
// we can find the Java and Android Gradle Plugin compatability here:
514+
// 'https://developer.android.com/build/releases/past-releases'
506515
@visibleForTesting
507-
final GradleHandledError jvm11RequiredHandler = GradleHandledError(
516+
final GradleHandledError incompatibleJavaAndAgpVersionsHandler= GradleHandledError(
508517
test: (String line) {
509-
return line.contains('Android Gradle plugin requires Java 11 to run');
518+
return _agpJavaError.hasMatch(line);
510519
},
511520
handler: ({
512521
required String line,
513522
required FlutterProject project,
514523
required bool usesAndroidX,
515524
}) async {
525+
final String helpfulGradleError = line.trim().substring(2);
526+
516527
globals.printBox(
517-
'${globals.logger.terminal.warningMark} You need Java 11 or higher to build your app with this version of Gradle.\n\n'
518-
'To get Java 11, update to the latest version of Android Studio on https://developer.android.com/studio/install.\n\n'
519-
'To check the Java version used by Flutter, run `flutter doctor -v`.',
528+
'${globals.logger.terminal.warningMark} $helpfulGradleError\n\n'
529+
'To fix this issue, try updating to the latest Android SDK and Android Studio on: ${AndroidProject.installAndroidStudioUrl}\n'
530+
'If that does not work, you can set the Java version used by Flutter by \n'
531+
'running `flutter config --jdk-dir=“</path/to/jdk>“`\n\n'
532+
'To check the Java version used by Flutter, run `flutter doctor --verbose`',
520533
title: _boxTitle,
521534
);
522535
return GradleBuildStatus.exit;
523536
},
524-
eventLabel: 'java11-required',
537+
eventLabel: 'incompatible-java-agp-version',
525538
);
526539

527540
/// Handles SSL exceptions: https://github.com/flutter/flutter/issues/104628

packages/flutter_tools/lib/src/project.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,11 @@ class AndroidProject extends FlutterProjectPlatform {
453453
static const String javaGradleCompatUrl =
454454
'https://docs.gradle.org/current/userguide/compatibility.html#java';
455455

456+
// User facing link that describes instructions for downloading
457+
// the latest version of Android Studio.
458+
static const String installAndroidStudioUrl =
459+
'https://developer.android.com/studio/install';
460+
456461
/// The parent of this project.
457462
final FlutterProject parent;
458463

packages/flutter_tools/test/general.shard/android/gradle_errors_test.dart

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ void main() {
4343
transformInputIssueHandler,
4444
lockFileDepMissingHandler,
4545
minCompileSdkVersionHandler,
46-
jvm11RequiredHandler,
46+
incompatibleJavaAndAgpVersionsHandler,
4747
outdatedGradleHandler,
4848
sslExceptionHandler,
4949
zipExceptionHandler,
@@ -983,44 +983,40 @@ Execution failed for task ':app:checkDebugAarMetadata'.
983983
});
984984
});
985985

986-
group('Java 11 requirement', () {
987-
testWithoutContext('pattern', () {
988-
expect(
989-
jvm11RequiredHandler.test('''
986+
group('incompatible java and android gradle plugin versions error', () {
987+
988+
const String errorMessage = '''
990989
* What went wrong:
991-
A problem occurred evaluating project ':flutter'.
992-
> Failed to apply plugin 'com.android.internal.library'.
993-
> Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8.
994-
You can try some of the following options:
990+
An exception occurred applying plugin request [id: 'com.android.application']
991+
> Failed to apply plugin 'com.android.internal.application'.
992+
> Android Gradle plugin requires Java 17 to run. You are currently using Java 11.
993+
You can try some of the following options:
995994
- changing the IDE settings.
996995
- changing the JAVA_HOME environment variable.
997-
- changing `org.gradle.java.home` in `gradle.properties`.'''
998-
),
996+
- changing `org.gradle.java.home` in `gradle.properties`.
997+
''';
998+
999+
testWithoutContext('pattern', () {
1000+
expect(
1001+
incompatibleJavaAndAgpVersionsHandler.test(errorMessage),
9991002
isTrue,
10001003
);
10011004
});
10021005

10031006
testUsingContext('suggestion', () async {
1004-
await jvm11RequiredHandler.handler(
1005-
project: FakeFlutterProject(),
1007+
await incompatibleJavaAndAgpVersionsHandler.handler(
1008+
line: errorMessage,
1009+
project: FlutterProject.fromDirectoryTest(fileSystem.currentDirectory),
10061010
usesAndroidX: true,
1007-
line: '',
10081011
);
10091012

1010-
expect(
1011-
testLogger.statusText,
1012-
contains(
1013-
'\n'
1014-
'┌─ Flutter Fix ─────────────────────────────────────────────────────────────────┐\n'
1015-
'│ [!] You need Java 11 or higher to build your app with this version of Gradle. │\n'
1016-
'│ │\n'
1017-
'│ To get Java 11, update to the latest version of Android Studio on │\n'
1018-
'│ https://developer.android.com/studio/install. │\n'
1019-
'│ │\n'
1020-
'│ To check the Java version used by Flutter, run `flutter doctor -v`. │\n'
1021-
'└───────────────────────────────────────────────────────────────────────────────┘\n'
1022-
)
1023-
);
1013+
// Ensure the error notes the required Java version, the Java version currently used,
1014+
// the android studio and android sdk installation link, the flutter command to set
1015+
// the Java version Flutter uses, and the flutter doctor command.
1016+
expect(testLogger.statusText, contains('Android Gradle plugin requires Java 17 to run. You are currently using Java 11.'));
1017+
expect(testLogger.statusText, contains('https://developer.android.com/studio/install'));
1018+
expect(testLogger.statusText, contains('`flutter config --jdk-dir=“</path/to/jdk>“`'));
1019+
expect(testLogger.statusText, contains('`flutter doctor --verbose`'));
10241020
}, overrides: <Type, Generator>{
10251021
GradleUtils: () => FakeGradleUtils(),
10261022
Platform: () => fakePlatform('android'),

0 commit comments

Comments
 (0)