From 27232664668033751aad6faa0f7b37ef1bbac0ed Mon Sep 17 00:00:00 2001 From: Reid Baker Date: Thu, 13 Apr 2023 18:13:58 -0400 Subject: [PATCH] i123643 print java version gradle (#123644) #123643 - Add task to projects evaluated by flutter.gradle that will print the java version. - Add integration test for the existence of javaVersion and the expected format. - Add gradle util to get the gradlew version for a specific platform (gradlew everywhere but windows). Why does this code need to exist? Figuring out what version of java is used by flutter/gradle is done in a few different ways that are not always aligned. See this issue https://github.com/flutter/flutter/issues/122609 , this issue https://github.com/flutter/flutter/issues/121501 this feature request https://github.com/flutter/flutter/issues/106416 As examples of why assuming the java version is dangerous. This task is code flutter can build upon and is the version gradle is using to build no matter how it is configured. ## 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] All existing and new tests are passing. --------- Co-authored-by: Mitchell Goodwin <58190796+MitchellGoodwin@users.noreply.github.com> Co-authored-by: Greg Spencer Co-authored-by: Victoria Ashworth Co-authored-by: Christopher Fujino Co-authored-by: Jackson Gardner Co-authored-by: Rydmike Co-authored-by: keyonghan <54558023+keyonghan@users.noreply.github.com> Co-authored-by: chunhtai <47866232+chunhtai@users.noreply.github.com> Co-authored-by: Taha Tesser Co-authored-by: Ben Konyi Co-authored-by: engine-flutter-autoroll Co-authored-by: hellohuanlin <41930132+hellohuanlin@users.noreply.github.com> Co-authored-by: Danny Tuppeny Co-authored-by: Chris Bracken Co-authored-by: Kate Lovett Co-authored-by: Michael Goderbauer Co-authored-by: Elias Yishak <42216813+eliasyishak@users.noreply.github.com> Co-authored-by: Christopher Fujino --- packages/flutter_tools/gradle/flutter.gradle | 18 ++++++ .../lib/src/android/gradle_utils.dart | 14 ++++- .../android/gradle_utils_test.dart | 20 +++++++ .../android_gradle_java_version_test.dart | 57 +++++++++++++++++++ 4 files changed, 106 insertions(+), 3 deletions(-) create mode 100644 packages/flutter_tools/test/integration.shard/android_gradle_java_version_test.dart diff --git a/packages/flutter_tools/gradle/flutter.gradle b/packages/flutter_tools/gradle/flutter.gradle index f7e44b334d..089c43330f 100644 --- a/packages/flutter_tools/gradle/flutter.gradle +++ b/packages/flutter_tools/gradle/flutter.gradle @@ -678,6 +678,23 @@ class FlutterPlugin implements Plugin { project.dependencies.add(configuration, dependency, config) } + // Add a task that can be called on flutter projects that prints the java version used in gradle. + // + // Format of the output of this task can be used in debugging what version of java gradle is using. + // Not recomended for use in time sensitive commands like `flutter run` or `flutter build` as + // gradle is slower than we want. Particularly in light of https://github.com/flutter/flutter/issues/119196. + // + private static void addTaskForJavaVersion(Project project) { + // Warning: the name of this task is used by other code. Change with caution. + project.tasks.register('javaVersion') { + description 'Print the current java version used by gradle. ' + 'see: https://docs.gradle.org/current/javadoc/org/gradle/api/JavaVersion.html' + doLast { + println(JavaVersion.current()) + } + } + } + /** * Returns a Flutter build mode suitable for the specified Android buildType. * @@ -850,6 +867,7 @@ class FlutterPlugin implements Plugin { if (project.hasProperty('validate-deferred-components')) { validateDeferredComponentsValue = project.property('validate-deferred-components').toBoolean() } + addTaskForJavaVersion(project) def targetPlatforms = getTargetPlatforms() def addFlutterDeps = { variant -> if (shouldSplitPerAbi()) { diff --git a/packages/flutter_tools/lib/src/android/gradle_utils.dart b/packages/flutter_tools/lib/src/android/gradle_utils.dart index 02b11d051a..3c4e541446 100644 --- a/packages/flutter_tools/lib/src/android/gradle_utils.dart +++ b/packages/flutter_tools/lib/src/android/gradle_utils.dart @@ -96,9 +96,8 @@ class GradleUtils { final Directory androidDir = project.android.hostAppGradleRoot; injectGradleWrapperIfNeeded(androidDir); - final File gradle = androidDir.childFile( - _platform.isWindows ? 'gradlew.bat' : 'gradlew', - ); + final File gradle = androidDir.childFile(getGradlewFileName(_platform)); + if (gradle.existsSync()) { _logger.printTrace('Using gradle from ${gradle.absolute.path}.'); // If the Gradle executable doesn't have execute permission, @@ -705,3 +704,12 @@ class GradleForAgp { final String agpMax; final String minRequiredGradle; } + +// Returns gradlew file name based on the platform. +String getGradlewFileName(Platform platform) { + if (platform.isWindows) { + return 'gradlew.bat'; + } else { + return 'gradlew'; + } +} diff --git a/packages/flutter_tools/test/general.shard/android/gradle_utils_test.dart b/packages/flutter_tools/test/general.shard/android/gradle_utils_test.dart index fea5ee3e95..054bec557c 100644 --- a/packages/flutter_tools/test/general.shard/android/gradle_utils_test.dart +++ b/packages/flutter_tools/test/general.shard/android/gradle_utils_test.dart @@ -209,6 +209,12 @@ void main() { androidDirectory.childFile('gradlew').path, ); }); + testWithoutContext('getGradleFileName for notWindows', () { + expect(getGradlewFileName(notWindowsPlatform), 'gradlew'); + }); + testWithoutContext('getGradleFileName for windows', () { + expect(getGradlewFileName(windowsPlatform), 'gradlew.bat'); + }); testWithoutContext('returns the gradle wrapper version', () async { const String expectedVersion = '7.4.2'; @@ -622,3 +628,17 @@ class JavaGradleTestData { final String? javaVersion; final bool validPair; } + +final Platform windowsPlatform = FakePlatform( + operatingSystem: 'windows', + environment: { + 'PROGRAMFILES(X86)': r'C:\Program Files (x86)\', + 'FLUTTER_ROOT': r'C:\flutter', + 'USERPROFILE': '/', + } +); +final Platform notWindowsPlatform = FakePlatform( + environment: { + 'FLUTTER_ROOT': r'/users/someuser/flutter', + } +); diff --git a/packages/flutter_tools/test/integration.shard/android_gradle_java_version_test.dart b/packages/flutter_tools/test/integration.shard/android_gradle_java_version_test.dart new file mode 100644 index 0000000000..cfc9cef09a --- /dev/null +++ b/packages/flutter_tools/test/integration.shard/android_gradle_java_version_test.dart @@ -0,0 +1,57 @@ +// Copyright 2014 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'package:file/file.dart'; +import 'package:flutter_tools/src/android/gradle_utils.dart' + show getGradlewFileName; +import 'package:flutter_tools/src/base/io.dart'; + +import '../src/common.dart'; +import 'test_utils.dart'; + +void main() { + late Directory tempDir; + + setUp(() async { + tempDir = createResolvedTempDirectorySync('run_test.'); + }); + + tearDown(() async { + tryToDelete(tempDir); + }); + + testWithoutContext( + 'gradle task exists named javaVersion that prints jdk version', () async { + // Create a new flutter project. + final String flutterBin = + fileSystem.path.join(getFlutterRoot(), 'bin', 'flutter'); + ProcessResult result = await processManager.run([ + flutterBin, + 'create', + tempDir.path, + '--project-name=testapp', + ], workingDirectory: tempDir.path); + expect(result.exitCode, 0); + // Ensure that gradle files exists from templates. + result = await processManager.run([ + flutterBin, + 'build', + 'apk', + '--config-only', + ], workingDirectory: tempDir.path); + expect(result.exitCode, 0); + + final Directory androidApp = tempDir.childDirectory('android'); + result = await processManager.run([ + '.${platform.pathSeparator}${getGradlewFileName(platform)}', + ...getLocalEngineArguments(), + '-q', // quiet output. + 'javaVersion', + ], workingDirectory: androidApp.path); + // Verify that gradlew has a javaVersion task. + expect(result.exitCode, 0); + // Verify the format is a number on its own line. + expect(result.stdout.toString(), matches(RegExp(r'\d+$', multiLine: true))); + }); +}