How to let two apps running on different computer inside android emulator connect

One of my android application listens to incoming connections from a desktop application which then sends data to it. The desktop application client only runs on a macOS or a windows. The android application during my software development or debugging runs inside emulator on my development machine which is Ubuntu. So how do I test my android application and the client without changing my development machine OS/Ubuntu. basically you need SSH on both machines to make it work. With SSJ we can open a tunnel between the two computers.

ssh -NL 4444:localhost:8000 ahmed@myzbook.comCode language: CSS (css)

I run the above command on my macOS where the desktop application is running . This will create a tunnel between macOS and Ubuntu dev machine. Now my desktop application client connects to 127.0.0.1 port 4444 and this connection goes and hits 127.0.0.1:8000 on Ubuntu machine. But this is not yet enough. We need to forward port 127.0.0.1:8000 to emulator-IP:port-android-app-is-listening-on/ for that.

adb forward tcp:8000 tcp:4000Code language: CSS (css)

Assuming my android app in emulator on Ubuntu is listing on port 4000. the above command will redirect 127.0.0.1:8000 to emulator:4000. You may also use socat tool instead of adb

Use android emulator running on a different machine for debugging

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).

Run the avd using emulator or android studio (AS) on the remote machine where only emulator and avd is needed. AS is only optional

./emulator -avd avd-name -accel on -gpu on

Run the below command on the machine where you have android studio and you are coding and debugging. Before this command,

ssh -NL 5554:localhost:5554 -L 5555:localhost:5555 myuser@remote-serverCode language: CSS (css)

Then kill the local adb-server , so that it restarts and starts to see open ports 5554 and 5555 on local host and assume that this is a local emulator. After that you can list adb devices to see if new emulator is visible, otherwise AS will itself show the new devices in the devices section of the IDE

adb kill-server<code>killall adb</code><code>adb device</code>Code language: HTML, XML (xml)

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

References

https://stackoverflow.com/questions/1754162/remote-debugging-with-android-emulator