[android] How do I launch the Android emulator from the command line?

I'm on Mac, working on Android development from the terminal. I have successfully created the HelloWorld project and now I'm trying to run it from the command line in the Android emulator. Which command runs the emulator for my HelloWorld project?

I already have the Android tools and platform-tools in my PATH.

Edit:

How do I tell the emulator to run my HelloWorld project from the command line? I've already built the project with ant.

The answer is


Here you can check the options to execute this command via command-line:

emulator -avd avd_name [-option [value]] ... [-qemu args]

For example, I use it like this below:

emulator -avd ICS_ARM -wipe-data -no-boot-anim -cpu-delay 0

I wrote this simple shell script using Zenity that lets you pick which avd you want to run. If you don't have ANDROID_HOME defined, you can just replace that with the full path to emulator. This would be easy to do with select instead of Zenity also, but I opted for Zenity since I'm running it from the xfce-application menu (though a .desktop-file).

#!/bin/sh

opt=$(zenity --title="Choose AVD" --text="Choose which emulator to start" --list \
                   --column="Devices" --separator="\n" `$ANDROID_HOME/emulator/emulator -list-avds`);

$ANDROID_HOME/emulator/emulator -avd $opt

Starting emulator from command line with dns help

Emulator program location : /Users/{{UserName}}/Library/Android/sdk/tools

  1. Check existing avd :- emulator -list-avds

  2. Start emulator with dns set /Users/{{UserName}}/Library/Android/sdk/tools/emulator -avd Pixel_API_26 -dns-server 8.8.8.8


I think the best way to reach it via terminal is :

cd ~/Library/Android/sdk/emulator

To run a certain AVD directly:

./emulator -avd {AVD_NAME}

To list your AVDs use :

./emulator -list-avds

Run the first emulator available:

~/Library/Android/sdk/tools/emulator -avd `~/Library/Android/sdk/tools/emulator -list-avds | awk '{print $1}'`

list of avd name available on

.android/avd

/SDK_PATH/tools/emulator -netdelay none -netspeed full -avd "AVD_NAME"


If you have Git Bash you can open your emulator with a double-click, with some initial setup (tested in Windows 10 only):

  1. Open Git Bash.
  2. Run cd ~/AppData/Local/Android/sdk/emulator
  3. Run ./emulator -list-avds and keep note of the emulator's name. MY_EMULATOR for example.
  4. Open Notepad and add these two lines (insert your emulator's name instead of MY_EMULATOR):

    cd ~/AppData/Local/Android/sdk/emulator; ./emulator -avd MY_EMULATOR

  5. Save the file somewhere convinient as myfile.sh. You can change "myfile" as suits you best, but the extension .sh is needed.

  6. Open Git Bash at the directory where you stored myfile.sh and run chmod +x myfile.sh. Remember to put your file's name instead of myfile.

And that's it! From now on, everytime you need to start that emulator, just locate your myfile.sh and double click on it and wait for your Android emulator to launch!


For one-click (BATCH file) launch, this is what I've done:

  • got the name of AVD from Android Studio -> Tools -> AVD Manager -> Click on Arrow Down and choose View Details ( for me it's Pixel_2_API_28, so change accordingly below)
  • create an start.bat file and put the following inside:
 c: 
 cd C:\Program Files (x86)\Android\android-sdk\emulator\ 
 emulator -avd Pixel_2_API_28

Explanations:

  • First line: for me was necessary because I am launching it from d:
  • Second line: for me was not working the tools\emulator
  • Third line: make sure you change the name of the AVD with yours (here Pixel_2_API_28)

Using:

  • launch start.bat from command line

A launcher script which lists existing AVDs and lets you pick the one you want. Requires python3 (at least v3.4) and a valid environment variable ANDROID_HOME or ANDROID_SDK pointing to the Android SDK dir.

#!/usr/bin/env python3

import os
import subprocess
from pathlib import Path

android_emulator_dir: Path = Path(os.environ['ANDROID_HOME'] or os.environ['ANDROID_SDK']) / 'emulator'

if android_emulator_dir.exists():
    emulator_dir = android_emulator_dir.absolute()
    print(f'SDK emulator dir: {emulator_dir}', end='\n\n')

    proc = subprocess.Popen(['./emulator', '-list-avds'], stdout=subprocess.PIPE, cwd=emulator_dir, text=True)
    avds = {idx: avd_name.strip() for idx, avd_name in enumerate(proc.stdout, start=1)}

    print('\n'.join([f'{idx}: {avd_name}' for idx, avd_name in avds.items()]))

    avd_idx = input("\nType AVD index and press Enter... ")
    avd_name = avds.get(int(avd_idx))

    if avd_name:
        subprocess.Popen(['./emulator', '-avd', avd_name, '-no-boot-anim'], cwd=emulator_dir)
    else:
        print('Invalid AVD index')
else:
    print(f'Either $ANDROID_HOME or $ANDROID_SDK must be defined!')

AppleScript version (MacOS only)

osascript -e '
set avds to paragraphs of (do shell script "~/Library/Android/sdk/emulator/emulator -list-avds")
set avd to (choose from list avds with prompt "Please select an AVD to start" default items "None" OK button name {"Start"} cancel button name {"Cancel"})
do shell script "~/Library/Android/sdk/emulator/emulator -avd " & avd & " -no-boot-anim > /dev/null 2>&1 &"
'

The script above can also be run from the Script Editor.app or from Automator.app (the bit between the single quotes). Also you can use Script Editor to save it as a native macos app: select File on the menu, press and hold the ? (Option) key, select Save As and then select File Format: Application.


On Mac (and Linux I think), after you have created your AVD, you can make an alias: alias run-android='~/Library/Android/sdk/tools/emulator -avd ${YOUR_AVD_NAME} &'

Note: the execution of the alias will not lock your terminal, if you want that, just remove the last '&'.

Run emulator it self will give you an error because he expect that, in your current position, you have: /emulator/qemu/${YOUR_PATFORM}/qemu-system-x86_64' to start the emulator.


Open command prompt and go to <android-Home>\sdk\tools>emulator -avd <AVD_NAME>

Here "emulator" is the command used to open your Android Virtual Device.


Go to the directory where emulator is located

${ANDROID_SDK}/tools/emulator

Check emulator list from CLI

emulator -list-avds

Run a AVD from the list

emulator -avd {avd_name}


The android create avd command is deprecated. It's now recommended to use avdmanager instead to launch emulators from the command line.

First, create a new emulator if one doesn't already exist:

avdmanager create avd --name "MyEmulator" -k "system-images;android-
26;google_apis;x86"

This assumes that you already have an X86 system image installed that matches API 26, and has the Google APIs installed.

You can then launch the emulator with emulator @MyEmulator.


List all your emulators:

emulator -list-avds

Run one of the listed emulators with -avd flag:

emulator -avd @name-of-your-emulator

where emulator is under:

${ANDROID_SDK}/tools/emulator

open CMD

  1. Open Command Prompt
  2. type the path of emulator in my case

C:\adt-bundle-windows-x86_64-20140702\sdk\tools enter image description here

  1. write "emulator -avd emulatorname" in my case

emulator -avd AdilVD

enter image description here


Instructions for Mac with zsh:

Open terminal window (CTRL+ALT+T) Run command nano ~/.zshrc to edit your profile Add following lines in the opened file:

export ANDROID_SDK_HOME="~/Library/Android/Sdk"

alias emulator="$ANDROID_SDK_HOME/emulator/emulator"

Save the file (CTRL+O, CTRL+X) Source the profile by running command source ~/.zshrc or just log out and log back in Test by running the command:

emulator -help or emulator -list-avds to show your simulator in terminal and run Android emulator with command:

emulator -avd <name>

NOTE: Should be same for bash by replacing .zshrc with .bashrc


  • easily type this command in cmd.
  • replace after Users\ your user name \
  • if you don't have this file reinstall android studio.

type this command in cmd


  1. Open CMD
  2. To get all Emulator list (C:\Users\DJango\AppData\Local\Android\sdk\emulator>emulator -list-avds)
  3. Choose any emulator (C:\Users\DJango\AppData\Local\Android\sdk\emulator>emulator -avd Pixel_2_API_28)

Just to add here, whenever you get "error: device offline" means that connection with emulator & adb bridge has been broken due to time taken in emulator startup.

Rather than re-starting emulator at this point try below two commands which stops & start adb bridge again.

adb kill-server

adb start-server


If you are strictly trying to run the emulator from the command line try this in OSX.

"/Applications/Android Studio.app/sdk/tools/emulator" -avd <NAMEOFDEVICE> -netspeed full -netdelay none

You can simplify it by adding an alias to the .bash_profile, and sending it to a background job.

alias android='/Applications/Android\ Studio.app/sdk/tools/emulator <NAMEOFDEVICE> -netspeed full -netdelay none &'

Let Bash know about the changes.

source ~/.bash_profile

(Linux) In you terminal Copy & Paste and changing the three fields in blue

/home/YouUser/Folder/adt-bundle-linux-x86_64-20140702/sdk/tools/emulator64-x86 -avd Android5.1.1

YouUser = user of session in linux
Folder = path of folfer
Android5.1.1 = You version of android in the emulator,for example (Android4.4.2)

you can view the emulators in you folder home cd /home/Youuser/.android/avd/


In windows, I use this PowerShell script to start it up.

$em = $env:USERPROFILE+"\AppData\Local\Android\sdk\tools\emulator.exe"; 
Start-Process $em " -avd Nexus_5X_API_24" -WindowStyle Hidden;

See below instructions for Ubuntu Linux with zsh:

  1. Open terminal window (CTRL+ALT+T)
  2. Run command nano ~/.zshrc to edit your profile
  3. Add following lines in the opened file:
export ANDROID_SDK_HOME="~/Android/Sdk"
alias emulator="$ANDROID_SDK_HOME/emulator/emulator"
  1. Save the file (CTRL+O, CTRL+X)
  2. Source the profile by running command source ~/.zshrc or just log out and log back in
  3. Test by running the command emulator -help in terminal

NOTE: Should be same for bash by replacing .zshrc with .bashrc


Nowadays asuming you have Android Studio installed (2.2) in my case and just 1 emulator you might use this one liner

export ANDROID_SDK_ROOT=~/Library/Android/sdk/ && emulator '@'`emulator -list-avds`

If you do this often, make it easier:

$ echo 'export ANDROID_SDK_ROOT=~/Library/Android/sdk/' >> ~/.profile

Add an alias to ~.aliases

alias androidup="emulator '@'`emulator -list-avds`" 

Recall to source ~/.profile ~/.aliases before testing it

Next time just $ androidup


I am late, here but want to share so may be it help some one and me too when ever needed later :) , So below is the way to open emulator from command line with one command using bash script. I am using MX Linux but process is same on all operating systems

1- First Check the installed emulators

emulator -list-avds

it will result like below

emulator -list-avds
Nexus_4_API_28
Pixel_2_API_28

2- open any plain text or code editor and create a new file and write as below

#!/bin/sh
emulator -avd Nexus_4_API_28

Nexus_4_API_28 is the emulator that i want to open you write yours which you got from first step

save this file with .sh extension

3- Then, change the permissions on the file to make it executable:

chmod u+x emu.sh

4- Now open the emulator just executing this bash script file with following command

./emu.sh

enter image description here


Examples related to android

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How to implement a simple scenario the OO way My eclipse won't open, i download the bundle pack it keeps saying error log getting " (1) no such column: _id10 " error java doesn't run if structure inside of onclick listener Cannot retrieve string(s) from preferences (settings) strange error in my Animation Drawable how to put image in a bundle and pass it to another activity FragmentActivity to Fragment A failure occurred while executing com.android.build.gradle.internal.tasks

Examples related to command-line

Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) Flutter command not found Angular - ng: command not found how to run python files in windows command prompt? How to run .NET Core console app from the command line Copy Paste in Bash on Ubuntu on Windows How to find which version of TensorFlow is installed in my system? How to install JQ on Mac by command-line? Python not working in the command line of git bash Run function in script from command line (Node JS)

Examples related to android-emulator

flutter run: No connected devices How to remove the Flutter debug banner? Android Studio AVD - Emulator: Process finished with exit code 1 Android Studio Emulator and "Process finished with exit code 0" Run react-native on android emulator ERROR Android emulator gets killed Error while waiting for device: Time out after 300seconds waiting for emulator to come online Unfortunately Launcher3 has stopped working error in android studio? updating Google play services in Emulator Android Studio emulator does not come with Play Store for API 23

Examples related to console

Error in MySQL when setting default value for DATE or DATETIME Where can I read the Console output in Visual Studio 2015 Chrome - ERR_CACHE_MISS Swift: print() vs println() vs NSLog() Datatables: Cannot read property 'mData' of undefined How do I write to the console from a Laravel Controller? Cannot read property 'push' of undefined when combining arrays Very simple log4j2 XML configuration file using Console and File appender Console.log not working at all Chrome: console.log, console.debug are not working

Examples related to console-application

How to run .NET Core console app from the command line ASP.NET Core configuration for .NET Core console application How to keep console window open How to navigate a few folders up? How to stop C# console applications from closing automatically? Could not load file or assembly ... An attempt was made to load a program with an incorrect format (System.BadImageFormatException) Can I write into the console in a unit test? If yes, why doesn't the console window open? What is the command to exit a Console application in C#? Can't specify the 'async' modifier on the 'Main' method of a console app Why is the console window closing immediately once displayed my output?