Skip to content

Commit 0052566

Browse files
author
Emmanuel Garcia
authored
Show custom error message when Kotlin or Gradle bump is required (#102421)
1 parent e8f8a82 commit 0052566

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

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

+38-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import '../globals.dart' as globals;
1212
import '../project.dart';
1313
import '../reporting/reporting.dart';
1414
import 'android_studio.dart';
15+
import 'gradle_utils.dart';
1516
import 'multidex.dart';
1617

1718
typedef GradleErrorTest = bool Function(String);
@@ -78,6 +79,7 @@ final List<GradleHandledError> gradleErrors = <GradleHandledError>[
7879
incompatibleKotlinVersionHandler,
7980
minCompileSdkVersionHandler,
8081
jvm11RequiredHandler,
82+
outdatedGradleHandler,
8183
];
8284

8385
const String _boxTitle = 'Flutter Fix';
@@ -487,7 +489,7 @@ final GradleHandledError lockFileDepMissingHandler = GradleHandledError(
487489
@visibleForTesting
488490
final GradleHandledError incompatibleKotlinVersionHandler = GradleHandledError(
489491
test: _lineMatcher(const <String>[
490-
'Module was compiled with an incompatible version of Kotlin',
492+
'was compiled with an incompatible version of Kotlin',
491493
]),
492494
handler: ({
493495
required String line,
@@ -509,6 +511,41 @@ final GradleHandledError incompatibleKotlinVersionHandler = GradleHandledError(
509511
eventLabel: 'incompatible-kotlin-version',
510512
);
511513

514+
final RegExp _outdatedGradlePattern = RegExp(r'The current Gradle version (.+) is not compatible with the Kotlin Gradle plugin');
515+
516+
@visibleForTesting
517+
final GradleHandledError outdatedGradleHandler = GradleHandledError(
518+
test: _outdatedGradlePattern.hasMatch,
519+
handler: ({
520+
required String line,
521+
required FlutterProject project,
522+
required bool usesAndroidX,
523+
required bool multidexEnabled,
524+
}) async {
525+
final File gradleFile = project.directory
526+
.childDirectory('android')
527+
.childFile('build.gradle');
528+
final File gradlePropertiesFile = project.directory
529+
.childDirectory('android')
530+
.childDirectory('gradle')
531+
.childDirectory('wrapper')
532+
.childFile('gradle-wrapper.properties');
533+
globals.printBox(
534+
'${globals.logger.terminal.warningMark} Your project needs to upgrade Gradle and the Android Gradle plugin.\n\n'
535+
'To fix this issue, replace the following content:\n'
536+
'${gradleFile.path}:\n'
537+
' ${globals.terminal.color("- classpath 'com.android.tools.build:gradle:<current-version>'", TerminalColor.red)}\n'
538+
' ${globals.terminal.color("+ classpath 'com.android.tools.build:gradle:$templateAndroidGradlePluginVersion'", TerminalColor.green)}\n'
539+
'${gradlePropertiesFile.path}:\n'
540+
' ${globals.terminal.color('- https://services.gradle.org/distributions/gradle-<current-version>-all.zip', TerminalColor.red)}\n'
541+
' ${globals.terminal.color('+ https://services.gradle.org/distributions/gradle-$templateDefaultGradleVersion-all.zip', TerminalColor.green)}',
542+
title: _boxTitle,
543+
);
544+
return GradleBuildStatus.exit;
545+
},
546+
eventLabel: 'outdated-gradle-version',
547+
);
548+
512549
final RegExp _minCompileSdkVersionPattern = RegExp(r'The minCompileSdk \(([0-9]+)\) specified in a');
513550

514551
@visibleForTesting

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

+50
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ void main() {
3636
incompatibleKotlinVersionHandler,
3737
minCompileSdkVersionHandler,
3838
jvm11RequiredHandler,
39+
outdatedGradleHandler,
3940
])
4041
);
4142
});
@@ -877,6 +878,10 @@ Execution failed for task ':app:generateDebugFeatureTransitiveDeps'.
877878
incompatibleKotlinVersionHandler.test('Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.5.1, expected version is 1.1.15.'),
878879
isTrue,
879880
);
881+
expect(
882+
incompatibleKotlinVersionHandler.test("class 'kotlin.Unit' was compiled with an incompatible version of Kotlin."),
883+
isTrue,
884+
);
880885
});
881886

882887
testUsingContext('suggestion', () async {
@@ -904,6 +909,51 @@ Execution failed for task ':app:generateDebugFeatureTransitiveDeps'.
904909
});
905910
});
906911

912+
group('Bump Gradle', () {
913+
const String errorMessage = '''
914+
A problem occurred evaluating project ':app'.
915+
> Failed to apply plugin [id 'kotlin-android']
916+
> The current Gradle version 4.10.2 is not compatible with the Kotlin Gradle plugin. Please use Gradle 6.1.1 or newer, or the previous version of the Kotlin plugin.
917+
''';
918+
919+
testWithoutContext('pattern', () {
920+
expect(
921+
outdatedGradleHandler.test(errorMessage),
922+
isTrue,
923+
);
924+
});
925+
926+
testUsingContext('suggestion', () async {
927+
await outdatedGradleHandler.handler(
928+
line: errorMessage,
929+
project: FlutterProject.fromDirectoryTest(globals.fs.currentDirectory),
930+
);
931+
932+
expect(
933+
testLogger.statusText,
934+
contains(
935+
'\n'
936+
'┌─ Flutter Fix ────────────────────────────────────────────────────────────────────┐\n'
937+
'│ [!] Your project needs to upgrade Gradle and the Android Gradle plugin. │\n'
938+
'│ │\n'
939+
'│ To fix this issue, replace the following content: │\n'
940+
'│ /android/build.gradle: │\n'
941+
"│ - classpath 'com.android.tools.build:gradle:<current-version>' │\n"
942+
"│ + classpath 'com.android.tools.build:gradle:7.1.2' │\n"
943+
'│ /android/gradle/wrapper/gradle-wrapper.properties: │\n'
944+
'│ - https://services.gradle.org/distributions/gradle-<current-version>-all.zip │\n'
945+
'│ + https://services.gradle.org/distributions/gradle-7.4-all.zip │\n'
946+
'└──────────────────────────────────────────────────────────────────────────────────┘\n'
947+
)
948+
);
949+
}, overrides: <Type, Generator>{
950+
GradleUtils: () => FakeGradleUtils(),
951+
Platform: () => fakePlatform('android'),
952+
FileSystem: () => MemoryFileSystem.test(),
953+
ProcessManager: () => FakeProcessManager.empty(),
954+
});
955+
});
956+
907957
group('Required compileSdkVersion', () {
908958
const String errorMessage = '''
909959
Execution failed for task ':app:checkDebugAarMetadata'.

0 commit comments

Comments
 (0)