Port forwarding for Android app development

Port forwarding for Android app development

Having a server that an Android app needs to connect to is quite common in the app developing world. The tools and libraries to do so are mature and there is an abundance of resources to learn how to do that.

But until you end up with a deployed server that is globally accessible you would probably need to do some local development. In my case, I am developing the server on my machine and the app on an actual test device.

What happens when you try to connect from the app on the test device to the development machine where the local server is running? Obviously the localhost for the test device is not pointing to the development machine.

The ADB way

You can tell ADB to forward a test device port to a development machine port.

adb reverse tcp:8085 tcp:8080

That's it. Any call to 127.0.0.1:8085 from your Android test device will be forwarded to the development machine's port 8080. Just keep your local running server running to reply to the connection request.

These redirections are active as long as the device is connected. When you disconnect your device you would need to run these commands again. In case you want to remove either one or all the reverse redirections, or list the active redirections, run respectively:

adb reverse --remove tcp:8085
adb reverse --remove-all
adb reverse --list

The Chome way

If you are using Chrome, there's another way to set up port redirection without using the command line. The feature is designed for testing how websites that are served from the development machine look on mobile Chrome, but it can work for Android apps as well.

Firstly, set up Chrome to be able to access your Android test device.

Then navigate to chrome://inspect/#devices. You will see a list of your connected test devices. Click on Port forward and enter the port on the test device you want to forward to the development machine (doc).

Any call to 127.0.0.1:8085 from your Android test device will be forwarded to the development machine's port 8080.

The port forward is active as long as chrome://inspect/#devices tab is open. Note that you might need to launch Chrome on the test device to initially activate the port forwarding.

Conveniently, the forwarding settings are retained when you reopen the tab. So just save a bookmark for this page and launch it when you need the port forwarding functionality.

Happy local client-server development!

Show Comments