🚀 Mac in Practice: Exposing an Android Emulator's ADB Port to the LAN

1 min read

When doing Android remote debugging or device-farm development, we often need other computers on the LAN (or a CI/CD server) to connect to an Android emulator running on a Mac.

🚀 Mac in Practice: Exposing an Android Emulator's ADB Port to the LAN

When doing Android remote debugging or device-farm development, we often need other computers on the LAN (or a CI/CD server) to connect to an Android emulator running on a Mac.

Because the emulator listens only on the local loopback address (127.0.0.1) by default, the LAN cannot reach it directly. This article solves the problem with adb forward combined with the socat port-forwarding tool.


🛠️ Step 1: Prepare the Environment and Start the Emulator

1. Install Android Studio

Go to Google’s official site to download and install Android Studio for Mac.

2. Create and Start an Emulator

Open Android Studio -> Device Manager, create a new virtual device (AVD), and start it. Make sure the emulator boots up normally to the home screen.

3. Install the Network Forwarding Tool Socat

We need socat to bridge LAN IP to 127.0.0.1. Install it with Homebrew:

brew install socat

⚙️ Step 2: Configure ADB Port Mapping

We first map a local port on the Mac to the emulator’s internal port, and then map a LAN port to that local port on the Mac.

1. Check the Device Connection

Make sure ADB can see the emulator:

adb devices
# Example output:
# List of devices attached
# emulator-5554	device

2. Enable Remote Debugging Listening (Optional)

Although the emulator usually enables this by default, running this command ensures the service is ready on port 5555:

adb tcpip 5555

3. Set Up Local Port Forwarding (Host -> Emulator)

Forward traffic from local port 1555 on the Mac to port 5555 on the emulator:

adb forward tcp:1555 tcp:5555

At this point you can already connect to the emulator via adb connect 127.0.0.1:1555, but only from this machine.


🌐 Step 3: Enable LAN Forwarding (LAN -> Host)

Now use socat to forward a port on the LAN IP to the 127.0.0.1:1555 you just configured.

1. Get the LAN IP

Run ifconfig | grep inet in the terminal to find your LAN IP (assume 10.0.8.2).

2. Start the Socat Forwarder

# Syntax: socat TCP-LISTEN:[LAN port],bind=[LAN IP],fork TCP:127.0.0.1:[local mapped port]
socat TCP-LISTEN:1555,bind=10.0.8.2,fork TCP:127.0.0.1:1555

Note: after running this command the terminal will appear to hang (it is listening). Do not close the window, or use nohup to run it in the background.


🤖 Step 4: Keep-Alive Automation Script

ADB forward rules sometimes break when the emulator restarts or the ADB service resets. We can use the following script to detect this and reconnect automatically.

The improved script (auto_forward.sh):

#!/bin/bash

# Configuration
ADB_PATH="$HOME/Library/Android/sdk/platform-tools/adb"  # ADB path, adjust as needed
LOCAL_PORT=1555
EMULATOR_PORT=5555

# Check whether ADB exists
if [ ! -f "$ADB_PATH" ]; then
    echo "❌ Error: adb not found, please check the path: $ADB_PATH"
    # If it is already on your PATH, try using the adb command directly
    ADB_PATH="adb"
fi

echo "🚀 Starting to monitor ADB port forwarding..."

# 1. Check whether a device is connected
if ! "$ADB_PATH" devices | grep -q "device"; then
    echo "⚠️  Error: no online Android device detected, please start the emulator first."
    exit 1
fi

# 2. Enter the infinite monitoring loop
while true; do
    # Check whether the forward rule exists
    # Example `adb forward --list` output: emulator-5554 tcp:1555 tcp:5555
    if ! "$ADB_PATH" forward --list | grep -q "tcp:$LOCAL_PORT tcp:$EMULATOR_PORT"; then
        echo "🔄 [$(date +%T)] Forward is missing or broken, reconnecting..."
  
        # Try to establish the forward
        if "$ADB_PATH" forward tcp:$LOCAL_PORT tcp:$EMULATOR_PORT; then
            echo "✅ [$(date +%T)] Forward established: 127.0.0.1:$LOCAL_PORT -> Emulator:$EMULATOR_PORT"
        else
            echo "❌ [$(date +%T)] Forward failed, check for port conflicts or device status."
        fi
    else
        # Optional: comment out the next line if you don't want frequent log output
        # echo "✅ [$(date +%T)] Forward status OK"
        : 
    fi
  
    sleep 10  # Check every 10 seconds
done

How to Use:

  1. Save it as auto_forward.sh.
  2. Make it executable: chmod +x auto_forward.sh.
  3. Run the script: ./auto_forward.sh.

✅ Verify the Connection

Now, on another computer on the LAN, run the following command to connect to your Mac’s emulator:

# Assume the Mac's IP is 10.0.8.2
adb connect 10.0.8.2:1555

If the connection succeeds, you can debug it just like a local device!

Comments