Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Java-Gradle-AGP validation to flutter analyze #123916

Merged
merged 68 commits into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from 59 commits
Commits
Show all changes
68 commits
Select commit Hold shift + click to select a range
a07f7ee
Add function to gradle_utils.dart that gets the gradle version from w…
reidbaker Mar 23, 2023
7b4f418
Add validator that alwasys returns false and test, plus todo in integ…
reidbaker Mar 23, 2023
2b95eb5
Update documentation, add constants in gradle_utils.dart
reidbaker Mar 24, 2023
477734e
Add method to get agp version, add method to validate agp against gra…
reidbaker Mar 24, 2023
5a8005a
Add task to projects evaluated by flutter.gradle that will print the …
reidbaker Mar 28, 2023
e05aa96
Merge branch 'master' into i122376-warn-on-gradle-java-AGP-incompatib…
reidbaker Mar 30, 2023
53e89a0
Update dart doc for validateGradleAndAgp to describe where the info c…
reidbaker Mar 30, 2023
af3c647
Fill out and test java gradle compatability function in gradle_utils
reidbaker Mar 30, 2023
7e643a6
Hook up java gradle evaluateion to hasValidJavaGradleAgpVersions with…
reidbaker Mar 30, 2023
6b4ed1b
Add java --version output parsing and tests
reidbaker Mar 31, 2023
96cae3d
Add getJavaBinary test
reidbaker Mar 31, 2023
5e08847
Update comment in android_sdk for mac behavior with java_home -v
reidbaker Apr 1, 2023
b263dfc
Update format of group java/gradle/agp tests to have one test per pai…
reidbaker Apr 1, 2023
1a5e9e1
Handle mid java values
reidbaker Apr 1, 2023
4749a95
pre review feedback and cleanup of todos
reidbaker Apr 1, 2023
fff074e
Undo formatting
reidbaker Apr 1, 2023
2df3691
Undo formatting 2
reidbaker Apr 1, 2023
3815b0c
Add fake AndroidStudio and more formatting fixes
reidbaker Apr 1, 2023
21b8887
remove hardcoded values
reidbaker Apr 1, 2023
87ea610
make getJavaVersion callable
reidbaker Apr 1, 2023
7bc2f4b
Undo accidental revert of project.dart using java version
reidbaker Apr 1, 2023
565dd58
Add validator to list
reidbaker Apr 1, 2023
51903c4
Update analyze suggetions integration test with java/gradle/agp confi…
reidbaker Apr 2, 2023
a84e4b6
Add window java output test
reidbaker Apr 2, 2023
c2df14e
copyright formatting
reidbaker Apr 2, 2023
453889c
Make isWithinVersionRange public and move it as a static helper in ve…
reidbaker Apr 2, 2023
07213fb
Add tests to isWithinVersionRange
reidbaker Apr 2, 2023
860b861
Make maxKnownAgpVersion visible for testing, and use it in a test, ma…
reidbaker Apr 2, 2023
8d379d6
Merge branch 'master' into i122376-warn-on-gradle-java-AGP-incompatib…
reidbaker Apr 2, 2023
54d1b99
dart fix --apply
reidbaker Apr 2, 2023
6ce48fe
Update error description to have better formatting
reidbaker Apr 2, 2023
2641a53
Return better result value from java/gradle/agp validation with descr…
reidbaker Apr 2, 2023
ad94e9b
Addd test for java/gradle/agp validation test is failing
reidbaker Apr 2, 2023
3a8dc54
Add passing test for java/gradle/agp in project_test
reidbaker Apr 2, 2023
9295069
Add expected passing compatibility tests
reidbaker Apr 2, 2023
f9d61ba
Add failing cases and check string description
reidbaker Apr 2, 2023
cd20ff2
dart fix --apply
reidbaker Apr 2, 2023
0e45f6f
Undo some auto formatting to make reviewing easier
reidbaker Apr 2, 2023
5bc570c
Make getVersion non static and update tests
reidbaker Apr 2, 2023
b3823a5
Remove dead code replaces by overriding java version in fake
reidbaker Apr 2, 2023
0fb85f4
Merge branch 'master' into i122376-warn-on-gradle-java-AGP-incompatib…
reidbaker Apr 2, 2023
0c66529
remove dead code, use constants when avilable, fix todo formatting
reidbaker Apr 2, 2023
2484666
Remove no longer used filesystem
reidbaker Apr 2, 2023
dab39a0
Add --verbose to get logs to debug test/integration.shard/analyze_sug…
reidbaker Apr 2, 2023
42092ee
Merge branch 'master' into i122376-warn-on-gradle-java-AGP-incompatib…
reidbaker Apr 3, 2023
77db98e
Add dart doc to parseJavaVersion, updated regex to support optional t…
reidbaker Apr 3, 2023
69f7fb9
Add whitespace test on gradle format
reidbaker Apr 3, 2023
221f2e6
Review comments, spelling and an additional test to cover -beta and -…
reidbaker Apr 3, 2023
ab0e2e6
spelling, using late in testing to avoid test polution
reidbaker Apr 3, 2023
6d0268d
Formatting and reduction in test polution by isolating variable scope
reidbaker Apr 4, 2023
ee8ccb6
remove unused variable
reidbaker Apr 4, 2023
5b10e19
Add test for specific version output of ci https://chrome-infra-packa…
reidbaker Apr 4, 2023
09adddc
Update gradle version so that java version and gradle are compatible
reidbaker Apr 4, 2023
09c2c97
Review feedback
reidbaker Apr 4, 2023
fa7da03
spelling fix
reidbaker Apr 4, 2023
7f50a6f
Print trace logging for getJavaVersion and getJavaBinary
reidbaker Apr 4, 2023
76aa064
Make tests that depend on logger use testUsingContext
reidbaker Apr 4, 2023
64c7b8e
Fix todo format
reidbaker Apr 4, 2023
9f03e54
Merge branch 'master' into i122376-warn-on-gradle-java-AGP-incompatib…
reidbaker Apr 4, 2023
033cd4b
Reformat java/gradle compat to use a class and be a list
reidbaker Apr 4, 2023
a749ad8
Reformat todo
reidbaker Apr 4, 2023
7ba52c0
Merge branch 'master' into i122376-warn-on-gradle-java-AGP-incompatib…
reidbaker Apr 4, 2023
ae705bf
formatting and dart docs
reidbaker Apr 4, 2023
6e69b85
Handle newer patch versions of java and gradle and add test to cover
reidbaker Apr 4, 2023
da7b866
dart docs and formatting
reidbaker Apr 4, 2023
e0572ac
spelling fix
reidbaker Apr 4, 2023
3165cbf
Merge branch 'master' into i122376-warn-on-gradle-java-AGP-incompatib…
reidbaker Apr 4, 2023
deb320f
Make test value final
reidbaker Apr 4, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.1-all.zip
97 changes: 89 additions & 8 deletions packages/flutter_tools/lib/src/android/android_sdk.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:meta/meta.dart';

import '../base/common.dart';
import '../base/file_system.dart';
import '../base/os.dart';
Expand Down Expand Up @@ -409,6 +411,57 @@ class AndroidSdk {
return null;
}

// Returns the version of java in the format \d(.\d)+(.\d)+
// Returns null if version not found.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't these be ///?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch fixed.

String? getJavaVersion({
required AndroidStudio? androidStudio,
required FileSystem fileSystem,
required OperatingSystemUtils operatingSystemUtils,
required Platform platform,
required ProcessUtils processUtils,
}) {
final String? javaBinary = findJavaBinary(
androidStudio: androidStudio,
fileSystem: fileSystem,
operatingSystemUtils: operatingSystemUtils,
platform: platform,
);
if (javaBinary == null) {
globals.printTrace('Could not find java binary to get version.');
return null;
}
final RunResult result = processUtils.runSync(
<String>[javaBinary, '--version'],
environment: sdkManagerEnv,
);
if (result.exitCode != 0) {
globals.printTrace(
'java --version failed: exitCode: ${result.exitCode} stdout: ${result.stdout} stderr: ${result.stderr}');
return null;
}
return parseJavaVersion(result.stdout);
}

// Extracts jdk version from the output of java --version.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

Also, I prefer "JDK" to "jdk" :P

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed but I doubt I will get all the places updated.

@visibleForTesting
static String? parseJavaVersion(String rawVersionOutput) {
// The contents that matter come in the format '11.0.18' or '1.8.0_202'.
final RegExp jdkVersionRegex = RegExp(r'\d+\.\d+(\.\d+(?:_\d+)?)?');
final Iterable<RegExpMatch> matches =
jdkVersionRegex.allMatches(rawVersionOutput);
if (matches.isEmpty) {
christopherfujino marked this conversation as resolved.
Show resolved Hide resolved
globals.logger.printWarning(_formatJavaVersionWarning(rawVersionOutput));
return null;
}
final String? versionString = matches.first.group(0);
if (versionString == null || versionString.split('_').isEmpty) {
globals.logger.printWarning(_formatJavaVersionWarning(rawVersionOutput));
return null;
}
// Trim away _d+ from versions 1.8 and below.
return versionString.split('_').first;
}

/// First try Java bundled with Android Studio, then sniff JAVA_HOME, then fallback to PATH.
static String? findJavaBinary({
required AndroidStudio? androidStudio,
Expand All @@ -417,36 +470,64 @@ class AndroidSdk {
required Platform platform,
}) {
if (androidStudio?.javaPath != null) {
globals.printTrace("Using Android Studio's java.");
return fileSystem.path.join(androidStudio!.javaPath!, 'bin', 'java');
}

final String? javaHomeEnv = platform.environment[_javaHomeEnvironmentVariable];
final String? javaHomeEnv =
platform.environment[_javaHomeEnvironmentVariable];
if (javaHomeEnv != null) {
// Trust JAVA_HOME.
globals.printTrace('Using JAVA_HOME.');
return fileSystem.path.join(javaHomeEnv, 'bin', 'java');
}

// MacOS specific logic to avoid popping up a dialog window.
// See: http://stackoverflow.com/questions/14292698/how-do-i-check-if-the-java-jdk-is-installed-on-mac.
if (platform.isMacOS) {
try {
final String javaHomeOutput = globals.processUtils.runSync(
<String>['/usr/libexec/java_home', '-v', '1.8'],
throwOnError: true,
hideStdout: true,
).stdout.trim();
// -v Filter versions (as if JAVA_VERSION had been set in the environment).
// It is unlikley that filtering to java version 1.8 is the right
// decision here. That said, trying this on a mac shows the same jdk
// path no matter what input is passed.
final String javaHomeOutput = globals.processUtils
.runSync(
<String>['/usr/libexec/java_home', '-v', '1.8'],
throwOnError: true,
hideStdout: true,
)
.stdout
.trim();
if (javaHomeOutput.isNotEmpty) {
final String javaHome = javaHomeOutput.split('\n').last.trim();
globals.printTrace('Using mac JAVA_HOME.');
return fileSystem.path.join(javaHome, 'bin', 'java');
}
} on Exception { /* ignore */ }
} on Exception {/* ignore */}
}

// Fallback to PATH based lookup.
return operatingSystemUtils.which(_javaExecutable)?.path;
final String? pathJava = operatingSystemUtils.which(_javaExecutable)?.path;
if (pathJava != null) {
globals.printTrace('Using path java.');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tiny nit ("path java" took me a few seconds to parse&understand than "java from PATH")

Suggested change
globals.printTrace('Using path java.');
globals.printTrace('Using java from PATH.');

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good readability suggestion. Fixed.

} else {
globals.printTrace('Could not find java path.');
}
return pathJava;
}

// Returns a user visible String that says the tool failed to parse
// the version of java along with the output.
static String _formatJavaVersionWarning(String javaVersionRaw) {
return 'Could not parse java version from: \n'
'$javaVersionRaw \n'
'If there is a version please look for an existing bug '
'https://github.com/flutter/flutter/issues/'
' and if one does not exist file a new issue.';
}

Map<String, String>? _sdkManagerEnv;

/// Returns an environment with the Java folder added to PATH for use in calling
/// Java-based Android SDK commands such as sdkmanager and avdmanager.
Map<String, String> get sdkManagerEnv {
Expand Down
Loading