blob: 0aeb57d58f963fe0b1021b46b28803ed3811f60a [file] [log] [blame]
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:async';
import 'package:meta/meta.dart';
import '../android/android_sdk.dart';
import '../android/android_workflow.dart';
import '../base/common.dart';
import '../base/file_system.dart';
import '../base/io.dart';
import '../base/process.dart';
import '../base/utils.dart';
import '../convert.dart';
import '../device.dart';
import '../emulator.dart';
import '../globals.dart' as globals;
import 'android_sdk.dart';
class AndroidEmulators extends EmulatorDiscovery {
bool get supportsPlatform => true;
bool get canListAnything => androidWorkflow.canListEmulators;
Future<List<Emulator>> get emulators async => getEmulatorAvds();
class AndroidEmulator extends Emulator {
AndroidEmulator(String id, [this._properties])
: super(id, _properties != null && _properties.isNotEmpty);
final Map<String, String> _properties;
// Android Studio uses the ID with underscores replaced with spaces
// for the name if displayname is not set so we do the same.
String get name => _prop('avd.ini.displayname') ?? id.replaceAll('_', ' ').trim();
String get manufacturer => _prop('hw.device.manufacturer');
Category get category =>;
PlatformType get platformType =>;
String _prop(String name) => _properties != null ? _properties[name] : null;
Future<void> launch() async {
final Process process = await processUtils.start(
<String>[getEmulatorPath(androidSdk), '-avd', id],
// Record output from the emulator process.
final List<String> stdoutList = <String>[];
final List<String> stderrList = <String>[];
final StreamSubscription<String> stdoutSubscription = process.stdout
.transform<String>(const LineSplitter())
final StreamSubscription<String> stderrSubscription = process.stderr
.transform<String>(const LineSplitter())
final Future<void> stdioFuture = waitGroup<void>(<Future<void>>[
// The emulator continues running on success, so we don't wait for the
// process to complete before continuing. However, if the process fails
// after the startup phase (3 seconds), then we only echo its output if
// its error code is non-zero and its stderr is non-empty.
bool earlyFailure = true;
unawaited(process.exitCode.then((int status) async {
if (status == 0) {
globals.printTrace('The Android emulator exited successfully');
// Make sure the process' stdout and stderr are drained.
await stdioFuture;
if (stdoutList.isNotEmpty) {
globals.printTrace('Android emulator stdout:');
if (!earlyFailure && stderrList.isEmpty) {
globals.printStatus('The Android emulator exited with code $status');
final String when = earlyFailure ? 'during startup' : 'after startup';
globals.printError('The Android emulator exited with code $status $when');
globals.printError('Android emulator stderr:');
globals.printError('Address these issues and try again.');
// Wait a few seconds for the emulator to start.
await Future<void>.delayed(const Duration(seconds: 3));
earlyFailure = false;
/// Return the list of available emulator AVDs.
List<AndroidEmulator> getEmulatorAvds() {
final String emulatorPath = getEmulatorPath(androidSdk);
if (emulatorPath == null) {
return <AndroidEmulator>[];
final String listAvdsOutput = processUtils.runSync(
<String>[emulatorPath, '-list-avds']).stdout.trim();
final List<AndroidEmulator> emulators = <AndroidEmulator>[];
if (listAvdsOutput != null) {
extractEmulatorAvdInfo(listAvdsOutput, emulators);
return emulators;
/// Parse the given `emulator -list-avds` output in [text], and fill out the given list
/// of emulators by reading information from the relevant ini files.
void extractEmulatorAvdInfo(String text, List<AndroidEmulator> emulators) {
for (final String id in text.trim().split('\n').where((String l) => l != '')) {
AndroidEmulator _loadEmulatorInfo(String id) {
id = id.trim();
final String avdPath = getAvdPath();
if (avdPath != null) {
final File iniFile = globals.fs.file(globals.fs.path.join(avdPath, '$id.ini'));
if (iniFile.existsSync()) {
final Map<String, String> ini = parseIniLines(iniFile.readAsLinesSync());
if (ini['path'] != null) {
final File configFile =
globals.fs.file(globals.fs.path.join(ini['path'], 'config.ini'));
if (configFile.existsSync()) {
final Map<String, String> properties =
return AndroidEmulator(id, properties);
return AndroidEmulator(id);
Map<String, String> parseIniLines(List<String> contents) {
final Map<String, String> results = <String, String>{};
final Iterable<List<String>> properties = contents
.map<String>((String l) => l.trim())
// Strip blank lines/comments
.where((String l) => l != '' && !l.startsWith('#'))
// Discard anything that isn't simple name=value
.where((String l) => l.contains('='))
// Split into name/value
.map<List<String>>((String l) => l.split('='));
for (final List<String> property in properties) {
results[property[0].trim()] = property[1].trim();
return results;