Running Android Emulator without Android Studio

One of the things we have to do when developing mobile code is run an emulator or simulator. When I was developing Xamarin code, Visual Studio handled this for me. However, now that I am writing React Native code, it’s not so easy. I’ve recently switched over to a Mac to aid my development (since the tools are a little more stable on a mac). The recommended way to run an Android emulator is to start Android Studio, create a project, then click on the “AVD Manager” to start an emulator.

That’s a lot – I need to start an IDE just to start something else? Well, not really, but you need to know a few things first.

Let’s start by setting ANDROID_HOME. You can find the SDK location from within Android Studio. Just bring up the Preferences page and select the Android SDK option. The location is displayed for you:

While you have Android Studio open, open the AVD Manager and create an emulator. You can create emulators using the command line, but it’s such a rare thing to do that I prefer using a UI for that one. Once complete, you can close Android Studio down again.

The default shell for users is bash. The profile for bash is called .bash_profile and is stored within your home directory. I’m an old Linux guy, so I edit these in the terminal app with vi. However, you can use any text editor you wish. You need to add the following lines to your .bash_profile file:

# Set up for Android Studio
ANDROID_HOME=/Users/adrian/Library/Android/sdk
PATH="${ANDROID_HOME}/tools/bin:${PATH}"
export ANDROID_HOME PATH

Replace the value of ANDROID_HOME with the path you noted from within Android Studio. You can now close the terminal window and re-open it, or type . ~/.bash_profile to read in the new profile. Once there, type echo $ANDROID_HOME and ensure that the location is correct:

You can also type which avdmanager to ensure you have access to the AVD Manager from the command line. You can query the list of virtual devices by using the command avdmanager list avd:

You can run the emulator with the following: cd ${ANDROID_HOME}/tools; ./emulator -avd Nexus_5X_API_25_x86. That’s a mouthful. I prefer to use a small shell script that can easily be run from anywhere, automatically selects a default emulator, and puts it in the background. Here is the script:

#!/bin/bash

ANDROID_HOME=/Users/adrian/Library/Android/sdk
AVD_NAME=Nexus_5X_API_25_x86

if [ $# -gt 0 ]; then
        AVD_NAME=$1
        shift
fi

pushd ${ANDROID_HOME}/tools
./emulator -avd ${AVD_NAME} $* &
popd

I place this script in /usr/local/bin/emulator (you will need root privileges for this), and make it executable with sudo chmod 755 /usr/local/bin/emulator. With this in place, I can run emulator to run the default emulator with all the default options. I can also name the emulator and provide any emulator command line options I want.

With this script in place, I don’t need to start the Android Studio any more – I open Visual Studio Code, open the in-built terminal and type emulator – no need for any other programs.

Leave a Reply

Please log in using one of these methods to post your comment:

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 )

Google+ photo

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

Connecting to %s