update Android device detection to handle adb error messages (#6521)
diff --git a/packages/flutter_tools/lib/src/android/android_device.dart b/packages/flutter_tools/lib/src/android/android_device.dart
index 02ed8b1..1101ab9 100644
--- a/packages/flutter_tools/lib/src/android/android_device.dart
+++ b/packages/flutter_tools/lib/src/android/android_device.dart
@@ -629,18 +629,24 @@
/// [mockAdbOutput] is public for testing.
List<AndroidDevice> getAdbDevices({ String mockAdbOutput }) {
List<AndroidDevice> devices = <AndroidDevice>[];
- List<String> output;
+ String text;
if (mockAdbOutput == null) {
String adbPath = getAdbPath(androidSdk);
if (adbPath == null)
return <AndroidDevice>[];
- output = runSync(<String>[adbPath, 'devices', '-l']).trim().split('\n');
+ text = runSync(<String>[adbPath, 'devices', '-l']);
} else {
- output = mockAdbOutput.trim().split('\n');
+ text = mockAdbOutput;
}
- for (String line in output) {
+ // Check for error messages from adb
+ if (!text.contains('List of devices')) {
+ printError(text);
+ return <AndroidDevice>[];
+ }
+
+ for (String line in text.trim().split('\n')) {
// Skip lines like: * daemon started successfully *
if (line.startsWith('* daemon '))
continue;
diff --git a/packages/flutter_tools/test/android_device_test.dart b/packages/flutter_tools/test/android_device_test.dart
index 851fb28..da0e012 100644
--- a/packages/flutter_tools/test/android_device_test.dart
+++ b/packages/flutter_tools/test/android_device_test.dart
@@ -3,6 +3,7 @@
// found in the LICENSE file.
import 'package:flutter_tools/src/android/android_device.dart';
+import 'package:flutter_tools/src/base/logger.dart';
import 'package:test/test.dart';
import 'src/context.dart';
@@ -41,11 +42,25 @@
testUsingContext('android n', () {
List<AndroidDevice> devices = getAdbDevices(mockAdbOutput: '''
+List of devices attached
ZX1G22JJWR device usb:3-3 product:shamu model:Nexus_6 device:shamu features:cmd,shell_v2
''');
expect(devices, hasLength(1));
expect(devices.first.name, 'Nexus 6');
});
+
+ BufferLogger logger = new BufferLogger();
+ testUsingContext('adb error message', () {
+ List<AndroidDevice> devices = getAdbDevices(mockAdbOutput: '''
+It appears you do not have 'Android SDK Platform-tools' installed.
+Use the 'android' tool to install them:
+ android update sdk --no-ui --filter 'platform-tools'
+''');
+ expect(devices, hasLength(0));
+ expect(logger.errorText, contains('you do not have'));
+ }, overrides: <Type, dynamic>{
+ Logger : logger,
+ });
});
group('parseAdbDeviceProperties', () {