15

If I run several emulators with -no-window option in one machine. The command "adb devices" shows:

List of devices attached
emulator-5554  device
emulator-5556  device
emulator-5558  device
...

However, according to this output, I can't tell the difference between each emulator device at all. I need to know which emulator runs on what AVD, then I can install APKs or send commands to the emulator.

How can I recognize each emulator device or get the serial number of emulator after it runs?

papalagi
  • 710
  • 2
  • 8
  • 20

3 Answers3

32

Always start the same AVD on the same ports, don't let emulator decide. Use

$ emulator -ports X,Y @avd_X

then, the serial number will be emulator-X and your avd_X will always be on ports X,Y, so you can run your commands with this serial number, like for example

$ adb -s emulator-X shell cmd

To kill the emulator run

$ adb -s emulator-X emu kill
Vlad
  • 18,195
  • 4
  • 41
  • 71
Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
  • @dtmilano - note the OP has a question about this – Marc Gravell Apr 19 '10 at 11:44
  • 4
    Still a great answer 3+ years later. Slight simplification (as of at least emulator 22.0.5.0): Using `-port` rather than `-ports` allows you to specify only a _single_ port number; e.g. `$ emulator -port 5556 @avd_X`; note that the port number must be an **even number** between 5554 and 5584 in this case (curiously, using `-ports` does not impose this range restriction). – mklement0 Sep 07 '13 at 19:01
  • 1
    @mklement0 if you like that answer, you'll love my answer. – Cameron Lowell Palmer Feb 04 '17 at 10:32
  • @CameronLowellPalmer well, the difference is in this case you know in advance which emulator is which and in the other you have to search to find it – Diego Torres Milano Feb 04 '17 at 19:41
  • 2
    Well, in the other case, you'll never know. You only know which AVD was used to start an emulator. If there are 10 emulators running you'll never know which one was the one you were looking for without starting it with some sort of unique identifier – Cameron Lowell Palmer Feb 04 '17 at 20:26
6

There are 2 ways that I know of to perform a reverse Serial Number to AVD name lookup

The Telnet option - The Ugly Way

As pointed out in this SO answer... you can reverse lookup the AVD name for each serial number using Telnet. This is kind of weak, because all you're doing is finding an instance of the emulator launched given a particular AVD name. It doesn't uniquely identify the emulator you are wanting to work on. It also suffers from the need to use telnet and parsing out the port number for each emulator.

First get the currently running serial numbers

adb devices

then telnet to each device's port number

telnet localhost 5554

and issue the command

avd name

which will return the AVD name of that emulator.

The UUID option - The Right Way

I originally saw this done in a project called DCMTK. Generate a UUID uuidgen and set a property on the emulator at launch! My example launches an emulator to perform some compile time checks for libraries that require running code on the target to determine type information.

emulator -avd nexus19-arm -no-window -no-boot-anim -noaudio -prop emu.uuid=7a6f8701-43c2-4e16-988a-8b4992c0bf8d >/dev/null </dev/null 2>&1 &

Then when you want to find that specific instance of the emulator you just roll through all running emulators and look for that UUID.

adb -s emulator-5556 shell getprop emu.uuid

in a loop:

for SERIAL_NUMBER in `adb devices| grep emulator| cut -f1`; do 
    UUID=`adb -s ${SERIAL_NUMBER} shell getprop emu.uuid | tr -d '\r\n'`
    echo ${SERIAL_NUMBER} ${UUID}
done

Tracking the whole emulator lifecycle

  1. Launch the emulator with a UUID property
  2. Then kick off a loop that checks each device to be online and/or having the matching UUID
  3. Once you get a match call adb -s ${SERIAL_NUMBER} wait-for-device so you know when you can talk to the emulator
  4. If you need the system to be fully online check for the property sys.boot_completed
  5. When you're done just kill off the emulator with adb -s ${SERIAL_NUMBER} emu kill
Cameron Lowell Palmer
  • 21,528
  • 7
  • 125
  • 126
  • This looks like a great solution but I can only get "The Ugly Way" to work. I am unable to read a property that was set when starting the emulator. All I get back is a blank line. Did this "Right Way" option break or is there some other tip to get it to work? – Paul Lefebvre Apr 19 '19 at 15:04
  • 1
    Apparently there is a maximum of 247 properties and newer avds reach this maximum with the default properties, which prevents you from adding your own. I guess this means the "Ugly Way" is the only way that works right now. – Paul Lefebvre Apr 22 '19 at 13:26
  • 2
    Thank you for the answer, I loop through looking for the AVD name using this: ``` NAME=`adb -s "${SERIAL_NUMBER}" emu avd name | tr -d 'OK\r\n'` ``` – Chris.Jenkins May 02 '23 at 16:49
2

The same string (e.g., emulator-5554) is show in the title bar of the emulator window.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks for your response. I must run all emulators with -no-window option. So, there is no window title bar. – papalagi Feb 07 '10 at 03:04
  • Please explain how you do Android application development in such a way that requires you to run multiple emulators, each with the -no-window option. – CommonsWare Feb 07 '10 at 13:31
  • 2
    I try to automatically run test cases of many projects simultaneously on same server. In order not to interfere test results, I think it is better to create an AVD for each project. So I must recognize each emulator to install the correct APK and send right commands by using shell scripts. If there is no better way to get the serial number of an emulator, I will diff the output of "adb devices" command before and after starting an emulator to determine the serial number. – papalagi Feb 07 '10 at 15:46
  • First, that's a really cool setup. But, yes, I suspect a diff or something may be your best bet to get the emulator ID. I haven't used -no-window -- I assume it's not dumping the emulator ID out to stdout or something as part of executing the command. – CommonsWare Feb 07 '10 at 21:34