408

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.

Jeff Axelrod
  • 25,625
  • 29
  • 137
  • 239
Thierry Lam
  • 41,912
  • 39
  • 108
  • 142
  • Relevant docs: create with [`avdmanager`](https://developer.android.com/studio/command-line/avdmanager) and launch with [`emulator`](https://developer.android.com/studio/run/emulator-commandline). – jchook Feb 12 '19 at 20:28

28 Answers28

391

I assume that you have built your project and just need to launch it, but you don't have any AVDs created and have to use command line for all the actions. You have to do the following.

  1. Create a new virtual device (AVD) for the platform you need. If you have to use command line for creating your AVD, you can call android create avd -n <name> -t <targetID> where targetID is the API level you need. If you can use GUI, just type in android avd and it will launch the manager, where you can do the same. You can read more about AVD management through GUI and through command line.
  2. Run the AVD either by using command emulator -avd <name> or through previously launched GUI. Wait until the emulator fully loads, it takes some time. You can read about additional options here.
  3. Now you have to install the application to your AVD. Usually during development you just use the same Ant script you used to build the project, just select install target. However, you can install the application manually using command adb install <path-to-your-APK>.
  4. Now switch to emulator and launch your application like on any normal device, through the launcher. Or, as an alternative, you can use the following command: adb shell am start -a android.intent.action.MAIN -n <package>/<activity class>. For example: adb shell am start -a android.intent.action.MAIN -n org.sample.helloworld/org.sample.helloworld.HelloWorld. As a commenter suggested, you can also replace org.sample.helloworld.HelloWorld in the line above with just .HelloWorld, and it will work too.
makenova
  • 3,149
  • 3
  • 14
  • 20
Malcolm
  • 38,924
  • 10
  • 67
  • 89
  • 1
    Your 4 points list is what I'm looking for. When I run `adb install `, I get `error: device not found`. If I run the emulator in one shell and the adb install command in another, I get `error: device offline`. How do I set an emulated virtual device? – Thierry Lam Feb 12 '11 at 20:14
  • 3
    OK, brought back info from the previous version of the answer. :) If you get the message that device is offline, I think that you just haven't let the system load. It is useless to try sending commands to the emulator when it shows Android logo, you have to wait until it shows you the lockscreen. And you are right that you have to use another shell instance to send commands to your device. By the way, you may check devices' current status by using `adb devices` command. – Malcolm Feb 12 '11 at 20:24
  • Minor shortcut: "adb shell am start -a android.intent.action.MAIN -n org.sample.helloworld/.HelloWorld" also works. – ThomasW Oct 01 '11 at 11:43
  • 1
    @ThomasW Thanks, I've added this info to the answer. – Malcolm Oct 01 '11 at 12:19
  • Would it also be a good idea to call `adb wait-for-device` after starting the emulator? – L2G Jul 23 '12 at 17:15
  • 1
    @L2G If you run this as a script, then yes, you can do that. When I wrote the answer, I assumed you do everything by hand, in which case, of course, you can wait yourself. – Malcolm Jul 23 '12 at 17:44
  • Incredible, after reading the whole google android sdk site, these 4 points should be in every newbee manual at the 2nd page. – Glenn Plas Feb 18 '13 at 23:37
  • 4
    restart adb-server by: `adb kill-server; adb start-server` if nothing shows up on `adb devices` – evandrix Feb 19 '13 at 07:07
  • also , add sdk>tools path into environment path variable . – Tushar Pandey Oct 07 '13 at 05:10
  • To list the emulators you've already created, use the command **`android list avd`**. – Joshua Pinter Apr 07 '14 at 15:15
  • 1
    Looks like they are deprecating the `android` command's functionality, which is really a shame. All this GUI Android Studio stuff is giving me bad Visual Studio vibes, and the similar names aren't helping matters. – trysis Sep 11 '16 at 21:51
  • @trysis What makes you say so? – Malcolm Sep 11 '16 at 21:58
  • Read the page you linked to in your first bullet point. It says the `android` command is no longer supported. – trysis Sep 11 '16 at 22:09
  • @trysis That's bad news. How the hell are we supposed to use Android Studio when we don't have a GUI? – Malcolm Sep 11 '16 at 22:15
  • I don't know. All it says is use Android Studio. I would look up blogs, etc. Maybe Google will reverse this tragic ovetsight when they see how many people complain? – trysis Sep 11 '16 at 22:20
  • `android create adv` appears [deprecated](https://developer.android.com/studio/tools/help/android). However, looks like you can migrate to [`avdmanager`](https://developer.android.com/studio/command-line/avdmanager) with a similar syntax. – jchook Feb 12 '19 at 20:24
  • @Malcolm - i want to be able to execute command "emulator -avd " from a script, and get the output. this is for making sure i am getting "INFO: boot completed". but it seems that even though the emulator finishes loading, the command never returns. "Wait until the emulator fully loads, it takes some time" is not an option, as i executing the command from a script and don't want to use hardcoded "sleep". – Nisim Naim Apr 15 '19 at 11:42
  • @NisimNaim This looks like something you should ask as a separate question. – Malcolm Apr 15 '19 at 11:54
  • @Malcolm i did. simply thought you might know. – Nisim Naim Apr 15 '19 at 11:55
320

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
Jithin
  • 2,487
  • 18
  • 36
azibi
  • 3,413
  • 1
  • 11
  • 16
  • 6
    If you run from a Linux terminal, you must `cd` into the $ANDROID_SDK/tools folder or it will not work. – shimatai May 24 '17 at 22:23
  • 4
    Thanks! If you use `emulator` more often why not create an alias: `nano ~/.bashrc` > add `alias emulator='$ANDROID_HOME/tools/emulator'`, save, `. ~/bashrc` and you're done. (replace `ANDROID_HOME` with the correct variable used or the static path to android folder). – Ismail Jun 23 '17 at 23:02
  • 3
    If the following error is raised: ERROR:./android/qt/qt_setup.cpp:28:Qt library not found at ../emulator/lib64/qt/lib, starting emulator from $ANDROID_HOME/tools is a workaround. https://github.com/decosoftware/deco-ide/issues/289 – tebanep Jul 13 '17 at 18:35
  • If you really don't navigate to the `tools` folder - it really won't work. I got error `PANIC: Missing emulator engine program for 'x86' CPU.`. (I'm on Linux, OpenSUSE.) After I navigated to `tools` and then just ran `emulator -avd "{name}"` - then it worked. – pesho hristov May 21 '20 at 09:41
  • Example for windows: `C:\Users\\AppData\Local\Android\Sdk\tools> .\emulator.exe -avd Pixel_2_API_21` – NM Naufaldo Mar 12 '21 at 13:36
131

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
Ali Sao
  • 1,320
  • 1
  • 6
  • 8
29

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

Community
  • 1
  • 1
ABDroids
  • 3,145
  • 3
  • 17
  • 9
25

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
Vinoth Vino
  • 6,991
  • 3
  • 54
  • 59
germangti
  • 898
  • 11
  • 14
  • Hi germangti, I have the same issue. I am getting "Valid ABIs: no ABIs. Error: Invalid --abi x86 for the selected target. ". How have you fixed the issue? – Muneer Muhammed Apr 12 '16 at 08:59
18

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

Adiii
  • 35,809
  • 6
  • 84
  • 87
13

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
Rick
  • 10,168
  • 2
  • 38
  • 40
  • zsh: no such file or directory: NAMEOFDEVICE – Ben Affleck May 06 '15 at 19:20
  • 2
    @Andy you are supposed to replace `NAMEOFDEVICE` with the actual name of the emulator. do a `emulator -list-avds` to get a list of avds that you have in your system. – nabn Sep 16 '15 at 07:43
9

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.

Vinoth Vino
  • 6,991
  • 3
  • 54
  • 59
Chandra Shekhar
  • 262
  • 2
  • 4
9

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!

romin21
  • 1,284
  • 1
  • 10
  • 25
5

(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/

David Hackro
  • 3,276
  • 5
  • 35
  • 56
5

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

Jaime Agudo
  • 6,969
  • 4
  • 26
  • 33
5

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.

fractalwrench
  • 3,784
  • 7
  • 31
  • 47
5
  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)
Arman Javed
  • 51
  • 1
  • 3
5

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

paydarsefat
  • 51
  • 1
  • 3
3

list of avd name available on

.android/avd

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

rinkesh
  • 2,852
  • 23
  • 30
3

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;
Andy Li
  • 89
  • 4
3

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}

Paul P
  • 31
  • 2
2

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

Javeed Ishaq
  • 1,666
  • 2
  • 14
  • 28
1

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.

Ariel Jiménez
  • 111
  • 2
  • 5
1

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
1

Run the first emulator available:

~/Library/Android/sdk/tools/emulator -avd `~/Library/Android/sdk/tools/emulator -list-avds | awk '{print $1}'`
Shl
  • 1,414
  • 11
  • 13
  • I do something similar: ```~/Library/Android/sdk/tools/emulator -avd $(~/Library/Android/sdk/tools/emulator -list-avds | head -1)``` – Kamen Tsvetkov Dec 12 '19 at 03:57
1

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
Watth
  • 198
  • 2
  • 10
0

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

dayanand
  • 149
  • 2
  • 5
0

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

Saikat
  • 8,190
  • 12
  • 69
  • 94
0
  • 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

0

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.

ccpizza
  • 21,405
  • 10
  • 121
  • 123
0
  1. To list all the devices installed: emulator -list-avds

This will list all the devices installed inside your Android Studio.

  1. To run emulator: emulator -avd device_name

For example, if you got Pixel_3a_API_30_x86 after running the command in option 1, then your command to run emulator will be: emulator -avd Pixel_3a_API_30_x86

Waqar Shahid
  • 41
  • 1
  • 2
  • 10
0

windows 10 what helped me:

1 cd C:\Users\xxx\AppData\Local\Android\Sdk\emulator

2 list all the available: emulator -list-avds

3 start as NON-GUI mode: emulator -avd Pixel_3a_API_30_x86 -no-window

vlatko606
  • 481
  • 4
  • 12