Remote debugging with Android emulator

Viewed 59028

Is it possible to write the code/compile Android application on one machine and debug it remotely on the emulator launched on another? I'm sick and tired of the emulator constantly eating half of my laptop's CPU.

10 Answers

I haven't previously tried (or even noticed) the adb connect command that cmb mentioned, but I can confirm that forwarding the TCP ports yourself — such as over SSH — works fine.

The emulator listens on two TCP ports per instance: 5554 for the telnet interface and 5555 for control communication with tools like DDMS. So you could probably get away with only forwarding port 5555 (though I've only tried it so far with both). Each subsequent emulator takes the next available even+odd port number tuple (up to around 5580, I think).

For reference, I did the following steps on my local machine:

  • ssh -NL 5554:localhost:5554 -L 5555:localhost:5555 myuser@remote-server
  • killall adb; adb devices

I believe the emulator tries to notify a local adb server at startup; hence the need to restart adb in order for it to probe the local 5554+ ports.

Note that the localhost in the ssh command refers to the local interface of the remote machine.

adb devices showed a new emulator — emulator-5554 — and I could use it as if it were running on my local machine.

None of the proposed solutions worked for me. I've started from Emirikol's solution and refined it, as with the new Android API > 21 the emulator was appearing offline and I had to go to Genymotion settings and leave Android SDK path empty. And from command line:

netsh interface portproxy add v4tov4 listenport=5555 connectport=5555 connectaddress=<emulatorIP>

netsh interface portproxy add v4tov4 listenport=5554 connectport=5554 connectaddress=<emulatorIP>

source:http://www.sarpex.co.uk/index.php/2016/10/02/connect-genymotion-emulator-remotely/ Disclaimer, I'm the author.

When you run adb, it starts a server copy of itself if one isn't already running. You can start that copy yourself on the machine with the device and since sdk 4.3 you can give it the -a option to tell that server to listen for remote machines. Do that with the following command which doesn't exit:

adb -a -P 5037 server nodaemon

On the machine you want to use the device from, set ADB_SERVER_SOCKET to tcp:xxxx:5037 in an environment variable (or give the same value to each adb invocation with the -L option), where xxxx is the IP address or hostname of the machine with the devices, and 5037 matches the port you gave the in the command above.

We use this to give access to about 100 emulators spread over 3 machines to a machine running end to end tests in parallel, and to developers wanting to share real devices remotely.

You can forward ports to and from the emulator with adb forward and adb reverse, and they'll appear on the machine with the devices (not the machine you're running 'adb forward' from).

This post contains many answers but step by step instructions are missing.

So this answer will explain how to connect 2 computers to run projects of one computer on the emulators of another computer.


Requirements before going further:

  1. Firewall should be off in both the PCs.
  2. Both PCs must be connected in the same network.

Notes:

  • Computer running Android Studio is mentioned as "PC_AS"
  • Computer running Emulator is mentioned as "PC_EM"

Step 1:

On the computer running Android Studio, open a terminal window and run following command.

ssh -NL 5554:localhost:5554 -L 5555:localhost:5555 dhaval@192.168.0.104
  • Above command will forward 5554 and 5555 ports of PC_AS to the same ports on PC_EM. So the emulators running on PC_EM can be detected on the PC_AS.
  • dhaval@192.168.0.104 is the address of the PC_EM. Format of the address is username@local_ip_address

Once you run above command, the terminal window will not show anything if the command is executed successfully. It will look like nothing is going on but the process is running there.

|  Note: Do not close this terminal until you want to stop remote debugging.


Step 2:

Now open another terminal window on PC_AS and run following commands one by one.

  1. killall adb;adb devices This command will stop the adb and stop the server.
  2. adb start-server This command will start the adb server.
  3. adb devices This command will list down all the connected adb devices.

Once you run above commands, you will see that emulators of PC_EM are now detected in the PC_AS. Now you can run the projects on those emulators and debug remotely.

|  Note: While doing the above process, emulators can show a dialog to trust the incoming request.

Android emulators by default listens on local port 5555, so one way to connect to a remote emulator is by using a port forwarding tool to forward all LAN packets to local 5555 port.

One such excellent tool is Trivial Port Forward

Here is the command:

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.

For more detailed example, see my blog post.

Related