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](https://github.com/flutter/flutter/issues/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.
This commit is contained in:
parent
f9351fae7e
commit
05c454c28c
@ -72,7 +72,7 @@ final List<GradleHandledError> gradleErrors = <GradleHandledError>[
|
|||||||
transformInputIssueHandler,
|
transformInputIssueHandler,
|
||||||
lockFileDepMissingHandler,
|
lockFileDepMissingHandler,
|
||||||
minCompileSdkVersionHandler,
|
minCompileSdkVersionHandler,
|
||||||
jvm11RequiredHandler,
|
incompatibleJavaAndAgpVersionsHandler,
|
||||||
outdatedGradleHandler,
|
outdatedGradleHandler,
|
||||||
sslExceptionHandler,
|
sslExceptionHandler,
|
||||||
zipExceptionHandler,
|
zipExceptionHandler,
|
||||||
@ -503,25 +503,38 @@ final GradleHandledError minCompileSdkVersionHandler = GradleHandledError(
|
|||||||
eventLabel: 'min-compile-sdk-version',
|
eventLabel: 'min-compile-sdk-version',
|
||||||
);
|
);
|
||||||
|
|
||||||
|
final RegExp _agpJavaError = RegExp(r'Android Gradle plugin requires Java (\d+\.?\d*) to run');
|
||||||
|
|
||||||
|
// If an incompatible Java and Android Gradle Plugin error is caught,
|
||||||
|
// Android Gradle Plugin throws the required Java version to fix the error.
|
||||||
|
// Android Gradle Plugin handles the error here: http://shortn/_SgUWyRdywL.
|
||||||
|
|
||||||
|
// If we ever need to reference or check the thrown requirements,
|
||||||
|
// we can find the Java and Android Gradle Plugin compatability here:
|
||||||
|
// 'https://developer.android.com/build/releases/past-releases'
|
||||||
@visibleForTesting
|
@visibleForTesting
|
||||||
final GradleHandledError jvm11RequiredHandler = GradleHandledError(
|
final GradleHandledError incompatibleJavaAndAgpVersionsHandler= GradleHandledError(
|
||||||
test: (String line) {
|
test: (String line) {
|
||||||
return line.contains('Android Gradle plugin requires Java 11 to run');
|
return _agpJavaError.hasMatch(line);
|
||||||
},
|
},
|
||||||
handler: ({
|
handler: ({
|
||||||
required String line,
|
required String line,
|
||||||
required FlutterProject project,
|
required FlutterProject project,
|
||||||
required bool usesAndroidX,
|
required bool usesAndroidX,
|
||||||
}) async {
|
}) async {
|
||||||
|
final String helpfulGradleError = line.trim().substring(2);
|
||||||
|
|
||||||
globals.printBox(
|
globals.printBox(
|
||||||
'${globals.logger.terminal.warningMark} You need Java 11 or higher to build your app with this version of Gradle.\n\n'
|
'${globals.logger.terminal.warningMark} $helpfulGradleError\n\n'
|
||||||
'To get Java 11, update to the latest version of Android Studio on https://developer.android.com/studio/install.\n\n'
|
'To fix this issue, try updating to the latest Android SDK and Android Studio on: ${AndroidProject.installAndroidStudioUrl}\n'
|
||||||
'To check the Java version used by Flutter, run `flutter doctor -v`.',
|
'If that does not work, you can set the Java version used by Flutter by \n'
|
||||||
|
'running `flutter config --jdk-dir=“</path/to/jdk>“`\n\n'
|
||||||
|
'To check the Java version used by Flutter, run `flutter doctor --verbose`',
|
||||||
title: _boxTitle,
|
title: _boxTitle,
|
||||||
);
|
);
|
||||||
return GradleBuildStatus.exit;
|
return GradleBuildStatus.exit;
|
||||||
},
|
},
|
||||||
eventLabel: 'java11-required',
|
eventLabel: 'incompatible-java-agp-version',
|
||||||
);
|
);
|
||||||
|
|
||||||
/// Handles SSL exceptions: https://github.com/flutter/flutter/issues/104628
|
/// Handles SSL exceptions: https://github.com/flutter/flutter/issues/104628
|
||||||
|
@ -453,6 +453,11 @@ class AndroidProject extends FlutterProjectPlatform {
|
|||||||
static const String javaGradleCompatUrl =
|
static const String javaGradleCompatUrl =
|
||||||
'https://docs.gradle.org/current/userguide/compatibility.html#java';
|
'https://docs.gradle.org/current/userguide/compatibility.html#java';
|
||||||
|
|
||||||
|
// User facing link that describes instructions for downloading
|
||||||
|
// the latest version of Android Studio.
|
||||||
|
static const String installAndroidStudioUrl =
|
||||||
|
'https://developer.android.com/studio/install';
|
||||||
|
|
||||||
/// The parent of this project.
|
/// The parent of this project.
|
||||||
final FlutterProject parent;
|
final FlutterProject parent;
|
||||||
|
|
||||||
|
@ -43,7 +43,7 @@ void main() {
|
|||||||
transformInputIssueHandler,
|
transformInputIssueHandler,
|
||||||
lockFileDepMissingHandler,
|
lockFileDepMissingHandler,
|
||||||
minCompileSdkVersionHandler,
|
minCompileSdkVersionHandler,
|
||||||
jvm11RequiredHandler,
|
incompatibleJavaAndAgpVersionsHandler,
|
||||||
outdatedGradleHandler,
|
outdatedGradleHandler,
|
||||||
sslExceptionHandler,
|
sslExceptionHandler,
|
||||||
zipExceptionHandler,
|
zipExceptionHandler,
|
||||||
@ -983,44 +983,40 @@ Execution failed for task ':app:checkDebugAarMetadata'.
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
group('Java 11 requirement', () {
|
group('incompatible java and android gradle plugin versions error', () {
|
||||||
testWithoutContext('pattern', () {
|
|
||||||
expect(
|
const String errorMessage = '''
|
||||||
jvm11RequiredHandler.test('''
|
|
||||||
* What went wrong:
|
* What went wrong:
|
||||||
A problem occurred evaluating project ':flutter'.
|
An exception occurred applying plugin request [id: 'com.android.application']
|
||||||
> Failed to apply plugin 'com.android.internal.library'.
|
> Failed to apply plugin 'com.android.internal.application'.
|
||||||
> Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8.
|
> Android Gradle plugin requires Java 17 to run. You are currently using Java 11.
|
||||||
You can try some of the following options:
|
You can try some of the following options:
|
||||||
- changing the IDE settings.
|
- changing the IDE settings.
|
||||||
- changing the JAVA_HOME environment variable.
|
- changing the JAVA_HOME environment variable.
|
||||||
- changing `org.gradle.java.home` in `gradle.properties`.'''
|
- changing `org.gradle.java.home` in `gradle.properties`.
|
||||||
),
|
''';
|
||||||
|
|
||||||
|
testWithoutContext('pattern', () {
|
||||||
|
expect(
|
||||||
|
incompatibleJavaAndAgpVersionsHandler.test(errorMessage),
|
||||||
isTrue,
|
isTrue,
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
testUsingContext('suggestion', () async {
|
testUsingContext('suggestion', () async {
|
||||||
await jvm11RequiredHandler.handler(
|
await incompatibleJavaAndAgpVersionsHandler.handler(
|
||||||
project: FakeFlutterProject(),
|
line: errorMessage,
|
||||||
|
project: FlutterProject.fromDirectoryTest(fileSystem.currentDirectory),
|
||||||
usesAndroidX: true,
|
usesAndroidX: true,
|
||||||
line: '',
|
|
||||||
);
|
);
|
||||||
|
|
||||||
expect(
|
// Ensure the error notes the required Java version, the Java version currently used,
|
||||||
testLogger.statusText,
|
// the android studio and android sdk installation link, the flutter command to set
|
||||||
contains(
|
// the Java version Flutter uses, and the flutter doctor command.
|
||||||
'\n'
|
expect(testLogger.statusText, contains('Android Gradle plugin requires Java 17 to run. You are currently using Java 11.'));
|
||||||
'┌─ Flutter Fix ─────────────────────────────────────────────────────────────────┐\n'
|
expect(testLogger.statusText, contains('https://developer.android.com/studio/install'));
|
||||||
'│ [!] You need Java 11 or higher to build your app with this version of Gradle. │\n'
|
expect(testLogger.statusText, contains('`flutter config --jdk-dir=“</path/to/jdk>“`'));
|
||||||
'│ │\n'
|
expect(testLogger.statusText, contains('`flutter doctor --verbose`'));
|
||||||
'│ To get Java 11, update to the latest version of Android Studio on │\n'
|
|
||||||
'│ https://developer.android.com/studio/install. │\n'
|
|
||||||
'│ │\n'
|
|
||||||
'│ To check the Java version used by Flutter, run `flutter doctor -v`. │\n'
|
|
||||||
'└───────────────────────────────────────────────────────────────────────────────┘\n'
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}, overrides: <Type, Generator>{
|
}, overrides: <Type, Generator>{
|
||||||
GradleUtils: () => FakeGradleUtils(),
|
GradleUtils: () => FakeGradleUtils(),
|
||||||
Platform: () => fakePlatform('android'),
|
Platform: () => fakePlatform('android'),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user