How to get device name along with AVD name
Created 3 Android Virtual Devices (Android 8, 9 10 versions)
Started emulators from AVD Manager
In Terminal window
ADB Command
adb devices
above command returned output as below
List of devices attached
emulator-5554 device
emulator-5556 device
emulator-5558 device
And emulator command
emulator -list-avds
above command returned output as below
Pixel_2_XL_API_27
Pixel_2_XL_API_28
Pixel_2_XL_API_29
I would like to know which device is attached to each AVD name
Also if I boot AVD in a different order then device name is changing
Example:
if I boot only one emulator Pixel_2_XL_API_28 then emulator-5554 is showing, if I boot the Pixel_2_XL_API_28 after booting Pixel_2_XL_API_29 then emulator-5556 is showing for Pixel_2_XL_API_28
is it possible to associate one device id with one AVD Name?
Thank you
Here is a solution for you, in python
def get_details():
devices = str(subprocess.check_output("adb devices|grep -|grep device|awk {'print $1'}", shell=True,text=True))
ans = str.strip(devices).split('\n')
port_avd={}
for emulator in ans:
port= emulator[-4:]
p = subprocess.Popen("((sleep 1;echo 'avd name';sleep 1)|telnet localhost " + port + ")|tail -2 ",shell=True,stderr=subprocess.PIPE,stdout=subprocess.PIPE,text=True)
port_avd[port] = p.stdout.readline().strip()
return port_avd
If you will print the output of this function you'll get a dictionary in the form of
{'5554': 'Nexus_5_API_24'}
Where key '5554' is from the list of devices and the value is the AVD name, as requested.
Important Notes:
The use of shell=True is not secure, you can look it up.
There might be a more efficient way to do it, since I am initiating a telnet session to get the AVD name.
Related
I am running multithreaded emulation and have more than one emulator.
Problem : In ADB They are all named the same so I only see one device
how do I change the names or send command to them seperately ?
EDIT :
Sorry guys I didn't see it had not so much information and to be honest I wasn't expecting to get answers
-> I'm running android x86 on virtualbox and ADB to control it
(I killed served and checked devices again for every situation)
Here you can see emulator-5554 is available when ANDROID 1 vm is running
Here you can see emulator-5554 is available when ANDROID 2 vm is running
Here you can see emulator-5554 is available when ANDROID 1 vm is running AND ANDROID 2 is running
So what I want to do is using controling both or more using python with this script
import subprocess
import time
import threading
def run_adb_command(command):
process = subprocess.Popen(["adb", command], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, error = process.communicate()
return output, error
run_adb_command("kill-server")
def start_virtual_devices():
for i in range(1, 21):
device_name = "Android " + str(i)
subprocess.run(["VBoxManage", "startvm", device_name, "--type", "headless"])
time.sleep(2)
def get_connected_devices():
output, error = run_adb_command("devices")
devices = []
for line in output.decode().splitlines():
if "\tdevice" in line:
device_id = line.split("\t")[0]
devices.append(device_id)
return devices
def set_proxy_and_login(device, proxy, login):
run_adb_command("-s {} shell settings put global http_proxy {}".format(device, proxy))
# Set login in the app using adb commands
proxies = [] # List of proxy strings in the format "host:port"
logins = [] # List of login strings in the format "username:password"
start_virtual_devices()
devices = get_connected_devices()
if len(devices) == 0:
print("0 devices are connected".format(len(devices)))
exit()
print(str(len(devices))+" devices are connected".format(len(devices)))
for i in range(len(devices)):
#device = devices[i]
#proxy = proxies[i % len(proxies)]
#login = logins[i % len(logins)]
#set_proxy_and_login(device, proxy, login)
print("Thread start")
print("All threads have finished execution")
This script basically just create VMs, wait for 30s so they load, look for devices in ADB and attribute proxy and device-id to each thread to control VMs with ADB
But the problem is since they are all named the same, ADB doesn't dissociate them and only count 1 everytime. How can I make them not all named the same ? I tried controlling them separately using IP:PORT but it doesn't seem to work or maybe I'm just super bad at it.
All my VMs are port redirected like that
I know for a fact it's possible since I know someone who did it but doesn't want to help me out to do it.
His program is compiled. So I looked at his VMs configuration and saw that
They all have a different host port
So I tried changed it as I thought it would change the device name but ADB devices just doesn't see it anymore.
So I had the good idea to install ADB by myseld on the computer and check for the name of the VMs but the installed ADB doesn't see any Android emulator connected. So Maybe I have to change some ADB settings to see and control them separately ?
here when device not showing on ADB on another computer
After looking on forums I found out emulator-5554 is named for the port of the emulator so if I can understand how to change the port or settings on ADB maybe I can have list of devices like that :
Emulator-5554
Emulator-5553
Emulator-5552
and control them using the -s command
Thank you for your time and help, I'm very confused on what to do to solve the issue.
Below is the procedure to enumerate Nokia-5 Android Nougat device under Ubuntu 14.04 LTS terminal window
SYMPTOM: adb devices does not correctly enumerate Nokia-5 device under Ubuntu 14.04 terminal and displays permissions error message like below:
> adb devices
**ABCDEFGHIJ123456 no permissions (verify udev rules); see [http://developer.android.com/tools/device.html**
MY ANDROID SETUP: Android SDK for Nougat and platform-tools are up-to-date via Android Studio. (ADB version 1.0.39)
DESCRIPTION:
On a fresh Nokia 5 android nougat device, I had problem enumerating the phone via ADB. Using Android Studio, selecting target device under Run would display the device as ABCDEFGHIJ123456[null]. Upon checking the permission of the adb under ${ANDROID_HOME}/platform-tools - indicated that adb had execute (+x) permissions. So, I was not sure what was the problem.
SOLUTION: Looking into lsusb, there was vendorID and productID followed by empty string like:
...
...
Bus 001 Device 007: ID 2e04:c026
...
...
It took a while to figure out the empty string (that I overlooked) within the list of other vendorIds.
Once I realized this, I edited the 51-android-rules to add the above vendorId (HMD Global) and productId along with permission=0666 and group="plugdev", saved the file and reconnected the device
> nano /etc/udev/rules.d/51-android.rules
and add the following line:
SUBSYSTEM=="usb",ATTR{idVendor}=="2e04",ATTR{idProduct}=="c026",MODE="0666",GROUP="plugdev"
OUTPUT:
$ adb devices
List of devices attached
* daemon not running. starting it now at tcp:5037 *
* daemon started successfully *
ABCDEFGHIJ123456 unauthorized
Select "Allow access from computer.." on the device and issue command again:
$ adb devices
List of devices attached
D1AGAD1762314433 device
Now, I am able to use the device without any glitch.
Hope it helps others in need!
With 16 emulators running, adb devices shows
List of devices attached
emulator-5584
emulator-5582
emulator-5580
emulator-5578
emulator-5576
emulator-5574
emulator-5572
emulator-5570
emulator-5568
emulator-5566
emulator-5564
emulator-5562
emulator-5560
emulator-5558
emulator-5556
emulator-5554
To start the 17th emulator, I run:
emulator -avd Nexus_4_API_19_17 -ports 5586,5587
but I later get the following warning
emulator: WARNING: Requested adb port (5587) is outside the recommended range [5555,5586]. ADB may not function properly for the emulator. See -help-port for details.
And adb devices doesn't show new emulators. Running emulator -help-port doesn't provide any useful information:
At startup, the emulator tries to bind its control console at a free
port starting from 5554, in increments of two (i.e. 5554, then 5556,
5558, etc..) this allows several emulator instances to run
concurrently on the same machine, each one using a different console
port number. use '-port ' to force an emulator instance to use
a given console port note that must be an even integer
between 5554 and 5584 included. +1 must also be free and will
be reserved for ADB. if any of these ports is already used, the
emulator will fail to start.
Is there a way to make adb work with more than 16 emulators ?
You need to increase ADB_LOCAL_TRANSPORT_MAX in adb/transport_local.cpp to the desired value and recompile the adb binary.
From v41, you can set ADB_LOCAL_TRANSPORT_MAX_PORT as an env-var without needing to recompile. This has the same effect as increasing transport_max count.
Run adb --help: my patch also added documentation for this environment variable at the bottom of the help text.
Im trying to install an apk on an specific emulator using TeamCity. Right now I`m starting an emulator an installing the apk successfully.. but if there is already one or more instances of the emulator running, team city cannot decide in which emulator install the apk (even when I'm starting the emulator from teamcity scripts).
The problem is that I cannot identify the emulator I just started using:
emulator -adv myEmulator
it will start an emulator between ports 5554 to 5587... I know I can set the emulator instance where I want to install my apk:
adb -s emulator-5554 install path/apk
but I don't know the id of the emulator I just started, so it could have any number in its name between that range.
I know I can set an UUID to the emulator (from here), but again.. don't know how to use that uuid.
Right now I will try to save the available emulators before I run my own using BATCH, and then compare both arrays to get my emulator.. but I'm not an expert on BATCH.. so if anyone has a clue it's gonna help,
Regards
You seem to look for -s option of adb:
-s <specific device> - directs command to the device or emulator with
the given serial number or qualifier.
other useful options:
-d - directs command to the only connected USB device
returns an error if more than one USB device is
present.
-e - directs command to the only running emulator.
returns an error if more than one emulator is
running.
and in general adb -h to see them all.
EDIT
You can define what port you want emulator to use:
-ports <consoleport>,<adbport> TCP ports used for the console and adb bridge
so you can assign ports manually and do not bother guessing. to avoid port collisions you can either force ports for all emulators launched (you should not have more than i.e. 3 running at the same time usually) or you can choose high port numbers for your range, high enough so even any other emulator is launched it will be using available ports below your range.
Alternatively you can parse emulator log produced when use with -verbose switch as you can find there:
emulator: control console listening on port 5554, ADB on port 5555
emulator: sent '0012host:emulator:5555' to ADB server
If you want to save emulator log to specific file, use regular stream redirection:
emulator -verbose #MyAVD > log.txt
I'm using Eclipse on mac for developing android apps.
When I run the application on emulator the log cat window shows what is expected and do work fine but when I run(or even debug) on real android connected device my log cat window doesn't show even a single line.
How to deal with log cat when running on real android devices?
Thanks,
You have to select the device in the DDMS environment, Windows tab, select "Devices", and then select your device, i.e. "HtXXXXXXX".
First, Is device really connected? To check whether the device is connected or not, just run adb devices on console. It will list all the devices attached to the system.
Second, just run the adb logcat command at the command prompt, it will display the logcat window separately.
While many devices are attached to a system, adb logcat will display the following message:
- waiting for device -
error: more than one device and emulator
so to resolve above case, you have to run the command with device id with -s option.
For example:
adb -s emulator-5556 logcat , this will display the logcat for emulator.
Try just to restart it when you connected to device.