Trying to use “adb connect” command to connect with an android emulator that is running on a different computer? I had similar scenario and had to spend few hours to make it work. Turned out, its surprisingly easy thing to do.
But why would anyone not launch the emulator on their own machine? Huh?
Well, there could be numerous reasons. Some of them are:
- Android emulator can consume huge amount of disk space. If you want to test your app on several device configurations, launching many emulators can easily fill up your hard disk (mostly c: drive) leaving very little space for operating system and other apps.
- Emulators are CPU intensive so if you have an average processor, launching one or more emulators can freeze up your system.
- Computer Memory (RAM) is yet another factor that can prevent smooth running if your system does not have a lot of it.
- Intel HAXM (hardware-assisted virtualization engine) is very effective in improving android emulator performance, however there are some cases when its not possible to install HAXM because of its hardware requirements.
So the solution?
You can use another machine specifically for launching android emulators and connect to those emulators from your development machine. This setup supports all adb commands so basically it can be used to:
- Remotely install and launch app on remote emulators either through adb or android studio.
- Remotely debug apps through android studio.
- Use Chrome remote debugging tools for debugging ionic apps or any webview/website.
- See debug/error messages from remote emulators through logcat.
So how everything works?
Android emulator by default listens on port 5555 so this command can be used to connect to it using adb:
adb connect localhost:5555
But here’s the catch, the emulator listens for local TCP connections only, which means it will not accept connections from remote computer over the network.
To explain this, lets say you have two machines one for development and another for emulators and they are accessible over network.
The development machine has IP 192.168.0.100 and the machine containing emulators has IP 192.168.0.112.
On the development machine, lets run this command:
adb connect 192.168.0.112:5555
Above command will generate a connection error like this:
This is because port 5555 on emulator machine accepts local or loopback connections only.
The MAGIC: Port Proxy/Forward.
To make this setup work, we just need to forward LAN’s port to local 5555 port. You can choose any port for incoming LAN connections, which should be forwarded to loopback’s 5555 port, on which emulator is already listening.
The port forwarding tool: Trivial Port Forward Command-line Tool
This tool can forward all request on one port to an another port on the same machine. Just run this command on the android emulator computer:
trivial_portforward.exe 1234 127.0.0.1 5555
Here 1234 is the port number where the development computer will connect. 127.0.0.1 is loopback address and 5555 is the emulator’s port.
After setting up the port forward we are all to connect the emulator from the development machine. Simple use adb connect just like we connect to a physical device. Here is the command:
adb connect 192.168.0.112:1234
If its not working, try turning off the firewalls on both the machines and see if it works. Firewalls can block connections to non standard ports so you may have to open the port through firewall.
Here is the result:
Now the remote emulator should be available to use through Android Studio, Chrome or adb. Go ahead and try for yourself. Feel free to comment if you’ve stuck somewhere or if you’ve some questions.
To view screen of android emulator machine from your development machine, you can use Teamviewer, Anydesk or simply Microsoft RDP.