Skip to content
This repository was archived by the owner on Feb 2, 2021. It is now read-only.

Commit 35c7757

Browse files
committed
Detect unexpected emulator exit and stops waiting for it
Fixes NativeScript/nativescript-cli#645
1 parent 724d4f3 commit 35c7757

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

mobile/android/android-emulator-services.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"use strict";
33

44
import * as Fiber from "fibers";
5+
import child_process = require("child_process"); // used only for type imports
56
import Future = require("fibers/future");
67
import * as iconv from "iconv-lite";
78
import {EOL} from "os";
@@ -22,6 +23,7 @@ class AndroidEmulatorServices implements Mobile.IEmulatorPlatformServices {
2223
private static TIMEOUT_SECONDS = 120;
2324
private static UNABLE_TO_START_EMULATOR_MESSAGE = "Cannot run your app in the native emulator. Increase the timeout of the operation with the --timeout option or try to restart your adb server with 'adb kill-server' command. Alternatively, run the Android Virtual Device manager and increase the allocated RAM for the virtual device.";
2425
private static RUNNING_ANDROID_EMULATOR_REGEX = /^(emulator-\d+)\s+device$/;
26+
private static EMULATOR_EXITED_MESSAGE = "The emulator has stopped unexpectedly. Verify its configuration and try again.";
2527

2628
private static MISSING_SDK_MESSAGE = "The Android SDK is not configured properly. " +
2729
"Verify that you have installed the Android SDK and that you have added its `platform-tools` and `tools` directories to your PATH environment variable.";
@@ -223,19 +225,33 @@ class AndroidEmulatorServices implements Mobile.IEmulatorPlatformServices {
223225

224226
// have to start new emulator
225227
this.$logger.info("Starting Android emulator with image %s", image);
228+
let emulatorProc: child_process.ChildProcess = null;
229+
let emulatorProcEnded = false;
226230
if(this.$options.geny) {
227231
//player is part of Genymotion, it should be part of the PATH.
228-
this.$childProcess.spawn("player", ["--vm-name", image],
229-
{ stdio: "ignore", detached: true }).unref();
232+
emulatorProc = this.$childProcess.spawn("player", ["--vm-name", image],
233+
{ stdio: "ignore", detached: true });
230234
} else {
231-
this.$childProcess.spawn('emulator', ['-avd', image],
232-
{ stdio: "ignore", detached: true }).unref();
235+
emulatorProc = this.$childProcess.spawn('emulator', ['-avd', image],
236+
{ stdio: "ignore", detached: true });
233237
}
238+
emulatorProc.on("error", (err:Error) => {
239+
this.$logger.trace("An error occured while starting the android emulator: %s", err.toString());
240+
emulatorProcEnded = true;
241+
});
242+
emulatorProc.on("exit", () => {
243+
this.$logger.trace("XXX EXIT EVENT!!!!");
244+
emulatorProcEnded = true;
245+
});
246+
(<any>emulatorProc).unref();
234247

235248
let isInfiniteWait = this.$utils.getMilliSecondsTimeout(AndroidEmulatorServices.TIMEOUT_SECONDS) === 0;
236249
let hasTimeLeft = helpers.getCurrentEpochTime() < this.endTimeEpoch;
237250

238251
while(hasTimeLeft || isInfiniteWait) {
252+
if (emulatorProcEnded) {
253+
//this.$errors.fail(AndroidEmulatorServices.EMULATOR_EXITED_MESSAGE);
254+
}
239255
emulatorId = this.getRunningEmulatorId(image).wait();
240256
if(emulatorId) {
241257
return emulatorId;

0 commit comments

Comments
 (0)