ADB on multiple emulator / emulator-5554 - android

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.

Related

Terminal & Windows command to get Android device name along with AVD name

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.

Unable to start the "HelloWorld" NativeScript app on an Android emulator [duplicate]

I'm having a problem with emulator-5554, it keeps telling me it is offline.
When I do a adb devices from the command line it says
emulator-5554 offline
Even after a fresh restart, I try that command and it still says it is offline.
The problem is when I try to install .apk files to the emulator using abd install <path> from the command prompt, it tells me that it is offline, if I create another device and run that one, then try to install the .apk files, it says I have too many devices connected. So in other words, I can't install my .apk files.
How in the world can I get rid of that damn emulator-5554? I heard that if you do a restart, it should clear all the devices, but that does not seem to be working. It is like it is getting initialized when my computer starts up. Has anyone run into this issue?
Thanks
1 . Simply "Wipe data" to fix this issue.
2 . If it doesn't work, go to emulated device and enable developer options > enable usb debugging
In such a case, you can do all of the following in order to be assured that your emulator starts working again :
Go to cmd and type adb kill-server
Go to task manager and find adb in processes. If you find one, right click on it and click on end process tree.
In eclipse, go to Window>Android Virtual Device Manager, click on the AVD you want to launch, click on start and uncheck "Launch From Snapshot" and then click on launch.
That's it! It will take a while and it should resolve your problem.
The way that Android detects emulators is by scanning ports starting at port 5555.
The number you see in the adb devices list (in your case 5554) will be one less than the port that adb is finding open.
You probably have a process running that is listening on port 5555. To get rid of the "offline" device, you will need to find that application and close it or reconfigure it to listen to a different port.
This solution is for Windows.
(See #Chris Knight's solution for Mac/Linux)
Start Windows Powershell:
Start -> type 'powershell' -> Press ENTER
Run the following command: adb devices
PS C:\Users\CJBS>adb devices
List of devices attached
emulator-5656 host
emulator-5652 host
12b80FF443 device
In this case, 12b80FF443 is my physical device, and the emulator-* entries are garbage.
Per #Brigham, "The way that Android detects emulators is by
scanning ports starting at port 5555.". The port number is indicated after the emulator name (in this case 5656 and 5652). The port number to check is the emulator port number plus 1. So in this case:-
5656 + 1 = 5657
5652 + 1 = 5653
So let's see which program is using these ports. In this case, the ports to check both start with "565". So I'll search for ports in use starting with 565. Execute: netstat -a -n -o | Select-String ":565"
PS C:\Users\CJBS> netstat -a -n -o | Select-String ":565"
TCP 127.0.0.1:5653 127.0.0.1:5653 ESTABLISHED 5944
TCP 127.0.0.1:5657 127.0.0.1:5657 ESTABLISHED 5944
The final field in this output is the PID (Process ID) - in this case it's PID 5944 for both of these two ports. So let's see what this process ID is. Execute: tasklist /v | Select-String 5944. Replace 5944 with the output of the previous command:
PS C:\Users\CJBS> tasklist /v | Select-String 5944
adb.exe 5944 Console 1 6,800 K Running MyPCName\CJBS 0:06:03 ADB Power Notification Window
What a surprise. It's ADB. As noted by other answers, it could be other programs, too.
Now, just kill this process ID. Execute kill 5944, replacing 5944 with the PID in the previous command.
PS C:\Users\CJBS> kill 5944
To confirm that the spurious emulator is gone, re-run the following command: adb devices
PS C:\Users\CJBS>adb devices
List of devices attached
* daemon not running. starting it now on port 5037 *
* daemon started successfully *
12b80FF443 device
ADB re-starts (as it was previously killed), and it detects no more fake emulators.
From the AVD Manager try the "Cold Boot Now" option in the drop-down. It worked for me!
If you are on Linux or Mac, and assuming the offline device is 'emulator-5554', you can run the following:
netstat -tulpn|grep 5554
Which yields the following output:
tcp 0 0 127.0.0.1:5554 0.0.0.0:* LISTEN 4848/emulator64-x86
tcp 0 0 127.0.0.1:5555 0.0.0.0:* LISTEN 4848/emulator64-x86
This tells me that the process id 4848 (yours will likely be different) is still listening on port 5554. You can now kill that process with:
sudo kill -9 4848
and the ghost offline-device is no more!
On macOS Big Sur and later, use
sudo lsof -i -P | grep LISTEN | grep 5554
to find out the process.
I finally solved this problem,
I had to go to the Developer options from the Settings in the Emulator,
then scrolled down a little, turned on the USB debugging. Instantly my device was recognized online, and I no longer faced that issue. I tried restarting android studio and emulator, killing adb process, but those did not work.
I also had the same issue. I've tried all solutions described here, but they didn't help me. Then I've removed all emulators in the Android Virtual Device Manager and created new ones. The problem was in the CPU/ABI system image configuration of the Android Virtual Device Manager. My Windows10 machine emulator with system image x86 is always offline, where the emulator with system image x86_64 is working fine as expected. Just be aware of this
I solved this by opening my commandprompt:
adb kill-server
adb devices
After starting up, ADB now detects the device/emulator.
In my case, I found some process that makes adb not work well.
You can try to kill some strange process and run "adb devices" to test.
It worked for me:
kill the process name MONyog.exe
Just write
adb -e reboot
and be happy with adb))
Enable USB Debugging into your emulator
Settings > About Phone > Build number > Tap it 7 times to become developer;
Settings > Developer Options > USB Debugging.
That's it enjoy
The "wipe user data" option finally solved my problem. just wipe user data every time you start the emulator. This always works for me!
I use windows 8 x64 , eclipse
open your emulator,
setting --> about emulated device --> click Build number repeatedly-->open developer options --> open USB debuggin
From AVD manager list at the actions dropdown:
Cold Boot Now
restarts it without all pain above.
Do you have bluestacks installed? If you do, the background processes that it runs creates the offline device "emulator-5554".
Go to the task manager and end all the processes with the description of "Bluestacks"
Try this ...
Close emulator if it Running.
Start Emulator again and wait for its online.
enter Command in commandprompt and press ENTER key : adb tcpip 5555
(Make sure that only One emulator running at a time.)
adb -s emulator-5555 emu kill
Press Enter Key....
Done.
check devices by command "adb devices" in cmd.
In my case, I started in 'Cold Boot Now' and clicked on Message to allow the connection.
Did you try deleting and recreating your AVD?
You can manually delete the AVD files by going to the directory they're stored in (in your user's /.android/avd subdirectory).
Go to windows task manager and end process "adb.exe". There might be more than 1 instances of the same process, make sure to end all of them.
on linux or mac the port thats blocked will emulator-id + 1 so 5555 so:
sudo lsof -i :5555
will show you the pid of process that are taking the port (should be the second column) so to kill it:
sudo lsof -i :5555 | awk '{print $2}' | xargs kill
then adb (fake) devices will no longer show on the list
In my case, the emulator was working with Oreo and lower, but not with Pie, and everything I tried seemed to have no effect. What finally worked was updating the emulator to latest (version 28).
I found that the emulation environment comes up as "offline" when the adb revision I am using was not recent. I properly updated my paths (and deleted the old adb version) and upon "adb kill-server", "adb devices", the emulation environment no longer came up as "offline".
I was immediately able to use "adb shell" after that point.
If the emulator is already open or executing it will tell you is offline. You can double check on the Command Line (Ubuntu) and execute:
adb devices
You must see your emulator offline, you have to close the running instance of the emulator (since the port will show as busy) and after that you can run your application. Hope this helps someone.
I tried everything but only this one works for my case:
Use SDK manager, and reinstall the system image.
Android Studio, click Configure, SDK Manager, Launch Standalone SDK Manager,
Check all "Google APIs Intel x86* System Image", "Intel x86 Atom*System Image" and install. Then re-start Android studio.
You might have to reconfigure and wipe the virtual device with AVD Manager, make sure you choose x86 version.
Ensure that your enable ADB integration is marked;
go to Tools>Android>Enable ADB integration .
if doesn't checked , check this option and close your virtual device and re-open it . this worked for me.. good luck!!
In MAC, you can use Activity Monitor utility, since, unlike Linux, we cannot use netstat -tulpn command in MAC. Search for the running instance of the emulator, typically qemu-system-i386. Kill that instance and you will see none of the ghost emulator running.
Simplest way to grab Activity monitor utility is to use spotlight search. just hit cmd-space and type in Activity Monitor.
I had the same issue with my virtual device. The problem is due to the Oreo image of the virtual devices that have the Play Store integrated. To solve this problem I installed a new device without the Play Store integrated and all it was fine.
Hope it helps, Bye
See emulator-5554 unauthorized for adb devices. On API 29 emulator I run adb devices command and got emulator-5554 unauthorized message. Then I created a new avd device from Google APIs image (in my case Q, x86), not from Google Play.
Simply delete and created gear avd again.It will work.

See app background processes in Android Studio

Maybe I'm just missing it but is there a way to view WHAT is running in the background on-device in android studio?
I'm getting a battery usage alert on my phone (galaxy s8 - OS v8.0), indicating my app is doing something in the background and I'd like to see what it is.
Thank you
Yea, via the terminal function. This is one of options at the bottom of the Android studio.
You need to run ADB to connect to the device. Once connected you can use Unix command lines to see the processes running.
From google dev : https://developer.android.com/studio/command-line/adb
Also, the command I use to connect an emulator to run unix commands is:
adb -s <DEVICE> shell
Normally the ADB is stored :
USER\AppData\Local\Android\Sdk\platform-tools
and then once connected you can use the following to see the running processes :
https://www.howtogeek.com/107217/how-to-manage-processes-from-the-linux-terminal-10-commands-you-need-to-know/
Hope this helps.

How to start more than 16 Android emulators

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.

Install Apk in specific emulator

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

Categories

Resources