Background:
I recently upgraded to an AMD processor and found that the Android device emulator was complaining about not having hardware acceleration. (This is an issue for another question). My old Intel computer worked fine, so I decided to run the emulator on the old Intel PC (emu-pc) and use my new AMD computer (dev-pc) to code.
Problem:
I wasn't able to directly have the emulator expose its ports on the emu-pc to connect to via adb on the dev-pc (again an issue for another question), so I installed the Windows 10 OpenSSH server (Microsoft instructions) and connected to it from my dev-pc, forwarding the proper ADB ports:
ssh {you}#<{remote ip} -L 5554:localhost:5554 -L 5555:localhost:5555
I then tried connecting to the remote emulator via adb:
adb connect localhost
I was met with
$ ./adb devices
List of devices attached
localhost:5555 unauthorized
I've tried a few of the posts stating you should disable and re-enable USB debugging or revoke all the USB debugging authorization or use the "Wipe Data" option in the AVD Manager. None of these worked. I also tried messing with the adbkeys on the emulator under /data/misc/adb/adbkeys but I get permission denied trying to do anything to that file. (I also can't ls it via an adb shell)
Any ideas?
I found something that worked for me. I was able to telnet to the emu-pc via port 5554, where I tried to auth on the Android console. The login message said:
Android Console: type 'auth <auth_token>' to authenticate
Android Console: you can find your <auth_token> in
'C:\Users\exile57\.emulator_console_auth_token'
I found that file and was able to log in. This made me think that the emulator thinks the connection is coming from the emu-pc, yet the keys that I was using were generated on the dev-pc. I found the keys on emu-pc in C:\Users\[your user]\.android as adbkey and adbkey.pub. I copied those to the dev-pc, killed the adb server, disconnected all devices, then tried reconnecting:
$ ./adb disconnect
./adb kill-server
./adb connect localhost
After a bit, I was able to connect and drive the device over adb:
$ ./adb devices
List of devices attached
localhost:5555 device
NOTE I'm not sure this is the whole story, as when testing this out for this answer, I deleted the dev-pc's adbkey and adbkey,pub and was still able to reconnect, so that seems a bit strange. It worked for me, but be aware, I'm not sure the mechanism.
In Android studio, Run menu > Run shows OFFLINE ... for the connected device.
Below is the procedure followed to solve it:
(Read the below note first) Delete the ~/.android/adbkey (or, rename to ~/.android/adbkey2, this is even better incase you want it back for some reason)
Note: I happened to do this step, but it didn't solve the problem, after doing all the below steps it worked, so unsure if this step is required.
Run locate platform-tools/adb
Note: use the path that comes from here in below commands
Kill adb server:
sudo ~/Android/Sdk/platform-tools/adb kill-server
You will get a Allow accept.. message popup on your device. Accept it. This is important, which solves the problem.
Start adb server:
sudo ~/Android/Sdk/platform-tools/adb start-server
In Android studio, do Run menu > Run again
It will show something like Samsung ... (your phone manufacture name).
Also installs the apk on device correctly this time without error.
Hope that helps.
Here is how I did it:
First you need to run the emulator on the host computer. I used Android Studio and I had to close it because I noticed that the adb process kept spawning.
Start port-forwarding using SSH in the development computer.
ssh -L 5554:localhost:5554 -L 5555:localhost:5555 user#emulator-host-ip
copy adbkey and adbkey.pub files found at: C:\Users\[your user]\.android from the host computer to the development computer. this step should get ride of the unauthorized problem
in the development computer kill the adb server and lookup connected devices:
$ ./adb kill-server
$ ./adb devices
List of devices attached
localhost:5555 device
I have created a Docker image which contains the Android SDK and am trying to expose my Android phone in a container running this image. So I used the --privileged flag and mounted the USB devices as follows:
$ docker run --privileged -v /dev/bus/usb:/dev/bus/usb -d -P my-android:0.0.1
However, when I run ADB devices, it does not show me the USB device:
ubuntu#d56b666be455:~/Android/Sdk/platform-tools$ ./adb devices
* daemon not running. starting it now on port 5037 *
* daemon started successfully *
List of devices attached
ubuntu#d56b666be455:~/Android/Sdk/platform-tools$
lsusb inside the container lists the device:
ubuntu#d56b666be455:~$ lsusb
...
Bus 002 Device 017: ID 04e8:6866 Samsung Electronics Co., Ltd GT-I9300 Phone [Galaxy S III] (debugging mode)
The device is however visible on the host:
⇒ ./adb devices
List of devices attached
4d11abcd65b74045 device
Host OS
$ uname -a
Linux ananya 3.16.0-33-generic #44~14.04.1-Ubuntu SMP Fri Mar 13 10:33:29 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
Docker version
$ docker --version
Docker version 1.5.0, build a8a31ef
What could be the issue?
I don't think the ADB daemon running on the device can be connected to two ADB servers. Try disconnecting it from your host machine's ADB and then connect it to the Docker container's ADB.
While I was trying the same, I ran into some other problems related to that, which I would like to share so that others may save their time:
Problem 1: lsusb was not installed in the container
In my case lsusb was not installed, so I installed it with the below command:
apt-get update
apt-get install usbutils
Problem 2: not able to see the device even after lsusb and ADB SDK installation
You need to restart your container with the -v option, and yes don't forget to kill the ADB server as stated in one of the answers.
On the host:
adb-kill server
docker run -ti -d --privileged -v /dev/bus/usb:/dev/bus/usb container_name
In case someone wanted do it from scratch, I have written a blog post on it:
How to connect ADB devices to Linux container
This doesn't answer the exact question you were asking, but does address what you were trying to accomplish - connecting to an android device connected to a docker host from an adb client running inside a docker container. I'm including this for anyone trying to accomplish the same thing (like I was).
The adb client supports a -H option which tells it where to find the adb server to connect to. Docker supports the hostname "host.docker.internal" which always maps to the IP address of the docker host. Assuming your device is connected to the docker host, you can do the following to get your containerized adb client to connect to the adb server running on the docker host:
adb -H host.docker.internal devices
Accomplishes the goal without having to mount the USB ports.
Reference: https://developer.android.com/studio/command-line/adb
Update:
I recently learned that host.docker.internal is only supported on Docker for Mac in versions 18.0 and above.
Running with just --privileged -v /dev/bus/usb:/dev/bus/usb did not work for me.
I tried forwarding the adb daemon's listening port using -p 5037:5037, but that did not help either.
It worked only after I added --net host.
This means the host machine's net interfaces are exposed to the docker so use it if you are fine with that. Maybe there are more ports that needed to be forwarded other than 5037....
This works for me.
Set your devices in PPT mode
(optionaly) Check if the devices is available localy $ adb devices
I try with many options and only one works. Using --network flag as the next example
Run container
$ docker run -it --net host ubuntu bash
output:
List of devices attached
464e128 device
I used this Dockerfile. Is for flutter projects
Update:
I ended up using --privileged and -v to map the whole of /dev/bus/usb and patching adb to accept one environment variable to specify the root USB device tree - /dev/bus/usb/001 etc.
This allowed us to use different USB busses for different containers for different groups of the same phone, and another environment variable patch allowed different VID:PID lists for different types of phone.
We're trying to allocate different USB buses to different Docker containers running TeamCity clients.
Each container needs ANDROID_ADB_SERVER_PORT setting to a different port (because we're not using segregated networking).
The host machine can't run adbd, because only one adbd can talk to a phone at any given time.
Each container gets one of the /dev/bus/usb/xxx directories, so we can plug phones into particular containers.
We have to synchronise the /dev/bus/usb/xxx directory every few seconds, to allow hot-plugging and reboots - just a shell loop on the host that runs tar cf devxxx.tar /dev/bus/usb/xxx, docker cp to transfer it, then docker exec to untar inside the container's /tmp, diff to detect nodes to delete, and mv -n to move new nodes in.
In fact, because we're running on Linux, we can probably just set up udev scripts, per Howto run a script when a USB device is pluged in.
Excuse me for offtop. But I cannot answer to this question so I write it down here.
I am using ubuntu:18.04 and tried to connect my Samsung Galaxy A3 2016 and debug it with ADB. Devices was unauthorized and no dialog popped up on the screen!
How I solved the problem:
I was interested will the problem still occure inside a Docker container? The answer was "No!".
Inside a Docker container, that I invoked with following: (this is the answer for this topic :) )
docker run --name android-adb -it -d --privileged -v /dev/bus/usb:/dev/bus/usb ubuntu:16.04 bash
I downloaded platform-tools (distribution with ADB). Added its path to environment. And restarted bash
After restarting ADB server inside Docker container AND killing ADB server on host machine. I found out that confirmation dialog appeared on the smartphone's screen!
So on the host machine the problem was in .android directory I think. Just move it to .android-backup. The next adb start-server command will create new one. Also I replaced platform-tools folder with most recent
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 am developing an Android application but fed-up of performance of My emulator
I do have a Android PC version installed in both VM-ware and Virtual Box
Can I use it as emulator? If so, how? How can I connect ADB to a virtual machine running PC Android?
I saw in some forum to use this but my VMs android having no specific IP.
How to i connect it??
Solution:
I would highly recommend to use Android x86 coz it many many times faster than Android emulator with Android x86 4.2 you can install and use any application with this and use "Google play" synch with your account as you do it with tablet
Working with latest Android X86 4.2 Jelly Bean and Virtual Box
I have found Different ways to connect with Internet and adb
Step: 1 Selection of Adapters
CASE 1: Only Internet {NAT Adapter}
The easiest solution is just use NAT adapter that will directly connect you to internet if host is connected to internet but you won't get the adb connection with this setup
Here you will get Public ip so you can't connect to Host computer
Case 2: Only adb {Host Only Adapter}
The easiest solution is just use Host Only Adapter
Note: The default Host Only adapter may not work due to DHCP server settings either create new HostOnlyAdapter or run DHCP server for existing Adapter()
Case 3: For both adb and Internet {Bridge Adapter}
You will have to take care in this case.
If you are using LAN for internet connection you shall use Bridge Adapter with your Ethernet card it will give you local ip and Virtual Machine will connect to Internet using host machine
Alternatively if you are with Wifi just do the same by selecting the Wifi adapter
For other type of connection you shall go with the same way
Step: 2 Connection with adb
to check the ip Address just press Alt+F1 {for console Window} [To switch back to Graphics view press Alt+F7 ]
you will see the console window type netcfg
it will show the ip address
Now move on to you host run command prompt move to adb directory
type
adb connect {your ip address}
Example
adb connect 192.168.1.51
Note: if adb is not running or responding you can do following
adb kill-server
adb start-server
you can check devices connected to adb
adb devices
As per your edited question, if you want to connect it to ADB you need to check what the IP of the VM is: Assuming you use VMWare's player;
Hit ALT-F1 in the VM and use the ifconfig command to know the IP address given to your network device (usually eth0). You can then hit ALT-F7 to go back to the Android UI.
Then, in your host PC, execute the adb connect [ANDROID_X86_IP] to connect the SDK debugger to your Android x86 VM; for example: adb connect 192.168.1.100:5555.
You should then see the list of devices connected and then it will be enabled to use for debugging.
ADB is typically located on your computer in a subfolder to your user folder in: ~/Android/Sdk/platform-tools. It is recommended to add it to your path so you can access it using the terminal wherever. I personally use this in ~/.bash_profile:
#add Android platform-tools directory
PATH=~/android-sdks/platform-tools:$PATH
export PATH
In case ADB fails the first time, you can try adb kill-server ; adb start-server to reset ADB.
If you want to read further, check out the Android-x86 website. It also has a lot of disc images available for download.
This is not a direct answer to your question, but did you see tricks to increase performance of emulator (read Why is the Android emulator so slow? How can we speed up the Android emulator?)
a) Use Intel Atom x86 instead of ARM
b) Use hw.gpu.enabled
I would say this makes emulator quite comparable in performance to a real device.
Update 1
Generally speaking, the idea is to configure Android PC to use tcp/ip for adb connection. And you may need to play around with network settings on VMWare or VirtualBox.
Useful links (which try to accomplish what you want
No network connection - Android-x86 on VMWare Fusion
http://lkubaski.wordpress.com/2012/08/15/running-android-on-vmware-player-with-networking-enabled/
http://www.transdroid.org/2011/01/26/techpost-debugging-against-a-virtual-machine-android/
install android x86 on vmware
run android terminal write command "ifconfig eth0"
windows user make Environment C:\Users\user- name\AppData\Local\Android\sdk\platform-tools
"adb connect IP address of the VM":5555
now run app on vmware
To save some time:
http://aztcs.org/meeting_notes/winhardsig/Android-vm/Android-VMwarePlayer-Win.pdf
Good link to get started in VMWARE
Where in the case you installed in the following directory /opt/android-sdk-linux
Set path variables
export ANDROID_HOME=/opt/android-sdk-linux
export PATH=${PATH}:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools
After performing path if you see the application from typing "android" in the console your path setting is good.
create a hello world application (get the sdk installed correctly here if you have questions)
Run the hello world app
Hit ALT-F1 in the VM and use the netcfg command to know the IP address given to your eth0 device. You can then hit ALT-F7 to go back to the Android UI. Then, in your host PC, execute the adb connect [ANDROID_X86_IP] to connect the SDK debugger to your Android x86 VM; for example: adb connect 192.168.1.100:5555. If problems bounce the service adb kill-server ; adb start-server
at console change directory to /android-sdks/platform-tools
then type
./adb connect 192.168.1.100
(note the 192.168.1.100 is what is required it defaults to port 5555)
I had to use the ./adb prefix and then I was configuring the VMWARE instance.
I feel allot less greasy avoiding XAML :-)
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.