From 0a9148870ad985267151b5003eefc43a51c0e42d Mon Sep 17 00:00:00 2001 From: Shashwat Pathak <111122076+shashwatpathak98@users.noreply.github.com> Date: Wed, 11 Sep 2024 21:51:16 +0530 Subject: [PATCH] Fix `flutter create` warning regarding Java compatibility (#152836) This PR modifies the warning message regarding Java compatibility to make it consistent with the Flutter style guide. **Current message looks like this** ``` [RECOMMENDED] If so, to keep the default AGP version 8.1.0, make sure to download a compatible Java version (Java 17 <= compatible Java version < Java 21). You may configure this compatible Java version by running: `flutter config --jdk-dir=` Note that this is a global configuration for Flutter. Alternatively, to continue using your configured Java version, update the AGP version specified in the following files to a compatible AGP version (minimum compatible AGP version: 7.0) as necessary: - /home/user/project/testproj/android/build.gradle See https://developer.android.com/build/releases/gradle-plugin for details on compatible Java/AGP versions. ``` **After Modification** ``` To keep the default AGP version 8.1.0, download a compatible Java version (Java 17 <= compatible Java version < Java 21). Configure this Java version globally for Flutter by running: flutter config --jdk-dir= Alternatively, to continue using your current Java version, update the AGP version in the following file(s) to a compatible version (minimum AGP version: 7.0): /home/user/project/testproj/android/build.gradle For details on compatible Java and AGP versions, see https://developer.android.com/build/releases/gradle-plugin ``` Fixes #152460 --- .../lib/src/commands/create.dart | 23 ++++++++----------- .../commands.shard/permeable/create_test.dart | 16 +++++++++++++ 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/packages/flutter_tools/lib/src/commands/create.dart b/packages/flutter_tools/lib/src/commands/create.dart index 9b21bbb7f7..992c6914b7 100644 --- a/packages/flutter_tools/lib/src/commands/create.dart +++ b/packages/flutter_tools/lib/src/commands/create.dart @@ -959,19 +959,17 @@ used. // AGP template version incompatible with Java version. final gradle.JavaAgpCompat? minimumCompatibleAgpVersion = gradle.getMinimumAgpVersionForJavaVersion(globals.logger, javaV: javaVersion); final String compatibleAgpVersionMessage = minimumCompatibleAgpVersion == null ? '' : ' (minimum compatible AGP version: ${minimumCompatibleAgpVersion.agpMin})'; - final String gradleBuildFilePaths = ' - ${_getBuildGradleConfigurationFilePaths(projectType, projectDirPath)!.join('\n - ')}'; + final String gradleBuildFilePaths = ' ${_getBuildGradleConfigurationFilePaths(projectType, projectDirPath)!.join('\n - ')}'; globals.printWarning(''' $incompatibleVersionsAndRecommendedOptionMessage -Alternatively, to continue using your configured Java version, update the AGP -version specified in the following files to a compatible AGP -version$compatibleAgpVersionMessage as necessary: +Alternatively, to continue using your current Java version, update the AGP +version in the following file(s) to a compatible version$compatibleAgpVersionMessage: $gradleBuildFilePaths -See -https://developer.android.com/build/releases/gradle-plugin for details on -compatible Java/AGP versions. +For details on compatible Java and AGP versions, see +https://developer.android.com/build/releases/gradle-plugin ''', emphasis: true ); @@ -994,12 +992,11 @@ String getIncompatibleJavaGradleAgpMessageHeader( return ''' The configured version of Java detected may conflict with the $incompatibleDependency version in your new Flutter $projectType. -[RECOMMENDED] If so, to keep the default $incompatibleDependencyVersion, make -sure to download a compatible Java version -$validJavaRangeMessage. -You may configure this compatible Java version by running: -`flutter config --jdk-dir=` -Note that this is a global configuration for Flutter. +To keep the default AGP version $incompatibleDependencyVersion, download a compatible Java version +(Java 17 <= $validJavaRangeMessage Java version < Java 21). Configure this Java version +globally for Flutter by running: + + flutter config --jdk-dir= '''; } diff --git a/packages/flutter_tools/test/commands.shard/permeable/create_test.dart b/packages/flutter_tools/test/commands.shard/permeable/create_test.dart index aa0a1ff115..1c5408fd0d 100644 --- a/packages/flutter_tools/test/commands.shard/permeable/create_test.dart +++ b/packages/flutter_tools/test/commands.shard/permeable/create_test.dart @@ -3926,6 +3926,22 @@ void main() { ), ProcessManager: () => fakeProcessManager, }); + + testUsingContext('flutter create should show the incompatible java AGP message', () async { + Cache.flutterRoot = '../..'; + + final CreateCommand command = CreateCommand(); + final CommandRunner runner = createTestCommandRunner(command); + + await runner.run(['create', '--no-pub', '--platforms=android', projectDir.path]); + + final String expectedMessage = getIncompatibleJavaGradleAgpMessageHeader(false, templateDefaultGradleVersion, templateAndroidGradlePluginVersion, 'app'); + + expect(logger.warningText, contains(expectedMessage)); + }, overrides: { + Java: () => FakeJava(version: const software.Version.withText(500, 0, 0, '500.0.0')), // Too high a version for template Gradle versions. + Logger: () => logger, + }); } Future _createProject(