Monday, January 18, 2016

Checking if android emulator or device is running

We can check if an android device or an emulator is attached(plugged in) or running before we start using the attached device.

Command line:
We can check the following command to check list of attached devices
adb devices
The above command will provide you the list of attached devices.
Example output,
List of devices attached 
192.168.56.101:5555 device
emulator-5554 device 
Programmatically:
We can do this check programmatically also as below
private static String sdkPath = "/Applications/adt-bundle-mac-x86_64-20140702/sdk/";
private static String adbPath = sdkPath + "platform-tools" + File.separator + "adb";
/**
 * Checks if an emulator or a device is already launched or plugged in
 * 
 * Example: 

 * List of devices attached 

 * 192.168.56.101:5555 device 

 * emulator-5554 device
 * 
 * @return
 */
public static boolean isEmulatorOrDeviceRunning() {

 try {
  String[] commandDevices = new String[] { adbPath, "devices" };
  Process process = new ProcessBuilder(commandDevices).start();

  BufferedReader inputStream = new BufferedReader(new InputStreamReader(process.getInputStream()));

  String output = "";
  String line = null;
  while ((line = inputStream.readLine()) != null) {
   System.out.println(line);
   output = output + line;
  }
  if (!output.replace("List of devices attached", "").trim().equals("")) {
   return true;
  }
 } catch (Exception e) {
  e.printStackTrace();
 }
 return false;
}
If the device or emulator is just plugged in or launched, it may not be ready though its running, so wait till it gets ready by the post Waiting for android emulator to be ready

Waiting for android emulator to be ready

When we start android emulator(or is already running), we need to ensure its ready for testing and is responding. 

Command line:
This can be ensured by following commands:
adb shell getprop dev.bootcomplete 
We need to wait for the above command till it returns 1
adb shell getprop sys_bootcomplete 
We need to wait for the above command till it returns 1
adb shell getprop init.svc.bootanim
We need to wait for the above command till it returns "stopped"
Note: 1st and 3rd commands are important to check. Maximum case 2nd command will be ready.

Programmatically:
Also can be done programmatically,
private static String sdkPath = "/Applications/adt-bundle-mac-x86_64-20140702/sdk/";
private static String adbPath = sdkPath + "platform-tools" + File.separator + "adb";
/**
 * Waits for the emulator to be ready
 */
public static void waitForEmulatorToBeReady() {
 try {
  String[] commandBootComplete = new String[] { adbPath, "shell", "getprop", "dev.bootcomplete" };
  Process process = new ProcessBuilder(commandBootComplete).start();
  BufferedReader inputStream = new BufferedReader(new InputStreamReader(process.getInputStream()));

  // wait till the property returns '1'
  while (!inputStream.readLine().equals("1")) {
   process.waitFor(1, TimeUnit.SECONDS);
   process = new ProcessBuilder(commandBootComplete).start();
   inputStream = new BufferedReader(new InputStreamReader(process.getInputStream()));
  }

  String[] commandBootAnim = new String[] { adbPath, "shell", "getprop", "init.svc.bootanim" };
  process = new ProcessBuilder(commandBootAnim).start();
  inputStream = new BufferedReader(new InputStreamReader(process.getInputStream()));

  // wait till the property returns 'stopped'
  while (!inputStream.readLine().equals("stopped")) {
   process.waitFor(1, TimeUnit.SECONDS);
   process = new ProcessBuilder(commandBootAnim).start();
   inputStream = new BufferedReader(new InputStreamReader(process.getInputStream()));
  }

  System.out.println("Emulator is ready to use!");
 } catch (Exception e) {
  e.printStackTrace();
 }
}
You can ensure if an emulator or device is already running before using the above method or commands by the post Checking if android emulator or device is running

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!

Start or launch android emulator programmatically and from command line

For executing android tests in android emulator(AVD), we need to start it and ensure its running before our test starts executing. This post will explain you how can we start android emulator from code and from command line(mainly these will be helpful when we configure our test execution jobs in CI tools like Jenkins)

Manually launching emulator:
This can be easily opened from the IDE(eclipse if you are using adt bundle)
Open eclipse > select menu "Windows" > Select "Android Virtual Device Manager" >  select the AVD you want to launch(create an AVD if not yet created) > Click on "Start..." button > Click "Launch" button to launch the emulator > The emulator will launch in a moment

Launching emulator from command line:
The following command will launch the android emulator. Please ensure you have SDK installed and path has set.
emulator -avd <avd_name>
For example,
emulator -avd AVD_for_Nexus_4_by_Google
You can start with many options, for more information please refer here.

Launching emulator programmatically:
You can achieve this in code too. You can execute this script using ProcessBuilder class in java as below.
private static String sdkPath = "/Applications/adt-bundle-mac-x86_64-20140702/sdk/";// or for windows D:/Android/adt-bundle-windows-x86_64-20140702/sdk/
private static String adbPath = sdkPath + "platform-tools" + File.separator + "adb";
private static String emulatorPath = sdkPath + "tools" + File.separator + "emulator";
Please make sure to change the value of "sdkPath" variable to your SDK installation directory.
The following code will start an emulator with the provided AVD name.
 
/**
 * Starts an emulator for the provided AVD name
 * 
 * @param nameOfAVD
 */
public static void launchEmulator(String nameOfAVD) {
 System.out.println("Starting emulator for '" + nameOfAVD + "' ...");
 String[] aCommand = new String[] { emulatorPath, "-avd", nameOfAVD };
 try {
  Process process = new ProcessBuilder(aCommand).start();
  process.waitFor(180, TimeUnit.SECONDS);
  System.out.println("Emulator launched successfully!");
 } catch (Exception e) {
  e.printStackTrace();
 }
}
The 3 minute(180 sec) wait in the above code is to wait for the emulator which can be decreased or increased depending upon your system performance. 

If you want to stop or kill your running emulator, please refer my post Stop or kill android emulator programmatically and from command line