From c587d73ec65da16a7360bfb67c15aca6d707e5d6 Mon Sep 17 00:00:00 2001 From: Jenn Magder Date: Mon, 4 May 2020 17:48:10 -0700 Subject: [PATCH] Use androidSdk globals variable everywhere (#56330) --- .../lib/src/android/android_builder.dart | 7 ++--- .../lib/src/android/android_device.dart | 12 ++++---- .../lib/src/android/android_emulator.dart | 4 +-- .../lib/src/android/android_sdk.dart | 3 -- .../lib/src/android/android_workflow.dart | 30 +++++++++---------- .../flutter_tools/lib/src/android/gradle.dart | 8 ++--- .../lib/src/android/gradle_utils.dart | 12 ++++---- .../lib/src/application_package.dart | 5 ++-- .../lib/src/commands/build_aar.dart | 4 +-- .../lib/src/commands/build_apk.dart | 3 +- .../lib/src/commands/build_appbundle.dart | 4 +-- .../lib/src/commands/config.dart | 5 ++-- packages/flutter_tools/lib/src/emulator.dart | 12 ++++---- .../android/android_emulator_test.dart | 5 ++-- 14 files changed, 52 insertions(+), 62 deletions(-) diff --git a/packages/flutter_tools/lib/src/android/android_builder.dart b/packages/flutter_tools/lib/src/android/android_builder.dart index 40ddbd6563..5d64cb48ec 100644 --- a/packages/flutter_tools/lib/src/android/android_builder.dart +++ b/packages/flutter_tools/lib/src/android/android_builder.dart @@ -12,7 +12,6 @@ import '../base/file_system.dart'; import '../build_info.dart'; import '../globals.dart' as globals; import '../project.dart'; -import 'android_sdk.dart'; import 'gradle.dart'; /// The builder in the current context. @@ -87,7 +86,7 @@ class _AndroidBuilderImpl extends AndroidBuilder { buildNumber: buildNumber, ); } finally { - androidSdk?.reinitialize(); + globals.androidSdk?.reinitialize(); } } @@ -107,7 +106,7 @@ class _AndroidBuilderImpl extends AndroidBuilder { localGradleErrors: gradleErrors, ); } finally { - androidSdk?.reinitialize(); + globals.androidSdk?.reinitialize(); } } @@ -127,7 +126,7 @@ class _AndroidBuilderImpl extends AndroidBuilder { localGradleErrors: gradleErrors, ); } finally { - androidSdk?.reinitialize(); + globals.androidSdk?.reinitialize(); } } } diff --git a/packages/flutter_tools/lib/src/android/android_device.dart b/packages/flutter_tools/lib/src/android/android_device.dart index 46b52c1014..84671cbbea 100644 --- a/packages/flutter_tools/lib/src/android/android_device.dart +++ b/packages/flutter_tools/lib/src/android/android_device.dart @@ -217,7 +217,7 @@ class AndroidDevice extends Device { _AndroidDevicePortForwarder _portForwarder; List adbCommandForDevice(List args) { - return [getAdbPath(androidSdk), '-s', id, ...args]; + return [getAdbPath(globals.androidSdk), '-s', id, ...args]; } String runAdbCheckedSync( @@ -274,19 +274,19 @@ class AndroidDevice extends Device { } Future _checkForSupportedAdbVersion() async { - if (androidSdk == null) { + if (globals.androidSdk == null) { return false; } try { final RunResult adbVersion = await processUtils.run( - [getAdbPath(androidSdk), 'version'], + [getAdbPath(globals.androidSdk), 'version'], throwOnError: true, ); if (_isValidAdbVersion(adbVersion.stdout)) { return true; } - globals.printError('The ADB at "${getAdbPath(androidSdk)}" is too old; please install version 1.0.39 or later.'); + globals.printError('The ADB at "${getAdbPath(globals.androidSdk)}" is too old; please install version 1.0.39 or later.'); } on Exception catch (error, trace) { globals.printError('Error running ADB: $error', stackTrace: trace); } @@ -301,7 +301,7 @@ class AndroidDevice extends Device { // adb server is out of date. killing.. // * daemon started successfully * await processUtils.run( - [getAdbPath(androidSdk), 'start-server'], + [getAdbPath(globals.androidSdk), 'start-server'], throwOnError: true, ); @@ -507,7 +507,7 @@ class AndroidDevice extends Device { return LaunchResult.failed(); } - if (!prebuiltApplication || androidSdk.licensesAvailable && androidSdk.latestVersion == null) { + if (!prebuiltApplication || globals.androidSdk.licensesAvailable && globals.androidSdk.latestVersion == null) { globals.printTrace('Building APK'); final FlutterProject project = FlutterProject.current(); await androidBuilder.buildApk( diff --git a/packages/flutter_tools/lib/src/android/android_emulator.dart b/packages/flutter_tools/lib/src/android/android_emulator.dart index 0aeb57d58f..c9777b7fb5 100644 --- a/packages/flutter_tools/lib/src/android/android_emulator.dart +++ b/packages/flutter_tools/lib/src/android/android_emulator.dart @@ -55,7 +55,7 @@ class AndroidEmulator extends Emulator { @override Future launch() async { final Process process = await processUtils.start( - [getEmulatorPath(androidSdk), '-avd', id], + [getEmulatorPath(globals.androidSdk), '-avd', id], ); // Record output from the emulator process. @@ -112,7 +112,7 @@ class AndroidEmulator extends Emulator { /// Return the list of available emulator AVDs. List getEmulatorAvds() { - final String emulatorPath = getEmulatorPath(androidSdk); + final String emulatorPath = getEmulatorPath(globals.androidSdk); if (emulatorPath == null) { return []; } diff --git a/packages/flutter_tools/lib/src/android/android_sdk.dart b/packages/flutter_tools/lib/src/android/android_sdk.dart index e749763a93..90065eb5e8 100644 --- a/packages/flutter_tools/lib/src/android/android_sdk.dart +++ b/packages/flutter_tools/lib/src/android/android_sdk.dart @@ -6,7 +6,6 @@ import 'package:meta/meta.dart'; import 'package:platform/platform.dart'; import '../base/common.dart'; -import '../base/context.dart'; import '../base/file_system.dart'; import '../base/os.dart'; import '../base/process.dart'; @@ -15,8 +14,6 @@ import '../convert.dart'; import '../globals.dart' as globals; import 'android_studio.dart'; -AndroidSdk get androidSdk => context.get(); - const String kAndroidHome = 'ANDROID_HOME'; const String kAndroidSdkRoot = 'ANDROID_SDK_ROOT'; diff --git a/packages/flutter_tools/lib/src/android/android_workflow.dart b/packages/flutter_tools/lib/src/android/android_workflow.dart index 209b19768a..8202377092 100644 --- a/packages/flutter_tools/lib/src/android/android_workflow.dart +++ b/packages/flutter_tools/lib/src/android/android_workflow.dart @@ -48,13 +48,13 @@ class AndroidWorkflow implements Workflow { bool get appliesToHostPlatform => true; @override - bool get canListDevices => getAdbPath(androidSdk) != null; + bool get canListDevices => getAdbPath(globals.androidSdk) != null; @override - bool get canLaunchDevices => androidSdk != null && androidSdk.validateSdkWellFormed().isEmpty; + bool get canLaunchDevices => globals.androidSdk != null && globals.androidSdk.validateSdkWellFormed().isEmpty; @override - bool get canListEmulators => getEmulatorPath(androidSdk) != null; + bool get canListEmulators => getEmulatorPath(globals.androidSdk) != null; } class AndroidValidator extends DoctorValidator { @@ -242,13 +242,13 @@ class AndroidLicenseValidator extends DoctorValidator { final List messages = []; // Match pre-existing early termination behavior - if (androidSdk == null || androidSdk.latestVersion == null || - androidSdk.validateSdkWellFormed().isNotEmpty || + if (globals.androidSdk == null || globals.androidSdk.latestVersion == null || + globals.androidSdk.validateSdkWellFormed().isNotEmpty || ! await _checkJavaVersionNoOutput()) { return ValidationResult(ValidationType.missing, messages); } - final String sdkVersionText = userMessages.androidStatusInfo(androidSdk.latestVersion.buildToolsVersionName); + final String sdkVersionText = userMessages.androidStatusInfo(globals.androidSdk.latestVersion.buildToolsVersionName); // Check for licenses. switch (await licensesAccepted) { @@ -325,8 +325,8 @@ class AndroidLicenseValidator extends DoctorValidator { try { final Process process = await processUtils.start( - [androidSdk.sdkManagerPath, '--licenses'], - environment: androidSdk.sdkManagerEnv, + [globals.androidSdk.sdkManagerPath, '--licenses'], + environment: globals.androidSdk.sdkManagerEnv, ); process.stdin.write('n\n'); // We expect logcat streams to occasionally contain invalid utf-8, @@ -351,19 +351,19 @@ class AndroidLicenseValidator extends DoctorValidator { /// Run the Android SDK manager tool in order to accept SDK licenses. static Future runLicenseManager() async { - if (androidSdk == null) { + if (globals.androidSdk == null) { globals.printStatus(userMessages.androidSdkShort); return false; } if (!_canRunSdkManager()) { - throwToolExit(userMessages.androidMissingSdkManager(androidSdk.sdkManagerPath, globals.platform)); + throwToolExit(userMessages.androidMissingSdkManager(globals.androidSdk.sdkManagerPath, globals.platform)); } try { final Process process = await processUtils.start( - [androidSdk.sdkManagerPath, '--licenses'], - environment: androidSdk.sdkManagerEnv, + [globals.androidSdk.sdkManagerPath, '--licenses'], + environment: globals.androidSdk.sdkManagerEnv, ); // The real stdin will never finish streaming. Pipe until the child process @@ -393,7 +393,7 @@ class AndroidLicenseValidator extends DoctorValidator { return exitCode == 0; } on ProcessException catch (e) { throwToolExit(userMessages.androidCannotRunSdkManager( - androidSdk.sdkManagerPath, + globals.androidSdk.sdkManagerPath, e.toString(), globals.platform, )); @@ -402,8 +402,8 @@ class AndroidLicenseValidator extends DoctorValidator { } static bool _canRunSdkManager() { - assert(androidSdk != null); - final String sdkManagerPath = androidSdk.sdkManagerPath; + assert(globals.androidSdk != null); + final String sdkManagerPath = globals.androidSdk.sdkManagerPath; return globals.processManager.canRun(sdkManagerPath); } } diff --git a/packages/flutter_tools/lib/src/android/gradle.dart b/packages/flutter_tools/lib/src/android/gradle.dart index 6dcae8f595..d12a8d869f 100644 --- a/packages/flutter_tools/lib/src/android/gradle.dart +++ b/packages/flutter_tools/lib/src/android/gradle.dart @@ -8,7 +8,6 @@ import 'package:crypto/crypto.dart'; import 'package:meta/meta.dart'; import 'package:xml/xml.dart' as xml; -import '../android/android_sdk.dart'; import '../artifacts.dart'; import '../base/common.dart'; import '../base/file_system.dart'; @@ -23,7 +22,6 @@ import '../flutter_manifest.dart'; import '../globals.dart' as globals; import '../project.dart'; import '../reporting/reporting.dart'; -import 'android_sdk.dart'; import 'gradle_errors.dart'; import 'gradle_utils.dart'; @@ -149,7 +147,7 @@ Future checkGradleDependencies() async { workingDirectory: flutterProject.android.hostAppGradleRoot.path, environment: gradleEnvironment, ); - androidSdk?.reinitialize(); + globals.androidSdk?.reinitialize(); progress.stop(); } @@ -229,7 +227,7 @@ Future buildGradleApp({ assert(target != null); assert(isBuildingBundle != null); assert(localGradleErrors != null); - assert(androidSdk != null); + assert(globals.androidSdk != null); if (!project.android.isUsingGradle) { _exitWithProjectNotUsingGradleMessage(); @@ -520,7 +518,7 @@ Future buildGradleAar({ assert(target != null); assert(androidBuildInfo != null); assert(outputDirectory != null); - assert(androidSdk != null); + assert(globals.androidSdk != null); final FlutterManifest manifest = project.manifest; if (!manifest.isModule && !manifest.isPlugin) { diff --git a/packages/flutter_tools/lib/src/android/gradle_utils.dart b/packages/flutter_tools/lib/src/android/gradle_utils.dart index 7ab8fab946..0190bb0aac 100644 --- a/packages/flutter_tools/lib/src/android/gradle_utils.dart +++ b/packages/flutter_tools/lib/src/android/gradle_utils.dart @@ -4,7 +4,6 @@ import 'package:meta/meta.dart'; -import '../android/android_sdk.dart'; import '../base/common.dart'; import '../base/context.dart'; import '../base/file_system.dart'; @@ -16,7 +15,6 @@ import '../cache.dart'; import '../globals.dart' as globals; import '../project.dart'; import '../reporting/reporting.dart'; -import 'android_sdk.dart'; import 'android_studio.dart'; /// The environment variables needed to run Gradle. @@ -239,7 +237,7 @@ void updateLocalProperties({ BuildInfo buildInfo, bool requireAndroidSdk = true, }) { - if (requireAndroidSdk && androidSdk == null) { + if (requireAndroidSdk && globals.androidSdk == null) { exitWithNoSdkMessage(); } final File localProperties = project.android.localPropertiesFile; @@ -265,8 +263,8 @@ void updateLocalProperties({ changed = true; } - if (androidSdk != null) { - changeIfNecessary('sdk.dir', globals.fsUtils.escapePath(androidSdk.directory)); + if (globals.androidSdk != null) { + changeIfNecessary('sdk.dir', globals.fsUtils.escapePath(globals.androidSdk.directory)); } changeIfNecessary('flutter.sdk', globals.fsUtils.escapePath(Cache.flutterRoot)); @@ -294,8 +292,8 @@ void updateLocalProperties({ /// Writes the path to the Android SDK, if known. void writeLocalProperties(File properties) { final SettingsFile settings = SettingsFile(); - if (androidSdk != null) { - settings.values['sdk.dir'] = globals.fsUtils.escapePath(androidSdk.directory); + if (globals.androidSdk != null) { + settings.values['sdk.dir'] = globals.fsUtils.escapePath(globals.androidSdk.directory); } settings.writeContents(properties); } diff --git a/packages/flutter_tools/lib/src/application_package.dart b/packages/flutter_tools/lib/src/application_package.dart index cd4fd1c1cd..0b00d6bcce 100644 --- a/packages/flutter_tools/lib/src/application_package.dart +++ b/packages/flutter_tools/lib/src/application_package.dart @@ -8,7 +8,6 @@ import 'dart:collection'; import 'package:meta/meta.dart'; import 'package:xml/xml.dart' as xml; -import 'android/android_sdk.dart'; import 'android/gradle.dart'; import 'base/common.dart'; import 'base/context.dart'; @@ -40,7 +39,7 @@ class ApplicationPackageFactory { case TargetPlatform.android_arm64: case TargetPlatform.android_x64: case TargetPlatform.android_x86: - if (androidSdk?.licensesAvailable == true && androidSdk?.latestVersion == null) { + if (globals.androidSdk?.licensesAvailable == true && globals.androidSdk?.latestVersion == null) { await checkGradleDependencies(); } return applicationBinary == null @@ -109,7 +108,7 @@ class AndroidApk extends ApplicationPackage { /// Creates a new AndroidApk from an existing APK. factory AndroidApk.fromApk(File apk) { - final String aaptPath = androidSdk?.latestVersion?.aaptPath; + final String aaptPath = globals.androidSdk?.latestVersion?.aaptPath; if (aaptPath == null) { globals.printError(userMessages.aaptNotFound); return null; diff --git a/packages/flutter_tools/lib/src/commands/build_aar.dart b/packages/flutter_tools/lib/src/commands/build_aar.dart index 7742e22ea8..9c7551a4c5 100644 --- a/packages/flutter_tools/lib/src/commands/build_aar.dart +++ b/packages/flutter_tools/lib/src/commands/build_aar.dart @@ -5,12 +5,12 @@ import 'dart:async'; import '../android/android_builder.dart'; -import '../android/android_sdk.dart'; import '../android/gradle_utils.dart'; import '../base/common.dart'; import '../base/os.dart'; import '../build_info.dart'; import '../cache.dart'; +import '../globals.dart' as globals; import '../project.dart'; import '../reporting/reporting.dart'; import '../runner/flutter_command.dart' show FlutterCommandResult; @@ -88,7 +88,7 @@ class BuildAarCommand extends BuildSubCommand { @override Future runCommand() async { - if (androidSdk == null) { + if (globals.androidSdk == null) { exitWithNoSdkMessage(); } final Set androidBuildInfo = {}; diff --git a/packages/flutter_tools/lib/src/commands/build_apk.dart b/packages/flutter_tools/lib/src/commands/build_apk.dart index a6d111603c..2023dfcd60 100644 --- a/packages/flutter_tools/lib/src/commands/build_apk.dart +++ b/packages/flutter_tools/lib/src/commands/build_apk.dart @@ -5,7 +5,6 @@ import 'dart:async'; import '../android/android_builder.dart'; -import '../android/android_sdk.dart'; import '../android/gradle_utils.dart'; import '../base/terminal.dart'; import '../build_info.dart'; @@ -86,7 +85,7 @@ class BuildApkCommand extends BuildSubCommand { @override Future runCommand() async { - if (androidSdk == null) { + if (globals.androidSdk == null) { exitWithNoSdkMessage(); } final BuildInfo buildInfo = getBuildInfo(); diff --git a/packages/flutter_tools/lib/src/commands/build_appbundle.dart b/packages/flutter_tools/lib/src/commands/build_appbundle.dart index 2514324e7f..71fb474f4e 100644 --- a/packages/flutter_tools/lib/src/commands/build_appbundle.dart +++ b/packages/flutter_tools/lib/src/commands/build_appbundle.dart @@ -5,10 +5,10 @@ import 'dart:async'; import '../android/android_builder.dart'; -import '../android/android_sdk.dart'; import '../android/gradle_utils.dart'; import '../build_info.dart'; import '../cache.dart'; +import '../globals.dart' as globals; import '../project.dart'; import '../reporting/reporting.dart'; import '../runner/flutter_command.dart' show FlutterCommandResult; @@ -76,7 +76,7 @@ class BuildAppBundleCommand extends BuildSubCommand { @override Future runCommand() async { - if (androidSdk == null) { + if (globals.androidSdk == null) { exitWithNoSdkMessage(); } final AndroidBuildInfo androidBuildInfo = AndroidBuildInfo(getBuildInfo(), diff --git a/packages/flutter_tools/lib/src/commands/config.dart b/packages/flutter_tools/lib/src/commands/config.dart index 6435fec5d3..639454ff5a 100644 --- a/packages/flutter_tools/lib/src/commands/config.dart +++ b/packages/flutter_tools/lib/src/commands/config.dart @@ -4,7 +4,6 @@ import 'dart:async'; -import '../android/android_sdk.dart'; import '../android/android_studio.dart'; import '../base/common.dart'; import '../convert.dart'; @@ -172,8 +171,8 @@ class ConfigCommand extends FlutterCommand { if (results['android-studio-dir'] == null && androidStudio != null) { results['android-studio-dir'] = androidStudio.directory; } - if (results['android-sdk'] == null && androidSdk != null) { - results['android-sdk'] = androidSdk.directory; + if (results['android-sdk'] == null && globals.androidSdk != null) { + results['android-sdk'] = globals.androidSdk.directory; } globals.printStatus(const JsonEncoder.withIndent(' ').convert(results)); diff --git a/packages/flutter_tools/lib/src/emulator.dart b/packages/flutter_tools/lib/src/emulator.dart index a4bea2127c..f325aefa67 100644 --- a/packages/flutter_tools/lib/src/emulator.dart +++ b/packages/flutter_tools/lib/src/emulator.dart @@ -115,7 +115,7 @@ class EmulatorManager { } final List args = [ - getAvdManagerPath(androidSdk), + getAvdManagerPath(globals.androidSdk), 'create', 'avd', '-n', name, @@ -123,7 +123,7 @@ class EmulatorManager { '-d', device, ]; final RunResult runResult = processUtils.runSync(args, - environment: androidSdk?.sdkManagerEnv); + environment: globals.androidSdk?.sdkManagerEnv); return CreateEmulatorResult( name, success: runResult.exitCode == 0, @@ -138,13 +138,13 @@ class EmulatorManager { ]; Future _getPreferredAvailableDevice() async { final List args = [ - getAvdManagerPath(androidSdk), + getAvdManagerPath(globals.androidSdk), 'list', 'device', '-c', ]; final RunResult runResult = processUtils.runSync(args, - environment: androidSdk?.sdkManagerEnv); + environment: globals.androidSdk?.sdkManagerEnv); if (runResult.exitCode != 0) { return null; } @@ -165,13 +165,13 @@ class EmulatorManager { // It seems that to get the available list of images, we need to send a // request to create without the image and it'll provide us a list :-( final List args = [ - getAvdManagerPath(androidSdk), + getAvdManagerPath(globals.androidSdk), 'create', 'avd', '-n', 'temp', ]; final RunResult runResult = processUtils.runSync(args, - environment: androidSdk?.sdkManagerEnv); + environment: globals.androidSdk?.sdkManagerEnv); // Get the list of IDs that match our criteria final List availableIDs = runResult.stderr diff --git a/packages/flutter_tools/test/general.shard/android/android_emulator_test.dart b/packages/flutter_tools/test/general.shard/android/android_emulator_test.dart index 406fc39209..883065425c 100644 --- a/packages/flutter_tools/test/general.shard/android/android_emulator_test.dart +++ b/packages/flutter_tools/test/general.shard/android/android_emulator_test.dart @@ -7,10 +7,11 @@ import 'dart:async'; import 'package:file/file.dart'; import 'package:file/memory.dart'; import 'package:flutter_tools/src/android/android_sdk.dart' - show getEmulatorPath, AndroidSdk, androidSdk; + show getEmulatorPath, AndroidSdk; import 'package:flutter_tools/src/android/android_emulator.dart'; import 'package:flutter_tools/src/base/common.dart'; import 'package:flutter_tools/src/device.dart'; +import 'package:flutter_tools/src/globals.dart' as globals; import 'package:mockito/mockito.dart'; import 'package:quiver/testing/async.dart'; @@ -130,7 +131,7 @@ void main() { testUsingContext('succeeds', () async { final AndroidEmulator emulator = AndroidEmulator(emulatorID); - expect(getEmulatorPath(androidSdk), mockSdk.emulatorPath); + expect(getEmulatorPath(globals.androidSdk), mockSdk.emulatorPath); final Completer completer = Completer(); FakeAsync().run((FakeAsync time) { unawaited(emulator.launch().whenComplete(completer.complete));