Reland "[tool] Move Java functions to their own file" (#126577)
Relands #126086, which was reverted by #126569.
This commit is contained in:
@@ -2,17 +2,13 @@
|
||||
// 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';
|
||||
import '../base/platform.dart';
|
||||
import '../base/process.dart';
|
||||
import '../base/version.dart';
|
||||
import '../convert.dart';
|
||||
import '../globals.dart' as globals;
|
||||
import 'android_studio.dart';
|
||||
import 'java.dart';
|
||||
|
||||
// ANDROID_HOME is deprecated.
|
||||
// See https://developer.android.com/studio/command-line/variables.html#envar
|
||||
@@ -36,16 +32,18 @@ final RegExp _sdkVersionRe = RegExp(r'^ro.build.version.sdk=([0-9]+)$');
|
||||
// $ANDROID_SDK_ROOT/platforms/android-23/android.jar
|
||||
// $ANDROID_SDK_ROOT/platforms/android-N/android.jar
|
||||
class AndroidSdk {
|
||||
AndroidSdk(this.directory) {
|
||||
AndroidSdk(this.directory, {
|
||||
Java? java,
|
||||
}): _java = java {
|
||||
reinitialize();
|
||||
}
|
||||
|
||||
static const String javaHomeEnvironmentVariable = 'JAVA_HOME';
|
||||
static const String _javaExecutable = 'java';
|
||||
|
||||
/// The Android SDK root directory.
|
||||
final Directory directory;
|
||||
|
||||
final Java? _java;
|
||||
|
||||
List<AndroidSdkVersion> _sdkVersions = <AndroidSdkVersion>[];
|
||||
AndroidSdkVersion? _latestVersion;
|
||||
|
||||
@@ -411,162 +409,6 @@ class AndroidSdk {
|
||||
return null;
|
||||
}
|
||||
|
||||
/// Returns the version of java in the format \d(.\d)+(.\d)+
|
||||
/// Returns null if version not found.
|
||||
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.
|
||||
@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) {
|
||||
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;
|
||||
}
|
||||
|
||||
/// A value that would be appropriate to use as JAVA_HOME.
|
||||
///
|
||||
/// This method considers jdk in the following order:
|
||||
/// * the JDK bundled with Android Studio, if one is found;
|
||||
/// * the JAVA_HOME in the ambient environment, if set;
|
||||
String? get javaHome {
|
||||
return findJavaHome(
|
||||
androidStudio: globals.androidStudio,
|
||||
fileSystem: globals.fs,
|
||||
operatingSystemUtils: globals.os,
|
||||
platform: globals.platform,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
static String? findJavaHome({
|
||||
required AndroidStudio? androidStudio,
|
||||
required FileSystem fileSystem,
|
||||
required OperatingSystemUtils operatingSystemUtils,
|
||||
required Platform platform,
|
||||
}) {
|
||||
if (androidStudio?.javaPath != null) {
|
||||
globals.printTrace("Using Android Studio's java.");
|
||||
return androidStudio!.javaPath!;
|
||||
}
|
||||
|
||||
final String? javaHomeEnv = platform.environment[javaHomeEnvironmentVariable];
|
||||
if (javaHomeEnv != null) {
|
||||
globals.printTrace('Using JAVA_HOME from environment valuables.');
|
||||
return javaHomeEnv;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// Finds the java binary that is used for all operations across the tool.
|
||||
///
|
||||
/// This comes from [findJavaHome] if that method returns non-null;
|
||||
/// otherwise, it gets from searching 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,
|
||||
required OperatingSystemUtils operatingSystemUtils,
|
||||
required Platform platform,
|
||||
}) {
|
||||
final String? javaHome = findJavaHome(
|
||||
androidStudio: androidStudio,
|
||||
fileSystem: fileSystem,
|
||||
operatingSystemUtils: operatingSystemUtils,
|
||||
platform: platform,
|
||||
);
|
||||
|
||||
if (javaHome != null) {
|
||||
return fileSystem.path.join(javaHome, 'bin', 'java');
|
||||
}
|
||||
|
||||
// Fallback to PATH based lookup.
|
||||
final String? pathJava = operatingSystemUtils.which(_javaExecutable)?.path;
|
||||
if (pathJava != null) {
|
||||
globals.printTrace('Using java from PATH.');
|
||||
} 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 {
|
||||
if (_sdkManagerEnv == null) {
|
||||
// If we can locate Java, then add it to the path used to run the Android SDK manager.
|
||||
_sdkManagerEnv = <String, String>{};
|
||||
final String? javaBinary = findJavaBinary(
|
||||
androidStudio: globals.androidStudio,
|
||||
fileSystem: globals.fs,
|
||||
operatingSystemUtils: globals.os,
|
||||
platform: globals.platform,
|
||||
);
|
||||
if (javaBinary != null && globals.platform.environment['PATH'] != null) {
|
||||
_sdkManagerEnv!['PATH'] = globals.fs.path.dirname(javaBinary) +
|
||||
globals.os.pathVarSeparator +
|
||||
globals.platform.environment['PATH']!;
|
||||
}
|
||||
}
|
||||
return _sdkManagerEnv!;
|
||||
}
|
||||
|
||||
/// Returns the version of the Android SDK manager tool or null if not found.
|
||||
String? get sdkManagerVersion {
|
||||
if (sdkManagerPath == null || !globals.processManager.canRun(sdkManagerPath)) {
|
||||
@@ -577,7 +419,7 @@ class AndroidSdk {
|
||||
}
|
||||
final RunResult result = globals.processUtils.runSync(
|
||||
<String>[sdkManagerPath!, '--version'],
|
||||
environment: sdkManagerEnv,
|
||||
environment: _java?.environment,
|
||||
);
|
||||
if (result.exitCode != 0) {
|
||||
globals.printTrace('sdkmanager --version failed: exitCode: ${result.exitCode} stdout: ${result.stdout} stderr: ${result.stderr}');
|
||||
|
||||
@@ -11,7 +11,6 @@ import '../base/context.dart';
|
||||
import '../base/file_system.dart';
|
||||
import '../base/io.dart';
|
||||
import '../base/logger.dart';
|
||||
import '../base/os.dart';
|
||||
import '../base/platform.dart';
|
||||
import '../base/user_messages.dart' hide userMessages;
|
||||
import '../base/version.dart';
|
||||
@@ -20,6 +19,7 @@ import '../doctor_validator.dart';
|
||||
import '../features.dart';
|
||||
import 'android_sdk.dart';
|
||||
import 'android_studio.dart';
|
||||
import 'java.dart';
|
||||
|
||||
const int kAndroidSdkMinVersion = 29;
|
||||
final Version kAndroidJavaMinVersion = Version(1, 8, 0);
|
||||
@@ -86,12 +86,6 @@ class AndroidValidator extends DoctorValidator {
|
||||
_androidStudio = androidStudio,
|
||||
_fileSystem = fileSystem,
|
||||
_logger = logger,
|
||||
_operatingSystemUtils = OperatingSystemUtils(
|
||||
fileSystem: fileSystem,
|
||||
logger: logger,
|
||||
platform: platform,
|
||||
processManager: processManager,
|
||||
),
|
||||
_platform = platform,
|
||||
_processManager = processManager,
|
||||
_userMessages = userMessages,
|
||||
@@ -101,7 +95,6 @@ class AndroidValidator extends DoctorValidator {
|
||||
final AndroidStudio? _androidStudio;
|
||||
final FileSystem _fileSystem;
|
||||
final Logger _logger;
|
||||
final OperatingSystemUtils _operatingSystemUtils;
|
||||
final Platform _platform;
|
||||
final ProcessManager _processManager;
|
||||
final UserMessages _userMessages;
|
||||
@@ -138,6 +131,8 @@ class AndroidValidator extends DoctorValidator {
|
||||
}
|
||||
String? javaVersionText;
|
||||
try {
|
||||
// TODO(andrewkolos): Use Java class to find version instead of using duplicate
|
||||
// code. See https://github.com/flutter/flutter/issues/124252.
|
||||
_logger.printTrace('java -version');
|
||||
final ProcessResult result = await _processManager.run(<String>[javaBinary, '-version']);
|
||||
if (result.exitCode == 0) {
|
||||
@@ -240,12 +235,13 @@ class AndroidValidator extends DoctorValidator {
|
||||
|
||||
_task = 'Finding Java binary';
|
||||
// Now check for the JDK.
|
||||
final String? javaBinary = AndroidSdk.findJavaBinary(
|
||||
final String? javaBinary = Java.find(
|
||||
logger: _logger,
|
||||
androidStudio: _androidStudio,
|
||||
fileSystem: _fileSystem,
|
||||
operatingSystemUtils: _operatingSystemUtils,
|
||||
platform: _platform,
|
||||
);
|
||||
processManager: _processManager,
|
||||
)?.binaryPath;
|
||||
if (javaBinary == null) {
|
||||
messages.add(ValidationMessage.error(_userMessages.androidMissingJdk));
|
||||
return ValidationResult(ValidationType.partial, messages, statusInfo: sdkVersionText);
|
||||
@@ -266,18 +262,18 @@ class AndroidValidator extends DoctorValidator {
|
||||
/// SDK have been accepted.
|
||||
class AndroidLicenseValidator extends DoctorValidator {
|
||||
AndroidLicenseValidator({
|
||||
required Java? java,
|
||||
required AndroidSdk? androidSdk,
|
||||
required Platform platform,
|
||||
required OperatingSystemUtils operatingSystemUtils,
|
||||
required FileSystem fileSystem,
|
||||
required ProcessManager processManager,
|
||||
required Logger logger,
|
||||
required AndroidStudio? androidStudio,
|
||||
required Stdio stdio,
|
||||
required UserMessages userMessages,
|
||||
}) : _androidSdk = androidSdk,
|
||||
}) : _java = java,
|
||||
_androidSdk = androidSdk,
|
||||
_platform = platform,
|
||||
_operatingSystemUtils = operatingSystemUtils,
|
||||
_fileSystem = fileSystem,
|
||||
_processManager = processManager,
|
||||
_logger = logger,
|
||||
@@ -286,10 +282,10 @@ class AndroidLicenseValidator extends DoctorValidator {
|
||||
_userMessages = userMessages,
|
||||
super('Android license subvalidator');
|
||||
|
||||
final Java? _java;
|
||||
final AndroidSdk? _androidSdk;
|
||||
final AndroidStudio? _androidStudio;
|
||||
final Stdio _stdio;
|
||||
final OperatingSystemUtils _operatingSystemUtils;
|
||||
final Platform _platform;
|
||||
final FileSystem _fileSystem;
|
||||
final ProcessManager _processManager;
|
||||
@@ -330,12 +326,13 @@ class AndroidLicenseValidator extends DoctorValidator {
|
||||
}
|
||||
|
||||
Future<bool> _checkJavaVersionNoOutput() async {
|
||||
final String? javaBinary = AndroidSdk.findJavaBinary(
|
||||
final String? javaBinary = Java.find(
|
||||
logger: _logger,
|
||||
androidStudio: _androidStudio,
|
||||
fileSystem: _fileSystem,
|
||||
operatingSystemUtils: _operatingSystemUtils,
|
||||
platform: _platform,
|
||||
);
|
||||
processManager: _processManager,
|
||||
)?.binaryPath;
|
||||
if (javaBinary == null) {
|
||||
return false;
|
||||
}
|
||||
@@ -387,7 +384,7 @@ class AndroidLicenseValidator extends DoctorValidator {
|
||||
try {
|
||||
final Process process = await _processManager.start(
|
||||
<String>[_androidSdk!.sdkManagerPath!, '--licenses'],
|
||||
environment: _androidSdk!.sdkManagerEnv,
|
||||
environment: _java?.environment,
|
||||
);
|
||||
process.stdin.write('n\n');
|
||||
// We expect logcat streams to occasionally contain invalid utf-8,
|
||||
@@ -427,7 +424,7 @@ class AndroidLicenseValidator extends DoctorValidator {
|
||||
try {
|
||||
final Process process = await _processManager.start(
|
||||
<String>[_androidSdk!.sdkManagerPath!, '--licenses'],
|
||||
environment: _androidSdk!.sdkManagerEnv,
|
||||
environment: _java?.environment,
|
||||
);
|
||||
|
||||
// The real stdin will never finish streaming. Pipe until the child process
|
||||
|
||||
@@ -147,8 +147,7 @@ class AndroidGradleBuilder implements AndroidBuilder {
|
||||
_gradleUtils = gradleUtils,
|
||||
_androidStudio = androidStudio,
|
||||
_fileSystemUtils = FileSystemUtils(fileSystem: fileSystem, platform: platform),
|
||||
_processUtils = ProcessUtils(logger: logger, processManager: processManager),
|
||||
_platform = platform;
|
||||
_processUtils = ProcessUtils(logger: logger, processManager: processManager);
|
||||
|
||||
final Logger _logger;
|
||||
final ProcessUtils _processUtils;
|
||||
@@ -158,7 +157,6 @@ class AndroidGradleBuilder implements AndroidBuilder {
|
||||
final GradleUtils _gradleUtils;
|
||||
final FileSystemUtils _fileSystemUtils;
|
||||
final AndroidStudio? _androidStudio;
|
||||
final Platform _platform;
|
||||
|
||||
/// Builds the AAR and POM files for the current Flutter module or plugin.
|
||||
@override
|
||||
@@ -268,11 +266,7 @@ class AndroidGradleBuilder implements AndroidBuilder {
|
||||
AndroidStudioJavaGradleConflictMigration(_logger,
|
||||
project: project.android,
|
||||
androidStudio: _androidStudio,
|
||||
fileSystem: _fileSystem,
|
||||
processUtils: _processUtils,
|
||||
platform: _platform,
|
||||
os: globals.os,
|
||||
androidSdk: globals.androidSdk)
|
||||
java: globals.java)
|
||||
,
|
||||
];
|
||||
|
||||
@@ -428,7 +422,7 @@ class AndroidGradleBuilder implements AndroidBuilder {
|
||||
..start();
|
||||
int exitCode = 1;
|
||||
try {
|
||||
final String? javaHome = globals.androidSdk?.javaHome;
|
||||
final String? javaHome = globals.java?.javaHome;
|
||||
exitCode = await _processUtils.stream(
|
||||
command,
|
||||
workingDirectory: project.android.hostAppGradleRoot.path,
|
||||
@@ -698,7 +692,7 @@ class AndroidGradleBuilder implements AndroidBuilder {
|
||||
..start();
|
||||
RunResult result;
|
||||
try {
|
||||
final String? javaHome = globals.androidSdk?.javaHome;
|
||||
final String? javaHome = globals.java?.javaHome;
|
||||
result = await _processUtils.run(
|
||||
command,
|
||||
workingDirectory: project.android.hostAppGradleRoot.path,
|
||||
@@ -751,7 +745,7 @@ class AndroidGradleBuilder implements AndroidBuilder {
|
||||
..start();
|
||||
RunResult result;
|
||||
try {
|
||||
final String? javaHome = globals.androidSdk?.javaHome;
|
||||
final String? javaHome = globals.java?.javaHome;
|
||||
result = await _processUtils.run(
|
||||
command,
|
||||
workingDirectory: project.android.hostAppGradleRoot.path,
|
||||
|
||||
230
packages/flutter_tools/lib/src/android/java.dart
Normal file
230
packages/flutter_tools/lib/src/android/java.dart
Normal file
@@ -0,0 +1,230 @@
|
||||
// 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:process/process.dart';
|
||||
|
||||
import '../base/file_system.dart';
|
||||
import '../base/logger.dart';
|
||||
import '../base/os.dart';
|
||||
import '../base/platform.dart';
|
||||
import '../base/process.dart';
|
||||
import 'android_studio.dart';
|
||||
|
||||
const String _javaHomeEnvironmentVariable = 'JAVA_HOME';
|
||||
const String _kJavaExecutable = 'java';
|
||||
|
||||
/// Represents an installation of Java.
|
||||
class Java {
|
||||
Java({
|
||||
required this.javaHome,
|
||||
required this.binaryPath,
|
||||
required Logger logger,
|
||||
required FileSystem fileSystem,
|
||||
required OperatingSystemUtils os,
|
||||
required Platform platform,
|
||||
required ProcessManager processManager,
|
||||
}): _logger = logger,
|
||||
_fileSystem = fileSystem,
|
||||
_os = os,
|
||||
_platform = platform,
|
||||
_processManager = processManager,
|
||||
_processUtils = ProcessUtils(processManager: processManager, logger: logger);
|
||||
|
||||
/// Finds the Java runtime environment that should be used for all java-dependent
|
||||
/// operations across the tool.
|
||||
///
|
||||
/// This searches for Java in the following places, in order:
|
||||
///
|
||||
/// 1. the runtime environment bundled with Android Studio;
|
||||
/// 2. the runtime environment found in the JAVA_HOME env variable, if set; or
|
||||
/// 3. the java binary found on PATH.
|
||||
///
|
||||
/// Returns null if no java binary could be found.
|
||||
// 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 Java? find({
|
||||
required AndroidStudio? androidStudio,
|
||||
required Logger logger,
|
||||
required FileSystem fileSystem,
|
||||
required Platform platform,
|
||||
required ProcessManager processManager,
|
||||
}) {
|
||||
final OperatingSystemUtils os = OperatingSystemUtils(
|
||||
fileSystem: fileSystem,
|
||||
logger: logger,
|
||||
platform: platform,
|
||||
processManager: processManager
|
||||
);
|
||||
final String? home = _findJavaHome(
|
||||
logger: logger,
|
||||
androidStudio: androidStudio,
|
||||
platform: platform
|
||||
);
|
||||
final String? binary = _findJavaBinary(
|
||||
logger: logger,
|
||||
javaHome: home,
|
||||
fileSystem: fileSystem,
|
||||
operatingSystemUtils: os,
|
||||
platform: platform
|
||||
);
|
||||
|
||||
if (binary == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return Java(
|
||||
javaHome: home,
|
||||
binaryPath: binary,
|
||||
logger: logger,
|
||||
fileSystem: fileSystem,
|
||||
os: os,
|
||||
platform: platform,
|
||||
processManager: processManager,
|
||||
);
|
||||
}
|
||||
|
||||
/// The path of the runtime's home directory.
|
||||
///
|
||||
/// This should only be used for logging and validation purposes.
|
||||
/// If you need to set JAVA_HOME when starting a process, consider
|
||||
/// using [environment] instead.
|
||||
/// If you need to inspect the files of the runtime, considering adding
|
||||
/// a new method to this class instead.
|
||||
final String? javaHome;
|
||||
|
||||
/// The path of the runtime's java binary.
|
||||
///
|
||||
/// This should be only used for logging and validation purposes.
|
||||
/// If you need to invoke the binary directly, consider adding a new method
|
||||
/// to this class instead.
|
||||
final String binaryPath;
|
||||
|
||||
final Logger _logger;
|
||||
final FileSystem _fileSystem;
|
||||
final OperatingSystemUtils _os;
|
||||
final Platform _platform;
|
||||
final ProcessManager _processManager;
|
||||
final ProcessUtils _processUtils;
|
||||
|
||||
/// Returns an environment variable map with
|
||||
/// 1. JAVA_HOME set if this object has a known home directory, and
|
||||
/// 2. The java binary folder appended onto PATH, if the binary location is known.
|
||||
///
|
||||
/// This map should be used as the environment when invoking any Java-dependent
|
||||
/// processes, such as Gradle or Android SDK tools (avdmanager, sdkmanager, etc.)
|
||||
Map<String, String> get environment {
|
||||
return <String, String>{
|
||||
if (javaHome != null) _javaHomeEnvironmentVariable: javaHome!,
|
||||
'PATH': _fileSystem.path.dirname(binaryPath) +
|
||||
_os.pathVarSeparator +
|
||||
_platform.environment['PATH']!,
|
||||
};
|
||||
}
|
||||
|
||||
/// Returns the version of java in the format \d(.\d)+(.\d)+
|
||||
/// Returns null if version could not be determined.
|
||||
late final JavaVersion? version = (() {
|
||||
final RunResult result = _processUtils.runSync(
|
||||
<String>[binaryPath, '--version'],
|
||||
environment: environment,
|
||||
);
|
||||
if (result.exitCode != 0) {
|
||||
_logger.printTrace('java --version failed: exitCode: ${result.exitCode}'
|
||||
' stdout: ${result.stdout} stderr: ${result.stderr}');
|
||||
}
|
||||
return JavaVersion.tryParseFromJavaOutput(result.stdout, logger: _logger);
|
||||
})();
|
||||
|
||||
bool canRun() {
|
||||
return _processManager.canRun(binaryPath);
|
||||
}
|
||||
}
|
||||
|
||||
String? _findJavaHome({
|
||||
required Logger logger,
|
||||
required AndroidStudio? androidStudio,
|
||||
required Platform platform,
|
||||
}) {
|
||||
final String? androidStudioJavaPath = androidStudio?.javaPath;
|
||||
if (androidStudioJavaPath != null) {
|
||||
return androidStudioJavaPath;
|
||||
}
|
||||
|
||||
final String? javaHomeEnv = platform.environment[_javaHomeEnvironmentVariable];
|
||||
if (javaHomeEnv != null) {
|
||||
return javaHomeEnv;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
String? _findJavaBinary({
|
||||
required Logger logger,
|
||||
required String? javaHome,
|
||||
required FileSystem fileSystem,
|
||||
required OperatingSystemUtils operatingSystemUtils,
|
||||
required Platform platform,
|
||||
}) {
|
||||
if (javaHome != null) {
|
||||
return fileSystem.path.join(javaHome, 'bin', 'java');
|
||||
}
|
||||
|
||||
// Fallback to PATH based lookup.
|
||||
return operatingSystemUtils.which(_kJavaExecutable)?.path;
|
||||
}
|
||||
|
||||
// Returns a user visible String that says the tool failed to parse
|
||||
// the version of java along with the output.
|
||||
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.';
|
||||
}
|
||||
|
||||
class JavaVersion {
|
||||
JavaVersion({
|
||||
required this.longText,
|
||||
required this.number
|
||||
});
|
||||
|
||||
/// Typically the first line of the output from `java --version`.
|
||||
/// For example, `"openjdk 19.0.2 2023-01-17"`.
|
||||
final String longText;
|
||||
|
||||
/// The version number. For example, `"19.0.2."`.
|
||||
final String number;
|
||||
|
||||
/// Extracts JDK version from the output of java --version.
|
||||
static JavaVersion? tryParseFromJavaOutput(String rawVersionOutput, {
|
||||
required Logger logger,
|
||||
}) {
|
||||
final List<String> versionLines = rawVersionOutput.split('\n');
|
||||
final String longText = versionLines.length >= 2 ? versionLines[1] : versionLines[0];
|
||||
|
||||
// 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) {
|
||||
logger.printWarning(_formatJavaVersionWarning(rawVersionOutput));
|
||||
return null;
|
||||
}
|
||||
final String? rawShortText = matches.first.group(0);
|
||||
if (rawShortText == null || rawShortText.split('_').isEmpty) {
|
||||
logger.printWarning(_formatJavaVersionWarning(rawVersionOutput));
|
||||
return null;
|
||||
}
|
||||
|
||||
// Trim away _d+ from versions 1.8 and below.
|
||||
final String shortText = rawShortText.split('_').first;
|
||||
|
||||
return JavaVersion(longText: longText, number: shortText);
|
||||
}
|
||||
}
|
||||
@@ -5,15 +5,12 @@
|
||||
import 'package:meta/meta.dart';
|
||||
|
||||
import '../../base/file_system.dart';
|
||||
import '../../base/os.dart';
|
||||
import '../../base/platform.dart';
|
||||
import '../../base/process.dart';
|
||||
import '../../base/project_migrator.dart';
|
||||
import '../../base/version.dart';
|
||||
import '../../project.dart';
|
||||
import '../android_sdk.dart';
|
||||
import '../android_studio.dart';
|
||||
import '../gradle_utils.dart';
|
||||
import '../java.dart';
|
||||
|
||||
// Android Studio 2022.2 "Flamingo" is the first to bundle a Java 17 JDK.
|
||||
// Previous versions bundled a Java 11 JDK.
|
||||
@@ -77,25 +74,14 @@ class AndroidStudioJavaGradleConflictMigration extends ProjectMigrator {
|
||||
super.logger,
|
||||
{required AndroidProject project,
|
||||
AndroidStudio? androidStudio,
|
||||
required FileSystem fileSystem,
|
||||
required ProcessUtils processUtils,
|
||||
required Platform platform,
|
||||
required OperatingSystemUtils os,
|
||||
AndroidSdk? androidSdk,
|
||||
required Java? java,
|
||||
}) : _gradleWrapperPropertiesFile = getGradleWrapperFile(project.hostAppGradleRoot),
|
||||
_androidStudio = androidStudio,
|
||||
_fileSystem = fileSystem,
|
||||
_processUtils = processUtils,
|
||||
_platform = platform,
|
||||
_os = os,
|
||||
_androidSdk = androidSdk;
|
||||
_java = java;
|
||||
|
||||
final File _gradleWrapperPropertiesFile;
|
||||
final AndroidStudio? _androidStudio;
|
||||
final FileSystem _fileSystem;
|
||||
final ProcessUtils _processUtils;
|
||||
final Platform _platform;
|
||||
final OperatingSystemUtils _os;
|
||||
final AndroidSdk? _androidSdk;
|
||||
final Java? _java;
|
||||
|
||||
@override
|
||||
void migrate() {
|
||||
@@ -113,13 +99,7 @@ class AndroidStudioJavaGradleConflictMigration extends ProjectMigrator {
|
||||
return;
|
||||
}
|
||||
|
||||
final String? javaVersionString = _androidSdk?.getJavaVersion(
|
||||
androidStudio: _androidStudio,
|
||||
fileSystem: _fileSystem,
|
||||
operatingSystemUtils: _os,
|
||||
platform: _platform,
|
||||
processUtils: _processUtils,
|
||||
);
|
||||
final String? javaVersionString = _java?.version?.number;
|
||||
final Version? javaVersion = Version.parse(javaVersionString);
|
||||
if (javaVersion == null) {
|
||||
logger.printTrace(javaVersionNotFound);
|
||||
|
||||
@@ -1360,6 +1360,7 @@ class EmulatorDomain extends Domain {
|
||||
EmulatorManager emulators = EmulatorManager(
|
||||
fileSystem: globals.fs,
|
||||
logger: globals.logger,
|
||||
java: globals.java,
|
||||
androidSdk: globals.androidSdk,
|
||||
processManager: globals.processManager,
|
||||
androidWorkflow: androidWorkflow!,
|
||||
|
||||
@@ -14,6 +14,7 @@ import 'android/android_studio.dart';
|
||||
import 'android/android_workflow.dart';
|
||||
import 'android/gradle.dart';
|
||||
import 'android/gradle_utils.dart';
|
||||
import 'android/java.dart';
|
||||
import 'application_package.dart';
|
||||
import 'artifacts.dart';
|
||||
import 'asset.dart';
|
||||
@@ -97,11 +98,11 @@ Future<T> runInContext<T>(
|
||||
androidStudio: globals.androidStudio,
|
||||
),
|
||||
AndroidLicenseValidator: () => AndroidLicenseValidator(
|
||||
operatingSystemUtils: globals.os,
|
||||
platform: globals.platform,
|
||||
userMessages: globals.userMessages,
|
||||
processManager: globals.processManager,
|
||||
androidStudio: globals.androidStudio,
|
||||
java: globals.java,
|
||||
androidSdk: globals.androidSdk,
|
||||
logger: globals.logger,
|
||||
fileSystem: globals.fs,
|
||||
@@ -216,6 +217,7 @@ Future<T> runInContext<T>(
|
||||
Doctor: () => Doctor(logger: globals.logger),
|
||||
DoctorValidatorsProvider: () => DoctorValidatorsProvider.defaultInstance,
|
||||
EmulatorManager: () => EmulatorManager(
|
||||
java: globals.java,
|
||||
androidSdk: globals.androidSdk,
|
||||
processManager: globals.processManager,
|
||||
logger: globals.logger,
|
||||
@@ -253,6 +255,13 @@ Future<T> runInContext<T>(
|
||||
xcode: globals.xcode!,
|
||||
platform: globals.platform,
|
||||
),
|
||||
Java: () => Java.find(
|
||||
androidStudio: globals.androidStudio,
|
||||
logger: globals.logger,
|
||||
fileSystem: globals.fs,
|
||||
platform: globals.platform,
|
||||
processManager: globals.processManager
|
||||
),
|
||||
LocalEngineLocator: () => LocalEngineLocator(
|
||||
userMessages: userMessages,
|
||||
logger: globals.logger,
|
||||
|
||||
@@ -10,6 +10,7 @@ import 'package:process/process.dart';
|
||||
import 'android/android_emulator.dart';
|
||||
import 'android/android_sdk.dart';
|
||||
import 'android/android_workflow.dart';
|
||||
import 'android/java.dart';
|
||||
import 'base/context.dart';
|
||||
import 'base/file_system.dart';
|
||||
import 'base/logger.dart';
|
||||
@@ -22,12 +23,14 @@ EmulatorManager? get emulatorManager => context.get<EmulatorManager>();
|
||||
/// A class to get all available emulators.
|
||||
class EmulatorManager {
|
||||
EmulatorManager({
|
||||
required Java? java,
|
||||
AndroidSdk? androidSdk,
|
||||
required Logger logger,
|
||||
required ProcessManager processManager,
|
||||
required AndroidWorkflow androidWorkflow,
|
||||
required FileSystem fileSystem,
|
||||
}) : _androidSdk = androidSdk,
|
||||
}) : _java = java,
|
||||
_androidSdk = androidSdk,
|
||||
_processUtils = ProcessUtils(logger: logger, processManager: processManager),
|
||||
_androidEmulators = AndroidEmulators(
|
||||
androidSdk: androidSdk,
|
||||
@@ -39,6 +42,7 @@ class EmulatorManager {
|
||||
_emulatorDiscoverers.add(_androidEmulators);
|
||||
}
|
||||
|
||||
final Java? _java;
|
||||
final AndroidSdk? _androidSdk;
|
||||
final AndroidEmulators _androidEmulators;
|
||||
final ProcessUtils _processUtils;
|
||||
@@ -152,7 +156,7 @@ class EmulatorManager {
|
||||
'-n', emulatorName,
|
||||
'-k', sdkId,
|
||||
'-d', device,
|
||||
], environment: _androidSdk?.sdkManagerEnv,
|
||||
], environment: _java?.environment,
|
||||
);
|
||||
return CreateEmulatorResult(
|
||||
emulatorName,
|
||||
@@ -175,7 +179,7 @@ class EmulatorManager {
|
||||
'-c',
|
||||
];
|
||||
final RunResult runResult = await _processUtils.run(args,
|
||||
environment: _androidSdk?.sdkManagerEnv);
|
||||
environment: _java?.environment);
|
||||
if (runResult.exitCode != 0) {
|
||||
return null;
|
||||
}
|
||||
@@ -205,7 +209,7 @@ class EmulatorManager {
|
||||
'-n', 'temp',
|
||||
];
|
||||
final RunResult runResult = await _processUtils.run(args,
|
||||
environment: _androidSdk?.sdkManagerEnv);
|
||||
environment: _java?.environment);
|
||||
|
||||
// Get the list of IDs that match our criteria
|
||||
final List<String> availableIDs = runResult.stderr
|
||||
|
||||
@@ -9,6 +9,7 @@ import 'package:unified_analytics/unified_analytics.dart';
|
||||
import 'android/android_sdk.dart';
|
||||
import 'android/android_studio.dart';
|
||||
import 'android/gradle_utils.dart';
|
||||
import 'android/java.dart';
|
||||
import 'artifacts.dart';
|
||||
import 'base/bot_detector.dart';
|
||||
import 'base/config.dart';
|
||||
@@ -308,3 +309,9 @@ final RegExp kVMServiceMessageRegExp = RegExp(r'The Dart VM service is listening
|
||||
// The official tool no longer allows non-null safe builds. This can be
|
||||
// overridden in other clients.
|
||||
NonNullSafeBuilds get nonNullSafeBuilds => context.get<NonNullSafeBuilds>() ?? NonNullSafeBuilds.notAllowed;
|
||||
|
||||
/// Contains information about the JRE/JDK to use for Java-dependent operations.
|
||||
///
|
||||
/// A value of [null] indicates that no installation of java could be found on
|
||||
/// the host machine.
|
||||
Java? get java => context.get<Java>();
|
||||
|
||||
@@ -568,13 +568,7 @@ class AndroidProject extends FlutterProjectPlatform {
|
||||
hostAppGradleRoot, globals.logger, globals.processManager);
|
||||
final String? agpVersion =
|
||||
gradle.getAgpVersion(hostAppGradleRoot, globals.logger);
|
||||
final String? javaVersion = globals.androidSdk?.getJavaVersion(
|
||||
androidStudio: globals.androidStudio,
|
||||
fileSystem: globals.fs,
|
||||
operatingSystemUtils: globals.os,
|
||||
platform: globals.platform,
|
||||
processUtils: globals.processUtils,
|
||||
);
|
||||
final String? javaVersion = globals.java?.version?.number;
|
||||
|
||||
// Assume valid configuration.
|
||||
String description = validJavaGradleAgpString;
|
||||
|
||||
Reference in New Issue
Block a user