P.S. if you're in a hurry, find the correct solution here
I was working on a full stack side project a few months ago, and I wanted to make API requests from my android app to my desktop, and for some reason localhost:8000
wasn't simply accessible by my phone.
Well, understandable, I know that every device's localhost is independent and cannot be accessed by your home network (your Wi-Fi, for example). So the localhost on my laptop won't be able to access the localhost on my phone.
So, I asked Google for help. And I got a large number of solutions, the most sensible one being "use the internal IP address of your PC", et voilΓ .
The Bad way
Running ip addr
on my machine tells me that the laptop's internal IP is 192.168.29.76
.
And sure enough, as long as both devices were using the same Wi-Fi network, accessing http://192.168.29.76:8000
instead of http://localhost:8000
did work. My Android app can now make web requests to my local backend server π
But this solution is... a bit unstable.
The internal IP of your laptop can keep changing whenever it connects to Wi-Fi, depending on various factors. And everytime it changes, you have to change the URL in your app's code, which is not ideal.
There's other ways as well like using ngrok, but it faces similar issues.
The Correct, easy way
use adb reverse
.
Yup, that's it.
Connect your android device to your pc via USB, ensure you have adb setup, and run this in your terminal:
adb reverse tcp:8000 tcp:8000
Now, your mobile can access localhost:8000
, just like your PC. (you can replace 8000
with whichever port you want to forward)
Why did nobody tell me this?
Yeah, I was also surprised when I was unable to find anyone on Google or StackOverflow mentioning the existence of adb reverse
when I tried to look for it.
Which is why I wrote this blog. Now hopefully, adb reverse
will become more popular.
If you know why adb reverse
isn't as popular, let me know. Also, if you know another android developer that should know about this little productivity hack, why not share this blog with them? :P
Cover image courtesy of Fotis Fotopoulos on Unsplash
Top comments (11)
amazing
Thank you
Wow, great stuff! Trying looking for a similar solution for a while and ended up a public forwarding service like ngrok for a while, but that comes with its own limitations.
Learned something new! Great post :)
you're welcome (;
Nice i only knew about adb forward, but it makes a lot of sense to also have adb reverse.
Ikr, i got to know about adb reverse because I read about adb forward on some obscure stackoverflow answer, and I wondered if a reverse port forwarding also exists, so I blindly tried adb reverse. And it worked.
Great tip! Thanks
:D
Do you know anything like? connecting smartTV/androind using ADB, like so i can forward my local host web server to TV to see my live changes
Instead of this may try to use DHCP IP reservation featuron on your home Wi-Fi router so that IP will be same every time device connects toWI-Fi. I am using this feature for my RaspberryPi.