Normalize Java SDK (JDK) location logic across the tool (#124233)

Normalize Java SDK (JDK) location logic across the tool
This commit is contained in:
Andrew Kolos
2023-04-05 13:36:50 -07:00
committed by GitHub
parent 855b180e12
commit ec13058a1e
3 changed files with 130 additions and 24 deletions

View File

@@ -462,7 +462,17 @@ class AndroidSdk {
return versionString.split('_').first;
}
/// Finds the java binary that is used for all operations across the tool.
///
/// First try Java bundled with Android Studio, then sniff JAVA_HOME, then fallback to PATH.
///
// TODO(andrewkolos): To prevent confusion when debugging Android-related
// issues (see https://github.com/flutter/flutter/issues/122609 for an example),
// this logic should be consistently followed by any Java-dependent operation
// across the the tool (building Android apps, interacting with the Android SDK, etc.).
// Currently, this consistency is fragile since the logic used for building
// Android apps exists independently of this method.
// See https://github.com/flutter/flutter/issues/124252.
static String? findJavaBinary({
required AndroidStudio? androidStudio,
required FileSystem fileSystem,
@@ -482,30 +492,6 @@ class AndroidSdk {
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 {
// -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 */}
}
// Fallback to PATH based lookup.
final String? pathJava = operatingSystemUtils.which(_javaExecutable)?.path;
if (pathJava != null) {

View File

@@ -27,6 +27,16 @@ import 'android_studio_validator.dart';
final RegExp _dotHomeStudioVersionMatcher =
RegExp(r'^\.?(AndroidStudio[^\d]*)([\d.]+)');
// TODO(andrewkolos): this global variable is used in several places to provide
// a java binary to multiple Java-dependent tools, including the Android SDK
// and Gradle. If this is null, these tools will implicitly fall back to current
// JAVA_HOME env variable and then to any java found on PATH.
//
// This logic is consistent with that used by flutter doctor to find a valid JDK,
// but this consistency is fragile--the implementations of this logic
// exist independently of each other.
//
// See https://github.com/flutter/flutter/issues/124252.
String? get javaPath => globals.androidStudio?.javaPath;
class AndroidStudio implements Comparable<AndroidStudio> {