DevOps: Launching an Android Emulator from the Terminal using Bash Scripts

I spend a lot of time in Visual Studio Code working with React Native these days. If you’re like me, you’re probably starting your debugging instances right from Visual Studio via the integrated terminal; npm run ios or npm run android is a super easy and quick way to kick of a build and begin testing your code from a local simulator/emulator. As you’re probably aware, doing so means starting your app in the “default” simulator for each platform, determined by your XCode version or Android Virtual Device (AVD) list respectively. But, what if you don’t want the default device? For iOS, the solution is fairly simple; you can simple pass a device name as a parameter along with your run command, like so

npm run ios -- --simulator "iPhone 13 mini"

A selection of devices that can be launched from the command line when a name is passed to npm start

On Android, the solution is less simple! If you don’t already have a device or emulator running and connected prior to executing npm run android, you will always start with the default emulator, which is typically whatever device appears first alphabetically in your list of AVDs. As far as I could find, the only way to start a different device is to launch Android Studio, navigate to the AVD manager and select one manually through the GUI. Once started (assuming it is the only one), subsequent runs of npm run android will launch on whatever emulator is active.

A selection of AVDs in my instance of Android Studio. Running npm run android will always start with the Nexus 10 device, in my case

I wanted to away to start the emulator without launching a whole separate IDE. As a workaround, I’d accept launching the AVD Manager directly, but I couldn’t find an easy way to do so. Instead, I set out to write a simple bash script to automate a lot of the labor of listing AVDs and launching one specifically for me using a semi-interactive prompt. Here’s what I ended up adding to my ~/.zprofile:

android-start() {
  echo Select an emulator to start:
  avds=($(emulator -list-avds))

  select em in $(emulator -list-avds) exit; do 
   case $em in
      exit) echo "exiting"
            break ;;
         *) echo "Starting: $em"
	    emulator -avd $em
            break ;;
   esac
  done
}

Before we go further, double check your default shell and make sure you have already added Android Tools to your $PATH, which I discuss as part of QUICK START: TELLING YOUR NEW REACT NATIVE INSTALL ABOUT YOUR EXISTING ANDROID TOOLS as well as how to set up a profile, if you haven’t already done so.

In my case zsh is my default shell, but the same logic would work for bash. The script above is comprised of just a few simple parts:

  • First, we get a list of emulators using the android tools command emulator -list-avds and save it to a variable.
  • We then loop over the array of emulator names, and wait for user input based on a simple switch statement.
  • When the user selects an emulator, we assign it to a variable and try to start it using the android tools command emulator -avd or exit, if the user choose the escape option.

Once the command has been added to the profile, simply restart any active terminal instances, then type the command name android-start to initiate the script. In practice, here’s what that looks like:

Running the bash command

Simple as that! Now you, too, have an easy way to start a specific emulator from the command line.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s