Connect to your Android emulator running on mac from your windows VM

Normally I’m developing on a mac with Xamarin Studio. Debugging an Android project on the Android emulator running on your mac is easy, because it just works. But now and the I need to do some work from my windows machine. My windows is running as a VM with Parallels Desktop.

Visual Studio isn’t recognising the Android emulator running on my mac. I’m ok with that because the mac and windows are two different machines. Now, there is a way to debug your Android app on the emulator running on mac from your visual studio running on windows (if you don’t lost me now, keep reading because doing so isn’t that hard).

Find the IP address of your mac

Open up a terminal application and search for your ip address:

# full list command
ifconfig
# or the first network adapter command
ifconfig en0

# results in
...
inet 192.168.0.2 netmask 0xffffff00 broadcast 192.168.0.255
...

Connect the emulator from Windows

Normally it should be possible to connect to an emulator via the adb command. On your windows machine open up an Android Adb Command Prompt and try to connect with the command:

adb connect the-ip-address-of-your-mac:5555

# so in my case
adb connect 192.168.0.2:5555

I was a bit surprised to see the following error:

unable to connect to 192.168.0.2:5555: cannot connect to 192.168.0.2:5555: No connection could be made because the target machine actively refused it. (10061)

Even with the firewall on the mac disabled I got the same result. After some googling I wasn’t surprised to find a solution on the Xamarin website itself. The full article can be found on https://developer.xamarin.com/guides/android/troubleshooting/questions/connect-android-emulator-mac-windows/

To make it easy, I’ll give you the short version:

Step 1

Kill the adb server on your mac

adb kill-server

Step 2

forward inbound TCP packets to the loopback interface and outbound packages back the other way

cd /tmp
mkfifo backpipe
nc -kl 5555 0<backpipe | nc 127.0.0.1 5555 > backpipe

That’s it, as long as you leave that command open, you can now connect from your Windows machine to your Android emulator running on the mac. And when the firewall of your mac is enabled you’ll get a pop-up to ask if it’s ok to change its settings.