Skip to content

Commit 6794e37

Browse files
reidbakerexaby73
authored andcommitted
Add Java-Gradle-AGP validation to flutter analyze (flutter#123916)
flutter#123917 Doc covering a broad set of issues related to android studio updating. https://docs.google.com/document/d/1hTXkjbUrBnXgu8NQsth1c3aEqo77rWoEj8CcsQ39wwQ/edit?pli=1# Specifically this pr: - Adds new functions to find a projects AGP, Gradle and java versions, and tests. - Adds new functions that take versions and parse if the versions are compatible with each other, and tests. - Adds validator for `flutter analyze --suggestions` that evaluates the java/gradle/agp versions and checks if they are compatible, and integration test. - Updates the version of gradle used by dev/integration_tests/flutter_gallery/ to the minimum supported by java 18 so that the integration tests pass (It is unknown why the java version is 18.9 instead of 11) - Moves `isWithinVersionRange` to version.dart, and tests. - Adds FakeAndroidStudio to fakes to be used in multiple tests but does not remove existing copies. Metrics will be included as part of the definition of done for this bug but not as part of this cl. It is already too big. Known work still left in this pr: * Understand why analyze integration tests are failing. Example output if Java and gradle are not compatible: ``` ┌───────────────────────────────────────────────────────────────────┐ │ General Info │ │ [✓] App Name: espresso_example │ │ [✓] Supported Platforms: android │ │ [✓] Is Flutter Package: yes │ │ [✓] Uses Material Design: yes │ │ [✓] Is Plugin: no │ │ [✗] Java/Gradle/Android Gradle Plugin: │ │ │ │ Incompatible Java/Gradle versions. │ │ │ │ Java Version: 17.0.6, Gradle Version: 7.0.2 │ │ │ │ See the link below for more information. │ │ https://docs.gradle.org/current/userguide/compatibility.html#java │ │ │ └───────────────────────────────────────────────────────────────────┘ ``` Example output if Gradle and AGP are not compatible ``` ┌─────────────────────────────────────────────────────────────────────────────┐ │ General Info │ │ [✓] App Name: espresso_example │ │ [✓] Supported Platforms: android │ │ [✓] Is Flutter Package: yes │ │ [✓] Uses Material Design: yes │ │ [✓] Is Plugin: no │ │ [✗] Java/Gradle/Android Gradle Plugin: Incompatible Gradle/AGP versions. │ │ │ │ Gradle Version: 7.0.2, AGP Version: 7.4.2 │ │ │ │ Update gradle to at least "7.5". │ │ See the link below for more information: │ │ https://developer.android.com/studio/releases/gradle-plugin#updating-gradle │ │ │ │ Incompatible Java/Gradle versions. │ │ │ │ Java Version: 17.0.6, Gradle Version: 7.0.2 │ │ │ │ See the link below for more information: │ │ https://docs.gradle.org/current/userguide/compatibility.html#java │ │ │ └─────────────────────────────────────────────────────────────────────────────┘ ``` Example output if Java/Gradle/Agp are not compatible. ``` ┌─────────────────────────────────────────────────────────────────────────────┐ │ General Info │ │ [✓] App Name: espresso_example │ │ [✓] Supported Platforms: android │ │ [✓] Is Flutter Package: yes │ │ [✓] Uses Material Design: yes │ │ [✓] Is Plugin: no │ │ [✗] Java/Gradle/Android Gradle Plugin: Incompatible Gradle/AGP versions. │ │ │ │ Gradle Version: 7.0.2, AGP Version: 7.4.2 │ │ │ │ Update gradle to at least "7.5". │ │ See the link below for more information: │ │ https://developer.android.com/studio/releases/gradle-plugin#updating-gradle │ │ │ │ Incompatible Java/Gradle versions. │ │ │ │ Java Version: 17.0.6, Gradle Version: 7.0.2 │ │ │ │ See the link below for more information: │ │ https://docs.gradle.org/current/userguide/compatibility.html#java │ │ │ └─────────────────────────────────────────────────────────────────────────────┘ ``` Commit messages - Add function to gradle_utils.dart that gets the gradle version from wrapper or system and add a test for each situation - Add method to get agp version, add method to validate agp against gradle version, update documentation, add tests for agp validation. - Update dart doc for validateGradleAndAgp to describe where the info came from and corner case behavior, create function to validate java and gradle and hardcode return to false - Fill out and test java gradle compatibility function in gradle_utils - Hook up java gradle evaluateion to hasValidJavaGradleAgpVersions with hardcoded java version - Add java --version output parsing and tests - Add getJavaBinary test - Update comment in android_sdk for mac behavior with java_home -v ## 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]. - [ ] All existing and new tests are passing.
1 parent 19b3a9e commit 6794e37

File tree

15 files changed

+1799
-235
lines changed

15 files changed

+1799
-235
lines changed

dev/integration_tests/flutter_gallery/android/gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
33
zipStoreBase=GRADLE_USER_HOME
44
zipStorePath=wrapper/dists
5-
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-all.zip
5+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.1-all.zip

packages/flutter_tools/lib/src/android/android_sdk.dart

Lines changed: 89 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
import 'package:meta/meta.dart';
6+
57
import '../base/common.dart';
68
import '../base/file_system.dart';
79
import '../base/os.dart';
@@ -409,6 +411,57 @@ class AndroidSdk {
409411
return null;
410412
}
411413

414+
/// Returns the version of java in the format \d(.\d)+(.\d)+
415+
/// Returns null if version not found.
416+
String? getJavaVersion({
417+
required AndroidStudio? androidStudio,
418+
required FileSystem fileSystem,
419+
required OperatingSystemUtils operatingSystemUtils,
420+
required Platform platform,
421+
required ProcessUtils processUtils,
422+
}) {
423+
final String? javaBinary = findJavaBinary(
424+
androidStudio: androidStudio,
425+
fileSystem: fileSystem,
426+
operatingSystemUtils: operatingSystemUtils,
427+
platform: platform,
428+
);
429+
if (javaBinary == null) {
430+
globals.printTrace('Could not find java binary to get version.');
431+
return null;
432+
}
433+
final RunResult result = processUtils.runSync(
434+
<String>[javaBinary, '--version'],
435+
environment: sdkManagerEnv,
436+
);
437+
if (result.exitCode != 0) {
438+
globals.printTrace(
439+
'java --version failed: exitCode: ${result.exitCode} stdout: ${result.stdout} stderr: ${result.stderr}');
440+
return null;
441+
}
442+
return parseJavaVersion(result.stdout);
443+
}
444+
445+
/// Extracts JDK version from the output of java --version.
446+
@visibleForTesting
447+
static String? parseJavaVersion(String rawVersionOutput) {
448+
// The contents that matter come in the format '11.0.18' or '1.8.0_202'.
449+
final RegExp jdkVersionRegex = RegExp(r'\d+\.\d+(\.\d+(?:_\d+)?)?');
450+
final Iterable<RegExpMatch> matches =
451+
jdkVersionRegex.allMatches(rawVersionOutput);
452+
if (matches.isEmpty) {
453+
globals.logger.printWarning(_formatJavaVersionWarning(rawVersionOutput));
454+
return null;
455+
}
456+
final String? versionString = matches.first.group(0);
457+
if (versionString == null || versionString.split('_').isEmpty) {
458+
globals.logger.printWarning(_formatJavaVersionWarning(rawVersionOutput));
459+
return null;
460+
}
461+
// Trim away _d+ from versions 1.8 and below.
462+
return versionString.split('_').first;
463+
}
464+
412465
/// First try Java bundled with Android Studio, then sniff JAVA_HOME, then fallback to PATH.
413466
static String? findJavaBinary({
414467
required AndroidStudio? androidStudio,
@@ -417,36 +470,64 @@ class AndroidSdk {
417470
required Platform platform,
418471
}) {
419472
if (androidStudio?.javaPath != null) {
473+
globals.printTrace("Using Android Studio's java.");
420474
return fileSystem.path.join(androidStudio!.javaPath!, 'bin', 'java');
421475
}
422476

423-
final String? javaHomeEnv = platform.environment[_javaHomeEnvironmentVariable];
477+
final String? javaHomeEnv =
478+
platform.environment[_javaHomeEnvironmentVariable];
424479
if (javaHomeEnv != null) {
425480
// Trust JAVA_HOME.
481+
globals.printTrace('Using JAVA_HOME.');
426482
return fileSystem.path.join(javaHomeEnv, 'bin', 'java');
427483
}
428484

429485
// MacOS specific logic to avoid popping up a dialog window.
430486
// See: http://stackoverflow.com/questions/14292698/how-do-i-check-if-the-java-jdk-is-installed-on-mac.
431487
if (platform.isMacOS) {
432488
try {
433-
final String javaHomeOutput = globals.processUtils.runSync(
434-
<String>['/usr/libexec/java_home', '-v', '1.8'],
435-
throwOnError: true,
436-
hideStdout: true,
437-
).stdout.trim();
489+
// -v Filter versions (as if JAVA_VERSION had been set in the environment).
490+
// It is unlikley that filtering to java version 1.8 is the right
491+
// decision here. That said, trying this on a mac shows the same jdk
492+
// path no matter what input is passed.
493+
final String javaHomeOutput = globals.processUtils
494+
.runSync(
495+
<String>['/usr/libexec/java_home', '-v', '1.8'],
496+
throwOnError: true,
497+
hideStdout: true,
498+
)
499+
.stdout
500+
.trim();
438501
if (javaHomeOutput.isNotEmpty) {
439502
final String javaHome = javaHomeOutput.split('\n').last.trim();
503+
globals.printTrace('Using mac JAVA_HOME.');
440504
return fileSystem.path.join(javaHome, 'bin', 'java');
441505
}
442-
} on Exception { /* ignore */ }
506+
} on Exception {/* ignore */}
443507
}
444508

445509
// Fallback to PATH based lookup.
446-
return operatingSystemUtils.which(_javaExecutable)?.path;
510+
final String? pathJava = operatingSystemUtils.which(_javaExecutable)?.path;
511+
if (pathJava != null) {
512+
globals.printTrace('Using java from PATH.');
513+
} else {
514+
globals.printTrace('Could not find java path.');
515+
}
516+
return pathJava;
517+
}
518+
519+
// Returns a user visible String that says the tool failed to parse
520+
// the version of java along with the output.
521+
static String _formatJavaVersionWarning(String javaVersionRaw) {
522+
return 'Could not parse java version from: \n'
523+
'$javaVersionRaw \n'
524+
'If there is a version please look for an existing bug '
525+
'https://github.com/flutter/flutter/issues/'
526+
' and if one does not exist file a new issue.';
447527
}
448528

449529
Map<String, String>? _sdkManagerEnv;
530+
450531
/// Returns an environment with the Java folder added to PATH for use in calling
451532
/// Java-based Android SDK commands such as sdkmanager and avdmanager.
452533
Map<String, String> get sdkManagerEnv {

0 commit comments

Comments
 (0)