Monday, January 18, 2016

Stop or kill android emulator programmatically and from command line

If you are looking for starting(or launching) android emulator from command line or programmatically, please refer my post start or launch android emulator programmatically and from command line
Sometimes we need to stop our running emulator(s) to start a new emulator or to execute tests in a real devices or because of some other situation. To stop a running android emulator we can do by following ways:

Manually stop emulator:
Open the running emulator > click on the close button(red cross icon in the top menu bar). The emulator should be closed.

Stop emulator from command line:
Use the following command to kill all running emulators.
adb emu kill
Stop emulator programmatically:
We can close emulators programmatically also. The following code will close all the running emulators.
private static String sdkPath = "/Applications/adt-bundle-mac-x86_64-20140702/sdk/";
private static String adbPath = sdkPath + "platform-tools" + File.separator + "adb";
/**
 * Kills all running emulators
 */
public static void closeEmulator() {
 System.out.println("Killing emulator...");
 String[] aCommand = new String[] { adbPath, "emu", "kill" };
 try {
  Process process = new ProcessBuilder(aCommand).start();
  process.waitFor(1, TimeUnit.SECONDS);
  System.out.println("Emulator closed successfully!");
 } catch (Exception e) {
  e.printStackTrace();
 }
}
Hope it helps!

No comments:

Post a Comment