Unverified Commit 27232664 authored by Reid Baker's avatar Reid Baker Committed by GitHub

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: 's avatarMitchell Goodwin <58190796+MitchellGoodwin@users.noreply.github.com>
Co-authored-by: 's avatarGreg Spencer <gspencergoog@users.noreply.github.com>
Co-authored-by: 's avatarVictoria Ashworth <vashworth@google.com>
Co-authored-by: 's avatarChristopher Fujino <christopherfujino@gmail.com>
Co-authored-by: 's avatarJackson Gardner <jacksongardner@google.com>
Co-authored-by: 's avatarRydmike <m.rydstrom@gmail.com>
Co-authored-by: 's avatarkeyonghan <54558023+keyonghan@users.noreply.github.com>
Co-authored-by: 's avatarchunhtai <47866232+chunhtai@users.noreply.github.com>
Co-authored-by: 's avatarTaha Tesser <tessertaha@gmail.com>
Co-authored-by: 's avatarBen Konyi <bkonyi@google.com>
Co-authored-by: 's avatarengine-flutter-autoroll <engine-flutter-autoroll@skia.org>
Co-authored-by: 's avatarhellohuanlin <41930132+hellohuanlin@users.noreply.github.com>
Co-authored-by: 's avatarDanny Tuppeny <danny@tuppeny.com>
Co-authored-by: 's avatarChris Bracken <chris@bracken.jp>
Co-authored-by: 's avatarKate Lovett <katelovett@google.com>
Co-authored-by: 's avatarMichael Goderbauer <goderbauer@google.com>
Co-authored-by: 's avatarElias Yishak <42216813+eliasyishak@users.noreply.github.com>
Co-authored-by: 's avatarChristopher Fujino <fujino@google.com>
parent 082227b4
...@@ -678,6 +678,23 @@ class FlutterPlugin implements Plugin<Project> { ...@@ -678,6 +678,23 @@ class FlutterPlugin implements Plugin<Project> {
project.dependencies.add(configuration, dependency, config) 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. * Returns a Flutter build mode suitable for the specified Android buildType.
* *
...@@ -850,6 +867,7 @@ class FlutterPlugin implements Plugin<Project> { ...@@ -850,6 +867,7 @@ class FlutterPlugin implements Plugin<Project> {
if (project.hasProperty('validate-deferred-components')) { if (project.hasProperty('validate-deferred-components')) {
validateDeferredComponentsValue = project.property('validate-deferred-components').toBoolean() validateDeferredComponentsValue = project.property('validate-deferred-components').toBoolean()
} }
addTaskForJavaVersion(project)
def targetPlatforms = getTargetPlatforms() def targetPlatforms = getTargetPlatforms()
def addFlutterDeps = { variant -> def addFlutterDeps = { variant ->
if (shouldSplitPerAbi()) { if (shouldSplitPerAbi()) {
......
...@@ -96,9 +96,8 @@ class GradleUtils { ...@@ -96,9 +96,8 @@ class GradleUtils {
final Directory androidDir = project.android.hostAppGradleRoot; final Directory androidDir = project.android.hostAppGradleRoot;
injectGradleWrapperIfNeeded(androidDir); injectGradleWrapperIfNeeded(androidDir);
final File gradle = androidDir.childFile( final File gradle = androidDir.childFile(getGradlewFileName(_platform));
_platform.isWindows ? 'gradlew.bat' : 'gradlew',
);
if (gradle.existsSync()) { if (gradle.existsSync()) {
_logger.printTrace('Using gradle from ${gradle.absolute.path}.'); _logger.printTrace('Using gradle from ${gradle.absolute.path}.');
// If the Gradle executable doesn't have execute permission, // If the Gradle executable doesn't have execute permission,
...@@ -705,3 +704,12 @@ class GradleForAgp { ...@@ -705,3 +704,12 @@ class GradleForAgp {
final String agpMax; final String agpMax;
final String minRequiredGradle; final String minRequiredGradle;
} }
// Returns gradlew file name based on the platform.
String getGradlewFileName(Platform platform) {
if (platform.isWindows) {
return 'gradlew.bat';
} else {
return 'gradlew';
}
}
...@@ -209,6 +209,12 @@ void main() { ...@@ -209,6 +209,12 @@ void main() {
androidDirectory.childFile('gradlew').path, 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 { testWithoutContext('returns the gradle wrapper version', () async {
const String expectedVersion = '7.4.2'; const String expectedVersion = '7.4.2';
...@@ -622,3 +628,17 @@ class JavaGradleTestData { ...@@ -622,3 +628,17 @@ class JavaGradleTestData {
final String? javaVersion; final String? javaVersion;
final bool validPair; 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',
}
);
// 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)));
});
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment