ADB Tips & Running Bonjour Services with an Android Emulator


One notable difference when developing application with an Android emulator is the fact that it runs inside of a VM and the host machine (usually your laptop) has limited access to it.

In this post, you will get to use several commands to make that process easier:

  • adb forward to make a port on the emulator accessible from the host.
  • adb reverse to make a port of the host reachable from the application running in the emulator.
  • dns-sd to register a new Bonjour service that will map to your Android emulator.

ADB Tips

List all devices:

$ adb devices

List of devices attached
ZX1G227H75	device
emulator-5554	device

Port forward for a specific device:

$ adb -s emulator-5554 forward tcp:5984 tcp:5001

You can now access the WebServer (in this case Couchbase Lite Listener Android) from the browser of the host:

Next, you can run the same application on another real device and forward to another port on the host:

$ adb -s ZX1G227H75 forward tcp:5984 tcp:5000

That makes both listener accessible from the host but what about the mapping the ports so that the emulator can directly access the device. In this scenario, if the application running in the emulator makes requests to http://localhost:5001 it should be routed to the device. Use the reverse command to add this reverse mapping:

$ adb -s emulator-5554 reverse tcp:5000 tcp:5000

Now, you can start a replication from the emulator to the device with localhost as a hostname and pre-defined ports.

curl -H 'Content-Type: application/json' \
			-X POST 'http://localhost:5000/_replicate' \
			-d '{"source": "http://localhost:5001/db", "target": "db", "continuous": true}'

Note: To stop the replication, include {"cancel": true} in the request body.

$ adb -s 12ecea49 forward tcp:5000 tcp:5984
$ adb -s emulator-5554 forward tcp:5001 tcp:5984
$ adb -s ZX1G227H75 forward tcp:5002 tcp:5984

DNS Discovery Tips

Suppose that you’re using JmDNS or Android NSD to discover the hostname of devices running the same application and on the same local network. If you broadcast a service in the Android simulator, the IP will resolve to and of course this will not resolve on other devices. So, you can combine port forwarding and use dns-sd to broadcast another service that resolves to the hostname of the host machine.

The adb forward always binds the port on localhost and Interestingly enough, it won’t work on the en0 interface. So, if the computer internal IP is it turns out that won’t forward to the emulator. Binding to another interface can be done with pcftl on Mac (roughly the same instructions taken from here and here).

Then, http://internal-IP:5000 will display the Couchbase Lite welcome message as shown below:

Next, register a new service on your machine:

$ dns-sd -R jamiltz sampleapp._tcp. local 5000 localhost

Uninstall applications with ADB

Paste the following in a new file called

adb devices | while read line
    if [ ! "$line" = "" ] && [ `echo $line | awk '{print $2}'` = "device" ]
        device=`echo $line | awk '{print $1}'`
        echo "$device $@ ..."
        adb -s $device $@

From the command line, run:

$ uninstall packagename

Other blog posts

comments powered by Disqus