diff --git a/packages/flutter_tools/lib/src/android/android_device_discovery.dart b/packages/flutter_tools/lib/src/android/android_device_discovery.dart index 8b144d52c6b..ad08368684c 100644 --- a/packages/flutter_tools/lib/src/android/android_device_discovery.dart +++ b/packages/flutter_tools/lib/src/android/android_device_discovery.dart @@ -64,7 +64,7 @@ class AndroidDevices extends PollingDeviceDiscovery { @override Future> pollingGetDevices({ Duration timeout }) async { - if (_androidSdk == null || _androidSdk.adbPath == null) { + if (_doesNotHaveAdb()) { return []; } String text; @@ -88,7 +88,7 @@ class AndroidDevices extends PollingDeviceDiscovery { @override Future> getDiagnostics() async { - if (_androidSdk == null || _androidSdk.adbPath == null) { + if (_doesNotHaveAdb()) { return []; } @@ -104,6 +104,12 @@ class AndroidDevices extends PollingDeviceDiscovery { return diagnostics; } + bool _doesNotHaveAdb() { + return _androidSdk == null || + _androidSdk.adbPath == null || + !_processManager.canRun(_androidSdk.adbPath); + } + // 015d172c98400a03 device usb:340787200X product:nakasi model:Nexus_7 device:grouper static final RegExp _kDeviceRegex = RegExp(r'^(\S+)\s+(\S+)(.*)'); diff --git a/packages/flutter_tools/test/general.shard/android/android_device_discovery_test.dart b/packages/flutter_tools/test/general.shard/android/android_device_discovery_test.dart index 798cfeb3a34..f1a8001ba4f 100644 --- a/packages/flutter_tools/test/general.shard/android/android_device_discovery_test.dart +++ b/packages/flutter_tools/test/general.shard/android/android_device_discovery_test.dart @@ -44,6 +44,25 @@ void main() { expect(await androidDevices.getDiagnostics(), isEmpty); }); + testWithoutContext('AndroidDevices returns empty device list and diagnostics when adb cannot be run', () async { + final AndroidDevices androidDevices = AndroidDevices( + androidSdk: FakeAndroidSdk(null), + logger: BufferLogger.test(), + androidWorkflow: AndroidWorkflow( + androidSdk: FakeAndroidSdk('adb'), + featureFlags: TestFeatureFlags(), + ), + // Will throw an exception if anything other than canRun is invoked + processManager: FakeProcessManger(), + fileSystem: MemoryFileSystem.test(), + platform: FakePlatform(), + userMessages: UserMessages(), + ); + + expect(await androidDevices.pollingGetDevices(), isEmpty); + expect(await androidDevices.getDiagnostics(), isEmpty); + }); + testWithoutContext('AndroidDevices returns empty device list and diagnostics on null Android SDK', () async { final AndroidDevices androidDevices = AndroidDevices( androidSdk: null, @@ -218,3 +237,10 @@ class FakeAndroidSdk extends Fake implements AndroidSdk { @override final String adbPath; } + +class FakeProcessManger extends Fake implements ProcessManager { + @override + bool canRun(dynamic executable, {String workingDirectory}) { + return false; + } +}