Android Quickie: Accessing Local Resources with "adb reverse"
Note: This was originally published in 2016, but the features still work today.
Our mobile backend can run locally on our workstations, but it has to access resources within the company firewall. And because of Bank Grade Security™ we can’t have our mobile devices on the same network as our workstation, so we can’t directly hit this service on our workstation from a device.
But sometimes you’re debugging device specific issues, or you want to run in an environment with Google Play Services, so an emulator won’t cut it.
So what’s a developer to do?
Thankfully Android 5.0+ has added a reverse port forwarding feature to the Android Debug Bridge.
Say our backend runs at localhost:8200 on our workstation. Simply fire up a terminal, and enter:
adb reverse tcp:8200 tcp:8200
Now within your mobile app, you can just point it at a local backend of http://localhost:8200 and it’ll hit the service on your workstation. I recommend using a build flavor or dependency injection to be able to easily define the backend URL, but that’s up to you :)
When you’re done and you want to remove the port mapping, run
adb reverse --remove-all
Having worked with both flaky mobile VPNs and also locked down security profiles, this new feature in API 21+ makes my life incredibly easier!