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 <gspencergoog@users.noreply.github.com>
Co-authored-by: Victoria Ashworth <vashworth@google.com>
Co-authored-by: Christopher Fujino <christopherfujino@gmail.com>
Co-authored-by: Jackson Gardner <jacksongardner@google.com>
Co-authored-by: Rydmike <m.rydstrom@gmail.com>
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 <tessertaha@gmail.com>
Co-authored-by: Ben Konyi <bkonyi@google.com>
Co-authored-by: engine-flutter-autoroll <engine-flutter-autoroll@skia.org>
Co-authored-by: hellohuanlin <41930132+hellohuanlin@users.noreply.github.com>
Co-authored-by: Danny Tuppeny <danny@tuppeny.com>
Co-authored-by: Chris Bracken <chris@bracken.jp>
Co-authored-by: Kate Lovett <katelovett@google.com>
Co-authored-by: Michael Goderbauer <goderbauer@google.com>
Co-authored-by: Elias Yishak <42216813+eliasyishak@users.noreply.github.com>
Co-authored-by: Christopher Fujino <fujino@google.com>
This commit is contained in:
Reid Baker 2023-04-13 18:13:58 -04:00 committed by GitHub
parent 082227b4ee
commit 2723266466
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 106 additions and 3 deletions

View File

@ -678,6 +678,23 @@ class FlutterPlugin implements Plugin<Project> {
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<Project> {
if (project.hasProperty('validate-deferred-components')) {
validateDeferredComponentsValue = project.property('validate-deferred-components').toBoolean()
}
addTaskForJavaVersion(project)
def targetPlatforms = getTargetPlatforms()
def addFlutterDeps = { variant ->
if (shouldSplitPerAbi()) {

View File

@ -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';
}
}

View File

@ -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: <String, String>{
'PROGRAMFILES(X86)': r'C:\Program Files (x86)\',
'FLUTTER_ROOT': r'C:\flutter',
'USERPROFILE': '/',
}
);
final Platform notWindowsPlatform = FakePlatform(
environment: <String, String>{
'FLUTTER_ROOT': r'/users/someuser/flutter',
}
);

View File

@ -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(<String>[
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(<String>[
flutterBin,
'build',
'apk',
'--config-only',
], workingDirectory: tempDir.path);
expect(result.exitCode, 0);
final Directory androidApp = tempDir.childDirectory('android');
result = await processManager.run(<String>[
'.${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)));
});
}