Android license detector in doctor, take two (#14783)
* Revert "Revert "Add android license verification to doctor and some refactoring" (#14727)"
This reverts commit d260294752.
* Add tests, fix sdkManagerEnv and use it consistently, and rearrange Status object model
* AnsiSpinner needs to leave the cursor where it found it.
* fix tests
* Const constructor warning only shows up on windows...?
* Avoid crash if we can't find the home directory
* Make pathVarSeparator return a string in the mock
* Implement review comments
* Fix out-of-order problem on stop
This commit is contained in:
@@ -12,9 +12,11 @@ import '../base/file_system.dart';
|
||||
import '../base/io.dart' show ProcessResult;
|
||||
import '../base/os.dart';
|
||||
import '../base/platform.dart';
|
||||
import '../base/process.dart';
|
||||
import '../base/process_manager.dart';
|
||||
import '../base/version.dart';
|
||||
import '../globals.dart';
|
||||
import 'android_studio.dart' as android_studio;
|
||||
|
||||
AndroidSdk get androidSdk => context[AndroidSdk];
|
||||
|
||||
@@ -63,6 +65,9 @@ class AndroidSdk {
|
||||
_init();
|
||||
}
|
||||
|
||||
static const String _kJavaHomeEnvironmentVariable = 'JAVA_HOME';
|
||||
static const String _kJavaExecutable = 'java';
|
||||
|
||||
/// The path to the Android SDK.
|
||||
final String directory;
|
||||
|
||||
@@ -291,11 +296,56 @@ class AndroidSdk {
|
||||
return fs.path.join(directory, 'tools', 'bin', 'sdkmanager');
|
||||
}
|
||||
|
||||
/// First try Java bundled with Android Studio, then sniff JAVA_HOME, then fallback to PATH.
|
||||
static String findJavaBinary() {
|
||||
|
||||
if (android_studio.javaPath != null)
|
||||
return fs.path.join(android_studio.javaPath, 'bin', 'java');
|
||||
|
||||
final String javaHomeEnv = platform.environment[_kJavaHomeEnvironmentVariable];
|
||||
if (javaHomeEnv != null) {
|
||||
// Trust JAVA_HOME.
|
||||
return fs.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 = runCheckedSync(<String>['/usr/libexec/java_home'], hideStdout: true);
|
||||
if (javaHomeOutput != null) {
|
||||
final List<String> javaHomeOutputSplit = javaHomeOutput.split('\n');
|
||||
if ((javaHomeOutputSplit != null) && (javaHomeOutputSplit.isNotEmpty)) {
|
||||
final String javaHome = javaHomeOutputSplit[0].trim();
|
||||
return fs.path.join(javaHome, 'bin', 'java');
|
||||
}
|
||||
}
|
||||
} catch (_) { /* ignore */ }
|
||||
}
|
||||
|
||||
// Fallback to PATH based lookup.
|
||||
return os.which(_kJavaExecutable)?.path;
|
||||
}
|
||||
|
||||
Map<String, String> _sdkManagerEnv;
|
||||
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();
|
||||
if (javaBinary != null) {
|
||||
_sdkManagerEnv['PATH'] =
|
||||
fs.path.dirname(javaBinary) + os.pathVarSeparator + platform.environment['PATH'];
|
||||
}
|
||||
}
|
||||
return _sdkManagerEnv;
|
||||
}
|
||||
|
||||
/// Returns the version of the Android SDK manager tool or null if not found.
|
||||
String get sdkManagerVersion {
|
||||
if (!processManager.canRun(sdkManagerPath))
|
||||
throwToolExit('Android sdkmanager not found. Update to the latest Android SDK to resolve this.');
|
||||
final ProcessResult result = processManager.runSync(<String>[sdkManagerPath, '--version']);
|
||||
final ProcessResult result = processManager.runSync(<String>[sdkManagerPath, '--version'], environment: sdkManagerEnv);
|
||||
if (result.exitCode != 0) {
|
||||
throwToolExit('sdkmanager --version failed: ${result.exitCode}', exitCode: result.exitCode);
|
||||
}
|
||||
|
||||
@@ -178,14 +178,14 @@ class AndroidStudio implements Comparable<AndroidStudio> {
|
||||
|
||||
// Read all $HOME/.AndroidStudio*/system/.home files. There may be several
|
||||
// pointing to the same installation, so we grab only the latest one.
|
||||
for (FileSystemEntity entity in fs.directory(homeDirPath).listSync()) {
|
||||
if (entity is Directory && entity.basename.startsWith('.AndroidStudio')) {
|
||||
final AndroidStudio studio = new AndroidStudio.fromHomeDot(entity);
|
||||
if (studio != null &&
|
||||
!_hasStudioAt(studio.directory, newerThan: studio.version)) {
|
||||
studios.removeWhere(
|
||||
(AndroidStudio other) => other.directory == studio.directory);
|
||||
studios.add(studio);
|
||||
if (fs.directory(homeDirPath).existsSync()) {
|
||||
for (FileSystemEntity entity in fs.directory(homeDirPath).listSync()) {
|
||||
if (entity is Directory && entity.basename.startsWith('.AndroidStudio')) {
|
||||
final AndroidStudio studio = new AndroidStudio.fromHomeDot(entity);
|
||||
if (studio != null && !_hasStudioAt(studio.directory, newerThan: studio.version)) {
|
||||
studios.removeWhere((AndroidStudio other) => other.directory == studio.directory);
|
||||
studios.add(studio);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,12 +3,11 @@
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
|
||||
import '../base/common.dart';
|
||||
import '../base/context.dart';
|
||||
import '../base/file_system.dart';
|
||||
import '../base/io.dart';
|
||||
import '../base/os.dart';
|
||||
import '../base/platform.dart';
|
||||
import '../base/process.dart';
|
||||
import '../base/process_manager.dart';
|
||||
@@ -17,10 +16,20 @@ import '../base/version.dart';
|
||||
import '../doctor.dart';
|
||||
import '../globals.dart';
|
||||
import 'android_sdk.dart';
|
||||
import 'android_studio.dart' as android_studio;
|
||||
|
||||
AndroidWorkflow get androidWorkflow => context.putIfAbsent(AndroidWorkflow, () => new AndroidWorkflow());
|
||||
|
||||
enum LicensesAccepted {
|
||||
none,
|
||||
some,
|
||||
all,
|
||||
unknown,
|
||||
}
|
||||
|
||||
final RegExp licenseCounts = new RegExp(r'(\d+) of (\d+) SDK package licenses? not accepted.');
|
||||
final RegExp licenseNotAccepted = new RegExp(r'licenses? not accepted', caseSensitive: false);
|
||||
final RegExp licenseAccepted = new RegExp(r'All SDK package licenses accepted.');
|
||||
|
||||
class AndroidWorkflow extends DoctorValidator implements Workflow {
|
||||
AndroidWorkflow() : super('Android toolchain - develop for Android devices');
|
||||
|
||||
@@ -33,41 +42,8 @@ class AndroidWorkflow extends DoctorValidator implements Workflow {
|
||||
@override
|
||||
bool get canLaunchDevices => androidSdk != null && androidSdk.validateSdkWellFormed().isEmpty;
|
||||
|
||||
static const String _kJavaHomeEnvironmentVariable = 'JAVA_HOME';
|
||||
static const String _kJavaExecutable = 'java';
|
||||
static const String _kJdkDownload = 'https://www.oracle.com/technetwork/java/javase/downloads/';
|
||||
|
||||
/// First try Java bundled with Android Studio, then sniff JAVA_HOME, then fallback to PATH.
|
||||
static String _findJavaBinary() {
|
||||
|
||||
if (android_studio.javaPath != null)
|
||||
return fs.path.join(android_studio.javaPath, 'bin', 'java');
|
||||
|
||||
final String javaHomeEnv = platform.environment[_kJavaHomeEnvironmentVariable];
|
||||
if (javaHomeEnv != null) {
|
||||
// Trust JAVA_HOME.
|
||||
return fs.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 = runCheckedSync(<String>['/usr/libexec/java_home'], hideStdout: true);
|
||||
if (javaHomeOutput != null) {
|
||||
final List<String> javaHomeOutputSplit = javaHomeOutput.split('\n');
|
||||
if ((javaHomeOutputSplit != null) && (javaHomeOutputSplit.isNotEmpty)) {
|
||||
final String javaHome = javaHomeOutputSplit[0].trim();
|
||||
return fs.path.join(javaHome, 'bin', 'java');
|
||||
}
|
||||
}
|
||||
} catch (_) { /* ignore */ }
|
||||
}
|
||||
|
||||
// Fallback to PATH based lookup.
|
||||
return os.which(_kJavaExecutable)?.path;
|
||||
}
|
||||
|
||||
/// Returns false if we cannot determine the Java version or if the version
|
||||
/// is not compatible.
|
||||
bool _checkJavaVersion(String javaBinary, List<ValidationMessage> messages) {
|
||||
@@ -154,7 +130,7 @@ class AndroidWorkflow extends DoctorValidator implements Workflow {
|
||||
}
|
||||
|
||||
// Now check for the JDK.
|
||||
final String javaBinary = _findJavaBinary();
|
||||
final String javaBinary = AndroidSdk.findJavaBinary();
|
||||
if (javaBinary == null) {
|
||||
messages.add(new ValidationMessage.error(
|
||||
'No Java Development Kit (JDK) found; You must have the environment '
|
||||
@@ -169,10 +145,59 @@ class AndroidWorkflow extends DoctorValidator implements Workflow {
|
||||
return new ValidationResult(ValidationType.partial, messages, statusInfo: sdkVersionText);
|
||||
}
|
||||
|
||||
// Check for licenses.
|
||||
switch (await licensesAccepted) {
|
||||
case LicensesAccepted.all:
|
||||
messages.add(new ValidationMessage('All Android licenses accepted.'));
|
||||
break;
|
||||
case LicensesAccepted.some:
|
||||
messages.add(new ValidationMessage.hint('Some Android licenses not accepted. To resolve this, run: flutter doctor --android-licenses'));
|
||||
return new ValidationResult(ValidationType.partial, messages, statusInfo: sdkVersionText);
|
||||
case LicensesAccepted.none:
|
||||
messages.add(new ValidationMessage.error('Android licenses not accepted. To resolve this, run: flutter doctor --android-licenses'));
|
||||
return new ValidationResult(ValidationType.partial, messages, statusInfo: sdkVersionText);
|
||||
case LicensesAccepted.unknown:
|
||||
messages.add(new ValidationMessage.error('Android license status unknown.'));
|
||||
return new ValidationResult(ValidationType.partial, messages, statusInfo: sdkVersionText);
|
||||
}
|
||||
|
||||
// Success.
|
||||
return new ValidationResult(ValidationType.installed, messages, statusInfo: sdkVersionText);
|
||||
}
|
||||
|
||||
Future<LicensesAccepted> get licensesAccepted async {
|
||||
LicensesAccepted status = LicensesAccepted.unknown;
|
||||
|
||||
void _onLine(String line) {
|
||||
if (licenseAccepted.hasMatch(line)) {
|
||||
status = LicensesAccepted.all;
|
||||
} else if (licenseCounts.hasMatch(line)) {
|
||||
final Match match = licenseCounts.firstMatch(line);
|
||||
if (match.group(1) != match.group(2)) {
|
||||
status = LicensesAccepted.some;
|
||||
} else {
|
||||
status = LicensesAccepted.none;
|
||||
}
|
||||
} else if (licenseNotAccepted.hasMatch(line)) {
|
||||
// In case the format changes, a more general match will keep doctor
|
||||
// mostly working.
|
||||
status = LicensesAccepted.none;
|
||||
}
|
||||
}
|
||||
|
||||
final Process process = await runCommand(<String>[androidSdk.sdkManagerPath, '--licenses'], environment: androidSdk.sdkManagerEnv);
|
||||
process.stdin.write('n\n');
|
||||
final Future<void> output = process.stdout.transform(const Utf8Decoder(allowMalformed: true)).transform(const LineSplitter()).listen(_onLine).asFuture<void>(null);
|
||||
final Future<void> errors = process.stderr.transform(const Utf8Decoder(allowMalformed: true)).transform(const LineSplitter()).listen(_onLine).asFuture<void>(null);
|
||||
try {
|
||||
await Future.wait<void>(<Future<void>>[output, errors]).timeout(const Duration(seconds: 30));
|
||||
} catch (TimeoutException) {
|
||||
printTrace('Intentionally killing ${androidSdk.sdkManagerPath}');
|
||||
processManager.killPid(process.pid);
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
/// Run the Android SDK manager tool in order to accept SDK licenses.
|
||||
static Future<bool> runLicenseManager() async {
|
||||
if (androidSdk == null) {
|
||||
@@ -180,14 +205,6 @@ class AndroidWorkflow extends DoctorValidator implements Workflow {
|
||||
return false;
|
||||
}
|
||||
|
||||
// If we can locate Java, then add it to the path used to run the Android SDK manager.
|
||||
final Map<String, String> sdkManagerEnv = <String, String>{};
|
||||
final String javaBinary = _findJavaBinary();
|
||||
if (javaBinary != null) {
|
||||
sdkManagerEnv['PATH'] =
|
||||
fs.path.dirname(javaBinary) + os.pathVarSeparator + platform.environment['PATH'];
|
||||
}
|
||||
|
||||
if (!processManager.canRun(androidSdk.sdkManagerPath))
|
||||
throwToolExit(
|
||||
'Android sdkmanager tool not found.\n'
|
||||
@@ -205,7 +222,7 @@ class AndroidWorkflow extends DoctorValidator implements Workflow {
|
||||
|
||||
final Process process = await runCommand(
|
||||
<String>[androidSdk.sdkManagerPath, '--licenses'],
|
||||
environment: sdkManagerEnv,
|
||||
environment: androidSdk.sdkManagerEnv,
|
||||
);
|
||||
|
||||
waitGroup<Null>(<Future<Null>>[
|
||||
|
||||
Reference in New Issue
Block a user