Flutter doctor should require java 1.8+ (#41989)
This commit is contained in:
committed by
Jonah Williams
parent
7cf362fc31
commit
c5b3b3ac26
@@ -19,6 +19,7 @@ import '../globals.dart';
|
||||
import 'android_sdk.dart';
|
||||
|
||||
const int kAndroidSdkMinVersion = 28;
|
||||
final Version kAndroidJavaMinVersion = Version(1, 8, 0);
|
||||
final Version kAndroidSdkBuildToolsMinVersion = Version(28, 0, 3);
|
||||
|
||||
AndroidWorkflow get androidWorkflow => context.get<AndroidWorkflow>();
|
||||
@@ -57,8 +58,19 @@ class AndroidValidator extends DoctorValidator {
|
||||
String get slowWarning => '${_task ?? 'This'} is taking a long time...';
|
||||
String _task;
|
||||
|
||||
/// Finds the semantic version anywhere in a text.
|
||||
static final RegExp _javaVersionPattern = RegExp(r'(\d+)(\.(\d+)(\.(\d+))?)?');
|
||||
|
||||
/// `java -version` response is not only a number, but also includes other
|
||||
/// information eg. `openjdk version "1.7.0_212"`.
|
||||
/// This method extracts only the semantic version from from that response.
|
||||
static String _extractJavaVersion(String text) {
|
||||
final Match match = _javaVersionPattern.firstMatch(text ?? '');
|
||||
return text?.substring(match.start, match.end);
|
||||
}
|
||||
|
||||
/// Returns false if we cannot determine the Java version or if the version
|
||||
/// is not compatible.
|
||||
/// is older that the minimum allowed version of 1.8.
|
||||
Future<bool> _checkJavaVersion(String javaBinary, List<ValidationMessage> messages) async {
|
||||
_task = 'Checking Java status';
|
||||
try {
|
||||
@@ -66,24 +78,28 @@ class AndroidValidator extends DoctorValidator {
|
||||
messages.add(ValidationMessage.error(userMessages.androidCantRunJavaBinary(javaBinary)));
|
||||
return false;
|
||||
}
|
||||
String javaVersion;
|
||||
String javaVersionText;
|
||||
try {
|
||||
printTrace('java -version');
|
||||
final ProcessResult result = await processManager.run(<String>[javaBinary, '-version']);
|
||||
if (result.exitCode == 0) {
|
||||
final List<String> versionLines = result.stderr.split('\n');
|
||||
javaVersion = versionLines.length >= 2 ? versionLines[1] : versionLines[0];
|
||||
javaVersionText = versionLines.length >= 2 ? versionLines[1] : versionLines[0];
|
||||
}
|
||||
} catch (error) {
|
||||
printTrace(error.toString());
|
||||
}
|
||||
if (javaVersion == null) {
|
||||
if (javaVersionText == null || javaVersionText.isEmpty) {
|
||||
// Could not determine the java version.
|
||||
messages.add(ValidationMessage.error(userMessages.androidUnknownJavaVersion));
|
||||
return false;
|
||||
}
|
||||
messages.add(ValidationMessage(userMessages.androidJavaVersion(javaVersion)));
|
||||
// TODO(johnmccutchan): Validate version.
|
||||
final Version javaVersion = Version.parse(_extractJavaVersion(javaVersionText));
|
||||
if (javaVersion < kAndroidJavaMinVersion) {
|
||||
messages.add(ValidationMessage.error(userMessages.androidJavaMinimumVersion(javaVersionText)));
|
||||
return false;
|
||||
}
|
||||
messages.add(ValidationMessage(userMessages.androidJavaVersion(javaVersionText)));
|
||||
return true;
|
||||
} finally {
|
||||
_task = null;
|
||||
|
||||
@@ -49,6 +49,7 @@ class UserMessages {
|
||||
String androidCantRunJavaBinary(String javaBinary) => 'Cannot execute $javaBinary to determine the version';
|
||||
String get androidUnknownJavaVersion => 'Could not determine java version';
|
||||
String androidJavaVersion(String javaVersion) => 'Java version $javaVersion';
|
||||
String androidJavaMinimumVersion(String javaVersion) => 'Java version $javaVersion is older than the minimum recommended version of 1.8';
|
||||
String androidSdkLicenseOnly(String envKey) =>
|
||||
'Android SDK contains licenses only.\n'
|
||||
'Your first build of an Android application will take longer than usual, '
|
||||
|
||||
Reference in New Issue
Block a user