$ cat tips/adb-reverse-localhost-android.md

Open localhost on an Android phone: adb reverse

On the phone, localhost is the phone itself, so the dev server on your computer does not answer at that address. adb reverse puts the computer's port on the phone, and the page opens in Chrome at http://localhost:8018/ with no change to the server.

The phone has to be connected to adb, by cable or over Wi-Fi. Then it is one command:

adb reverse tcp:8018 tcp:8018

The first port is on the phone, the second on the computer. From then on http://localhost:8018/ in Chrome on the phone reaches the server on the computer.

My dev server runs in Docker and listens on 127.0.0.1 only. For the phone to reach it by the computer’s address on the Wi-Fi network, I would have to open the port to the network and add that address to Django’s ALLOWED_HOSTS. Through adb reverse the phone asks for localhost, so I change nothing in the server.

The page can also be opened on the phone by a command from the computer:

adb shell am start -a android.intent.action.VIEW -d http://localhost:8018/ com.android.chrome

My phone was connected over Wi-Fi. That is how I checked interface changes on it before they went to production.

checked: Pixel 9 Pro, Chrome on Android, Ubuntu 22.04, platform-tools 37.0.1

$ ls ./blog/tips
all tips →
↑