Unverified Commit b083bc14 authored by Jenn Magder's avatar Jenn Magder Committed by GitHub

Migrate android_sdk and application_package to null safety (#79611)

parent 75453e6a
...@@ -2,10 +2,6 @@ ...@@ -2,10 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
// @dart = 2.8
import 'package:meta/meta.dart';
import '../base/common.dart'; import '../base/common.dart';
import '../base/file_system.dart'; import '../base/file_system.dart';
import '../base/os.dart'; import '../base/os.dart';
...@@ -48,8 +44,8 @@ class AndroidSdk { ...@@ -48,8 +44,8 @@ class AndroidSdk {
/// The Android SDK root directory. /// The Android SDK root directory.
final Directory directory; final Directory directory;
List<AndroidSdkVersion> _sdkVersions; List<AndroidSdkVersion> _sdkVersions = <AndroidSdkVersion>[];
AndroidSdkVersion _latestVersion; AndroidSdkVersion? _latestVersion;
/// Whether the `platform-tools` or `cmdline-tools` directory exists in the Android SDK. /// Whether the `platform-tools` or `cmdline-tools` directory exists in the Android SDK.
/// ///
...@@ -69,9 +65,9 @@ class AndroidSdk { ...@@ -69,9 +65,9 @@ class AndroidSdk {
/// the SDK on demand. /// the SDK on demand.
bool get licensesAvailable => directory.childDirectory('licenses').existsSync(); bool get licensesAvailable => directory.childDirectory('licenses').existsSync();
static AndroidSdk locateAndroidSdk() { static AndroidSdk? locateAndroidSdk() {
String findAndroidHomeDir() { String? findAndroidHomeDir() {
String androidHomeDir; String? androidHomeDir;
if (globals.config.containsKey('android-sdk')) { if (globals.config.containsKey('android-sdk')) {
androidHomeDir = globals.config.getValue('android-sdk') as String; androidHomeDir = globals.config.getValue('android-sdk') as String;
} else if (globals.platform.environment.containsKey(kAndroidHome)) { } else if (globals.platform.environment.containsKey(kAndroidHome)) {
...@@ -81,7 +77,7 @@ class AndroidSdk { ...@@ -81,7 +77,7 @@ class AndroidSdk {
} else if (globals.platform.isLinux) { } else if (globals.platform.isLinux) {
if (globals.fsUtils.homeDirPath != null) { if (globals.fsUtils.homeDirPath != null) {
androidHomeDir = globals.fs.path.join( androidHomeDir = globals.fs.path.join(
globals.fsUtils.homeDirPath, globals.fsUtils.homeDirPath!,
'Android', 'Android',
'Sdk', 'Sdk',
); );
...@@ -89,7 +85,7 @@ class AndroidSdk { ...@@ -89,7 +85,7 @@ class AndroidSdk {
} else if (globals.platform.isMacOS) { } else if (globals.platform.isMacOS) {
if (globals.fsUtils.homeDirPath != null) { if (globals.fsUtils.homeDirPath != null) {
androidHomeDir = globals.fs.path.join( androidHomeDir = globals.fs.path.join(
globals.fsUtils.homeDirPath, globals.fsUtils.homeDirPath!,
'Library', 'Library',
'Android', 'Android',
'sdk', 'sdk',
...@@ -98,7 +94,7 @@ class AndroidSdk { ...@@ -98,7 +94,7 @@ class AndroidSdk {
} else if (globals.platform.isWindows) { } else if (globals.platform.isWindows) {
if (globals.fsUtils.homeDirPath != null) { if (globals.fsUtils.homeDirPath != null) {
androidHomeDir = globals.fs.path.join( androidHomeDir = globals.fs.path.join(
globals.fsUtils.homeDirPath, globals.fsUtils.homeDirPath!,
'AppData', 'AppData',
'Local', 'Local',
'Android', 'Android',
...@@ -141,7 +137,7 @@ class AndroidSdk { ...@@ -141,7 +137,7 @@ class AndroidSdk {
return null; return null;
} }
final String androidHomeDir = findAndroidHomeDir(); final String? androidHomeDir = findAndroidHomeDir();
if (androidHomeDir == null) { if (androidHomeDir == null) {
// No dice. // No dice.
globals.printTrace('Unable to locate an Android SDK.'); globals.printTrace('Unable to locate an Android SDK.');
...@@ -165,26 +161,29 @@ class AndroidSdk { ...@@ -165,26 +161,29 @@ class AndroidSdk {
List<AndroidSdkVersion> get sdkVersions => _sdkVersions; List<AndroidSdkVersion> get sdkVersions => _sdkVersions;
AndroidSdkVersion get latestVersion => _latestVersion; AndroidSdkVersion? get latestVersion => _latestVersion;
String get adbPath => _adbPath ??= getPlatformToolsPath(globals.platform.isWindows ? 'adb.exe' : 'adb'); String? get adbPath => _adbPath ??= getPlatformToolsPath(globals.platform.isWindows ? 'adb.exe' : 'adb');
String _adbPath; String? _adbPath;
String get emulatorPath => getEmulatorPath(); String? get emulatorPath => getEmulatorPath();
String get avdManagerPath => getAvdManagerPath(); String? get avdManagerPath => getAvdManagerPath();
/// Locate the path for storing AVD emulator images. Returns null if none found. /// Locate the path for storing AVD emulator images. Returns null if none found.
String getAvdPath() { String? getAvdPath() {
final String? avdHome = globals.platform.environment['ANDROID_AVD_HOME'];
final String? home = globals.platform.environment['HOME'];
final List<String> searchPaths = <String>[ final List<String> searchPaths = <String>[
globals.platform.environment['ANDROID_AVD_HOME'], if (avdHome != null)
if (globals.platform.environment['HOME'] != null) avdHome,
globals.fs.path.join(globals.platform.environment['HOME'], '.android', 'avd'), if (home != null)
globals.fs.path.join(home, '.android', 'avd'),
]; ];
if (globals.platform.isWindows) { if (globals.platform.isWindows) {
final String homeDrive = globals.platform.environment['HOMEDRIVE']; final String? homeDrive = globals.platform.environment['HOMEDRIVE'];
final String homePath = globals.platform.environment['HOMEPATH']; final String? homePath = globals.platform.environment['HOMEPATH'];
if (homeDrive != null && homePath != null) { if (homeDrive != null && homePath != null) {
// Can't use path.join for HOMEDRIVE/HOMEPATH // Can't use path.join for HOMEDRIVE/HOMEPATH
...@@ -194,10 +193,12 @@ class AndroidSdk { ...@@ -194,10 +193,12 @@ class AndroidSdk {
} }
} }
return searchPaths.where((String p) => p != null).firstWhere( for (final String searchPath in searchPaths.whereType<String>()) {
(String p) => globals.fs.directory(p).existsSync(), if (globals.fs.directory(searchPath).existsSync()) {
orElse: () => null, return searchPath;
); }
}
return null;
} }
Directory get _platformsDir => directory.childDirectory('platforms'); Directory get _platformsDir => directory.childDirectory('platforms');
...@@ -232,10 +233,10 @@ class AndroidSdk { ...@@ -232,10 +233,10 @@ class AndroidSdk {
return <String>[msg.toString()]; return <String>[msg.toString()];
} }
return latestVersion.validateSdkWellFormed(); return latestVersion!.validateSdkWellFormed();
} }
String getPlatformToolsPath(String binaryName) { String? getPlatformToolsPath(String binaryName) {
final File cmdlineToolsBinary = directory.childDirectory('cmdline-tools').childFile(binaryName); final File cmdlineToolsBinary = directory.childDirectory('cmdline-tools').childFile(binaryName);
if (cmdlineToolsBinary.existsSync()) { if (cmdlineToolsBinary.existsSync()) {
return cmdlineToolsBinary.path; return cmdlineToolsBinary.path;
...@@ -247,7 +248,7 @@ class AndroidSdk { ...@@ -247,7 +248,7 @@ class AndroidSdk {
return null; return null;
} }
String getEmulatorPath() { String? getEmulatorPath() {
final String binaryName = globals.platform.isWindows ? 'emulator.exe' : 'emulator'; final String binaryName = globals.platform.isWindows ? 'emulator.exe' : 'emulator';
// Emulator now lives inside "emulator" but used to live inside "tools" so // Emulator now lives inside "emulator" but used to live inside "tools" so
// try both. // try both.
...@@ -261,7 +262,7 @@ class AndroidSdk { ...@@ -261,7 +262,7 @@ class AndroidSdk {
return null; return null;
} }
String getCmdlineToolsPath(String binaryName) { String? getCmdlineToolsPath(String binaryName) {
// First look for the latest version of the command-line tools // First look for the latest version of the command-line tools
final File cmdlineToolsLatestBinary = directory final File cmdlineToolsLatestBinary = directory
.childDirectory('cmdline-tools') .childDirectory('cmdline-tools')
...@@ -285,7 +286,7 @@ class AndroidSdk { ...@@ -285,7 +286,7 @@ class AndroidSdk {
return null; return null;
} }
}) })
.where((Version version) => version != null) .whereType<Version>()
.toList(); .toList();
cmdlineTools.sort(); cmdlineTools.sort();
...@@ -310,7 +311,7 @@ class AndroidSdk { ...@@ -310,7 +311,7 @@ class AndroidSdk {
return null; return null;
} }
String getAvdManagerPath() => getCmdlineToolsPath(globals.platform.isWindows ? 'avdmanager.bat' : 'avdmanager'); String? getAvdManagerPath() => getCmdlineToolsPath(globals.platform.isWindows ? 'avdmanager.bat' : 'avdmanager');
/// Sets up various paths used internally. /// Sets up various paths used internally.
/// ///
...@@ -330,33 +331,37 @@ class AndroidSdk { ...@@ -330,33 +331,37 @@ class AndroidSdk {
return null; return null;
} }
}) })
.where((Version version) => version != null) .whereType<Version>()
.toList(); .toList();
} }
// Match up platforms with the best corresponding build-tools. // Match up platforms with the best corresponding build-tools.
_sdkVersions = _platforms.map<AndroidSdkVersion>((Directory platformDir) { _sdkVersions = _platforms.map<AndroidSdkVersion?>((Directory platformDir) {
final String platformName = platformDir.basename; final String platformName = platformDir.basename;
int platformVersion; int platformVersion;
try { try {
final Match numberedVersion = _numberedAndroidPlatformRe.firstMatch(platformName); final Match? numberedVersion = _numberedAndroidPlatformRe.firstMatch(platformName);
if (numberedVersion != null) { if (numberedVersion != null) {
platformVersion = int.parse(numberedVersion.group(1)); platformVersion = int.parse(numberedVersion.group(1)!);
} else { } else {
final String buildProps = platformDir.childFile('build.prop').readAsStringSync(); final String buildProps = platformDir.childFile('build.prop').readAsStringSync();
final String versionString = const LineSplitter() final String? versionString = const LineSplitter()
.convert(buildProps) .convert(buildProps)
.map<Match>(_sdkVersionRe.firstMatch) .map<RegExpMatch?>(_sdkVersionRe.firstMatch)
.firstWhere((Match match) => match != null) .whereType<Match>()
.first
.group(1); .group(1);
if (versionString == null) {
return null;
}
platformVersion = int.parse(versionString); platformVersion = int.parse(versionString);
} }
} on Exception { } on Exception {
return null; return null;
} }
Version buildToolsVersion = Version.primary(buildTools.where((Version version) { Version? buildToolsVersion = Version.primary(buildTools.where((Version version) {
return version.major == platformVersion; return version.major == platformVersion;
}).toList()); }).toList());
...@@ -373,7 +378,7 @@ class AndroidSdk { ...@@ -373,7 +378,7 @@ class AndroidSdk {
buildToolsVersion: buildToolsVersion, buildToolsVersion: buildToolsVersion,
fileSystem: globals.fs, fileSystem: globals.fs,
); );
}).where((AndroidSdkVersion version) => version != null).toList(); }).whereType<AndroidSdkVersion>().toList();
_sdkVersions.sort(); _sdkVersions.sort();
...@@ -388,7 +393,7 @@ class AndroidSdk { ...@@ -388,7 +393,7 @@ class AndroidSdk {
final String executable = globals.platform.isWindows final String executable = globals.platform.isWindows
? 'sdkmanager.bat' ? 'sdkmanager.bat'
: 'sdkmanager'; : 'sdkmanager';
final String path = getCmdlineToolsPath(executable); final String? path = getCmdlineToolsPath(executable);
if (path != null) { if (path != null) {
return path; return path;
} }
...@@ -401,17 +406,17 @@ class AndroidSdk { ...@@ -401,17 +406,17 @@ class AndroidSdk {
} }
/// First try Java bundled with Android Studio, then sniff JAVA_HOME, then fallback to PATH. /// First try Java bundled with Android Studio, then sniff JAVA_HOME, then fallback to PATH.
static String findJavaBinary({ static String? findJavaBinary({
@required AndroidStudio androidStudio, required AndroidStudio? androidStudio,
@required FileSystem fileSystem, required FileSystem fileSystem,
@required OperatingSystemUtils operatingSystemUtils, required OperatingSystemUtils operatingSystemUtils,
@required Platform platform, required Platform platform,
}) { }) {
if (androidStudio?.javaPath != null) { if (androidStudio?.javaPath != null) {
return fileSystem.path.join(androidStudio.javaPath, 'bin', 'java'); return fileSystem.path.join(androidStudio!.javaPath!, 'bin', 'java');
} }
final String javaHomeEnv = platform.environment[_javaHomeEnvironmentVariable]; final String? javaHomeEnv = platform.environment[_javaHomeEnvironmentVariable];
if (javaHomeEnv != null) { if (javaHomeEnv != null) {
// Trust JAVA_HOME. // Trust JAVA_HOME.
return fileSystem.path.join(javaHomeEnv, 'bin', 'java'); return fileSystem.path.join(javaHomeEnv, 'bin', 'java');
...@@ -439,30 +444,30 @@ class AndroidSdk { ...@@ -439,30 +444,30 @@ class AndroidSdk {
return operatingSystemUtils.which(_javaExecutable)?.path; return operatingSystemUtils.which(_javaExecutable)?.path;
} }
Map<String, String> _sdkManagerEnv; Map<String, String>? _sdkManagerEnv;
/// Returns an environment with the Java folder added to PATH for use in calling /// Returns an environment with the Java folder added to PATH for use in calling
/// Java-based Android SDK commands such as sdkmanager and avdmanager. /// Java-based Android SDK commands such as sdkmanager and avdmanager.
Map<String, String> get sdkManagerEnv { Map<String, String> get sdkManagerEnv {
if (_sdkManagerEnv == null) { if (_sdkManagerEnv == null) {
// If we can locate Java, then add it to the path used to run the Android SDK manager. // If we can locate Java, then add it to the path used to run the Android SDK manager.
_sdkManagerEnv = <String, String>{}; _sdkManagerEnv = <String, String>{};
final String javaBinary = findJavaBinary( final String? javaBinary = findJavaBinary(
androidStudio: globals.androidStudio, androidStudio: globals.androidStudio,
fileSystem: globals.fs, fileSystem: globals.fs,
operatingSystemUtils: globals.os, operatingSystemUtils: globals.os,
platform: globals.platform, platform: globals.platform,
); );
if (javaBinary != null) { if (javaBinary != null && globals.platform.environment['PATH'] != null) {
_sdkManagerEnv['PATH'] = globals.fs.path.dirname(javaBinary) + _sdkManagerEnv!['PATH'] = globals.fs.path.dirname(javaBinary) +
globals.os.pathVarSeparator + globals.os.pathVarSeparator +
globals.platform.environment['PATH']; globals.platform.environment['PATH']!;
} }
} }
return _sdkManagerEnv; return _sdkManagerEnv!;
} }
/// Returns the version of the Android SDK manager tool or null if not found. /// Returns the version of the Android SDK manager tool or null if not found.
String get sdkManagerVersion { String? get sdkManagerVersion {
if (!globals.processManager.canRun(sdkManagerPath)) { if (!globals.processManager.canRun(sdkManagerPath)) {
throwToolExit('Android sdkmanager not found. Update to the latest Android SDK to resolve this.'); throwToolExit('Android sdkmanager not found. Update to the latest Android SDK to resolve this.');
} }
...@@ -484,10 +489,10 @@ class AndroidSdk { ...@@ -484,10 +489,10 @@ class AndroidSdk {
class AndroidSdkVersion implements Comparable<AndroidSdkVersion> { class AndroidSdkVersion implements Comparable<AndroidSdkVersion> {
AndroidSdkVersion._( AndroidSdkVersion._(
this.sdk, { this.sdk, {
@required this.sdkLevel, required this.sdkLevel,
@required this.platformName, required this.platformName,
@required this.buildToolsVersion, required this.buildToolsVersion,
@required FileSystem fileSystem, required FileSystem fileSystem,
}) : assert(sdkLevel != null), }) : assert(sdkLevel != null),
assert(platformName != null), assert(platformName != null),
assert(buildToolsVersion != null), assert(buildToolsVersion != null),
...@@ -513,12 +518,14 @@ class AndroidSdkVersion implements Comparable<AndroidSdkVersion> { ...@@ -513,12 +518,14 @@ class AndroidSdkVersion implements Comparable<AndroidSdkVersion> {
String get aaptPath => getBuildToolsPath('aapt'); String get aaptPath => getBuildToolsPath('aapt');
List<String> validateSdkWellFormed() { List<String> validateSdkWellFormed() {
if (_exists(androidJarPath) != null) { final String? existsAndroidJarPath = _exists(androidJarPath);
return <String>[_exists(androidJarPath)]; if (existsAndroidJarPath != null) {
return <String>[existsAndroidJarPath];
} }
if (_canRun(aaptPath) != null) { final String? canRunAaptPath = _canRun(aaptPath);
return <String>[_canRun(aaptPath)]; if (canRunAaptPath != null) {
return <String>[canRunAaptPath];
} }
return <String>[]; return <String>[];
...@@ -538,14 +545,14 @@ class AndroidSdkVersion implements Comparable<AndroidSdkVersion> { ...@@ -538,14 +545,14 @@ class AndroidSdkVersion implements Comparable<AndroidSdkVersion> {
@override @override
String toString() => '[${sdk.directory}, SDK version $sdkLevel, build-tools $buildToolsVersionName]'; String toString() => '[${sdk.directory}, SDK version $sdkLevel, build-tools $buildToolsVersionName]';
String _exists(String path) { String? _exists(String path) {
if (!_fileSystem.isFileSync(path)) { if (!_fileSystem.isFileSync(path)) {
return 'Android SDK file not found: $path.'; return 'Android SDK file not found: $path.';
} }
return null; return null;
} }
String _canRun(String path) { String? _canRun(String path) {
if (!globals.processManager.canRun(path)) { if (!globals.processManager.canRun(path)) {
return 'Android SDK file not found: $path.'; return 'Android SDK file not found: $path.';
} }
......
...@@ -2,16 +2,12 @@ ...@@ -2,16 +2,12 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
// @dart = 2.8
import 'package:meta/meta.dart';
import 'base/context.dart'; import 'base/context.dart';
import 'base/file_system.dart'; import 'base/file_system.dart';
import 'build_info.dart'; import 'build_info.dart';
abstract class ApplicationPackageFactory { abstract class ApplicationPackageFactory {
static ApplicationPackageFactory get instance => context.get<ApplicationPackageFactory>(); static ApplicationPackageFactory? get instance => context.get<ApplicationPackageFactory>();
/// Create an [ApplicationPackage] for the given platform. /// Create an [ApplicationPackage] for the given platform.
Future<ApplicationPackage> getPackageForPlatform( Future<ApplicationPackage> getPackageForPlatform(
...@@ -22,17 +18,17 @@ abstract class ApplicationPackageFactory { ...@@ -22,17 +18,17 @@ abstract class ApplicationPackageFactory {
} }
abstract class ApplicationPackage { abstract class ApplicationPackage {
ApplicationPackage({ @required this.id }) ApplicationPackage({ required this.id })
: assert(id != null); : assert(id != null);
/// Package ID from the Android Manifest or equivalent. /// Package ID from the Android Manifest or equivalent.
final String id; final String id;
String get name; String? get name;
String get displayName => name; String? get displayName => name;
File get packagesFile => null; File? get packagesFile => null;
@override @override
String toString() => displayName ?? id; String toString() => displayName ?? id;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment