How do I launch the Android emulator from the comm

2020-01-23 04:39发布

问题:

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.

回答1:

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.


回答2:

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


回答3:

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

cd ~/Library/Android/sdk/tools

To run a certain AVD directly:

./emulator -avd {AVD_NAME}

To list your AVDs use :

./emulator -list-avds


回答4:

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



回答5:

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


回答6:

open CMD

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

C:\adt-bundle-windows-x86_64-20140702\sdk\tools

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

emulator -avd AdilVD



回答7:

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


回答8:

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.



回答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!



回答10:

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



回答11:

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.



回答12:

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



回答13:

list of avd name available on

.android/avd

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



回答14:

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;


回答15:

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.



回答16:

Run the first emulator available:

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


回答17:

  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)


回答18:

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



回答19:

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