50

My scenario is this: I telework from home and log into my work machine via Windows Remote Desktop. I use Eclipse as my development environment for Android apps on my work computer.

Using ADB, I would like to be able to deploy apps from my work computer to a device on my home network, for scenarios where the emulator doesn't do the app justice.

I found this post, which discuss a very similar scenario, with the exception of deploying to an emulator running on a local PC, instead of deploying to a local device:

http://www.41post.com/5003/programming/android-adb-remote-emulator-access

I'm trying to take the same steps, but figure out how to target a local device on my home WiFi network & tethered to my local home PC, instead of the local emulator.

Right now, I have the remote PC set up to try and connect to my public router IP on port 5585 - but in my router, what IP/port do I forward this to to connect to the local device?

In the example using the emulator, they forward to the local PC address where the emulator is running and port 5555, and adb is not running on the local PC. I have configured my router to forward to my device IP, with the device on WiFi, as well as my local PC IP where the device is tethered.

However in both scenarios, when I try adb connect <routerIP>:5585 on my remote PC, it gives me an error unable to connect to <routerIP>:5585:5585. I get the same response when trying to forward to/listen to other ports. I'm not getting any security errors in the router log, so it appears the port forwarding is working.

Questions:

  • What local IP/port number should I forward to when configuring port forwarding on my local network to connect to the local device using the remote adb instance?
  • Should I be targeting the local PC IP that the device is tethered to, or the local device IP?
  • If I target the local device IP, what port number should I forward to?
  • Do I need adb running on my local PC?
e-sushi
  • 13,786
  • 10
  • 38
  • 57
Sean Barbeau
  • 11,496
  • 8
  • 58
  • 111
  • 1
    Any reason why you don't copy the apk to your local machine and just deploy it from there? – Abdullah Jibaly Nov 07 '12 at 21:48
  • FYI, it seems that adb connect is supposed to target the IP of the actual Android device based on what I see here: http://stackoverflow.com/questions/2604727/how-can-i-connect-to-android-with-adb-over-tcp – Abdullah Jibaly Nov 07 '12 at 21:54
  • @AbdullahJibaly Copying the APK locally is an option, but just not a very efficient one in the iterative development process. I'd prefer to use ADB directly, which cuts a lot of the steps in between compiling and the app appearing on the device. – Sean Barbeau Nov 07 '12 at 22:23
  • @AbdullahJibaly There isn't an approved answer on the post you list, so I'm not sure what to make of it. Also, these responses also seem to require root - I'd like to do this on a stock device. When I try 'adb tcpip 5555' to redirect, it just hangs at the 'redirecting' message and doesn't return to the prompt. – Sean Barbeau Nov 08 '12 at 15:24

7 Answers7

51

I had a similar situation. I work on a remote desktop for development but my android device is connected to my local laptop. I wanted to be able to use adb and the android plugin in eclipse on the remote desktop and connect to the device attached to my laptop. After searching on the internet and not finding anything that really helped, I decided to write a port forwarder that would do the trick. You can find it here. I hope it will be helpful to other people as well.

Guy Chauliac
  • 640
  • 7
  • 8
  • Very nice! I got this working using Eclipse Juno on the machine I'm remote-desktopped into, and with the device (Samsung Galaxy S3) plugged into my local machine. I was able to see the Logcat output via DDMS from the device on the remote desktop machine, execute adb commands from the commandline, and deploy an app. Very smooth and much better than trying to shuffle APKs between machines. fyi - I am getting a ton of "socket closed" errors in both command line consoles, and "An established connection was aborted by the software in the host machine" in Eclipse console. But, it still works. – Sean Barbeau Mar 27 '13 at 20:31
  • Just uploaded a new version which does not display the socket closed errors. It's [here](http://code.google.com/p/adbportforward/downloads/detail?name=adbportforwardV2.jar) – Guy Chauliac Apr 16 '13 at 11:43
  • I was pretty excited to come across this, but trying it out, I see the following whenever I try to do anything over adb (like adb shell): Remote machine: 15941 [pool-1-thread-1] DEBUG chabernac.portforward.PortForward - Socket accepted 5037-->10.0.160.162:6037 Machine with device: 8874466 [pool-2-thread-1] DEBUG chabernac.portforward.PortForward - Socket accepted 6037-->127.0.0.1:5037 And ADB tells me "error: device not found" – kcstrom May 20 '13 at 21:12
  • I figured out that my above issue was because I didn't have my device setup properly to connect over adb shell normally. It working now! Thanks! – kcstrom May 20 '13 at 21:24
  • Works like a charm! Thanks for this handy tool. I tried several USB-RDP-Forwarder, but they were just too slow, but that one works even via Internet just great! – TSGames Aug 10 '13 at 19:47
  • 3
    Project has moved to bitbucket. You can find it at: https://bitbucket.org/chabernac/adbportforward/wiki/Home – Guy Chauliac Apr 10 '14 at 14:49
  • 1
    I followed what you've suggested at above bitbucket link and my connection is all set and done but as soon as I run adb commands for e.g. `adb devices` i get following error : `167838 [pool-1-thread-2] ERROR chabernac.portforward.PortForward - An error occ ured in setting up connection to 10.94.14.132:6037 java.net.ConnectException: Connection refused: connect at java.net.TwoStacksPlainSocketImpl.socketConnect(Native Method) at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source) at java.net.AbstractPlainSocketImpl.connectToAddress.............` – roger_that Jun 06 '14 at 08:04
  • 2
    @GuyChauliac, great! worked very smoothly. but some systems are unable to start acting as a client, they can ping the server successfully, can act as a server, but when I fire command for Client, it gets stuck after "Android ADB client started: true" then no updation even when the server is in ready state. :( – jQueen Jul 22 '14 at 11:52
  • Can we achieve this using port forward command in adb? In Server and client both? – jQueen Jul 22 '14 at 11:58
  • 1
    I code *inside* a Linux VM, so I can't run a HAXM emulator on the guest OS, but with this workaround I can deploy to a HAXM emulator running on the host! Thanks for this! – Vicky Chijwani Oct 28 '14 at 17:08
  • Work fine, but i have problens in server machine, path not support space e.g: ```C:\adb file\``` < not work but ```C:\adbfile\``` < work. =( You can update the script @GuyChauliac ? – Florida Aug 09 '15 at 02:54
  • Works with Android Studio as well. @GuyChauliac Thanks. – UpLate Jan 18 '16 at 02:16
  • Works perfectly with a machine connected via Windows Remote Desktop. Thanks a lot for making this! – Jim C Mar 21 '16 at 15:41
  • Thanks for the tool! mb worth to mention, both adb should be the same versions – Павел Dec 17 '16 at 22:49
  • Thanks. Worked successfully for me, with adb server running on Windows 10 host, and adb client running on Ubuntu VM. – Alex Che Jul 09 '18 at 13:11
  • this works, but i cannot get the Flutter Debugger from vscode to connect to the device. I can launch the App, but i cannot debug it probably because the debugger wil try to connect to localhost – Marijn Dec 21 '20 at 13:44
  • This is a great solution. After 4-5 days of struggle, stumbled upon this solution. Able to implement [Moblile]<--(USB cable)-->[Windows 10] <------Intranet----->[Mac with Android Studio] and it worked. Now shall attempt over Internet. Thanks a lot @ Guy Chauliac – Sriram Nadiminti Jun 10 '21 at 15:18
  • ssh already provides the port forwarding. why would we need yet another solution for this when adb is involved? – Slava May 11 '22 at 12:58
26

Beginning Android 4.3 you can:

  1. Make adb server listen on all interfaces. You have two options:
    • Make gListen=1 and recompile adb (I have compiled it on Linux-x64 machine for you and put it here)
    • Start adb server with -a parameter: adb -a -P 5037 fork-server server&
  2. Use adb on your remote machine with extra parameter, e.g. adb -H <remote_host> shell
Roman Saveljev
  • 2,544
  • 1
  • 21
  • 20
  • 2
    Thanks for the alternate approach! To clarify - does this require that the Android device be 4.3 or higher? Or does this require the SDK tools released with Android 4.3? – Sean Barbeau Sep 18 '13 at 20:01
  • I just tested it and it works fine with a 2.3 Gingerbread device. – kar Sep 23 '14 at 08:17
  • This works, but it is very slow to push files to the emulator (adb install is very slow, for example). This is not a network or SSH problem since rsync is working fine. Any tips? – amfcosta Mar 08 '15 at 20:56
  • Could someone please provide an example of how to make it work using option 2? – AlexIIP Nov 26 '15 at 06:21
  • Excellent answer, exactly what i'm looking for. The end '&' can be delete. – Swing Jan 28 '16 at 02:45
13

Another setup for remote host + local device testing. This will be useful for lots of people working from home on a laptop, connected to their development host machine still in the office. Note that I assume both devhost/laptop are both running Unix, but other OSes will be able to run the commands on the command prompt/shell.

# Kill old adb server.
devhost$ adb kill-server 

# Activate adb server on client
laptop$ adb start-server

# Start ssh tunnel. Hide/minimize this window not to close it by accident
laptop$ ssh -XC -R 5037:localhost:5037 <your devhost machine>

# Should work by now with the local device connected to the laptop
devhost$ adb logcat 
Jindor
  • 420
  • 3
  • 10
  • 1
    This is by far the best solution – distante Aug 13 '20 at 08:36
  • in my case, when using QT Creator, it additionally attempts to use port 5039 and once the app gets deployed (it does) the mobile keeps waiting for debugger on port 8100, ideas? – Vega4 Jan 24 '21 at 18:12
  • amazing. confirmed that this works with windows wsl as well – Jun Mar 16 '21 at 10:54
  • It works, I can connect with "adb shell" but how to bring them into AndroidStudio device dropdown list and be able to debug on it ? – hannes ach Feb 25 '22 at 11:56
  • The only thing is that `-XC` parameters to ssh seem to be unrelated to the solution. – Slava May 11 '22 at 12:06
5

You can solve the issue by port forwarding.

  • Download Secure Shell app from the Chrome app store
  • Connect to your machine (step-by-step setup)
  • In this connection, disable adb server: adb kill-server
  • Create a new port forwarding connection (same as a regular connection, but set the SSH Arguments field to: -N -R 5037:localhost:5037)
  • On your laptop, open up a terminal and enable adb server: adb start-server
  • Very helpful instructions. But note that this work only if adb versions on local and remote machines are the same – Ajay Feb 26 '16 at 04:42
5

Probably there is a simpler solution, providing the device, the local and remote machine belong to the same network. Let's say your device has a certain IP over the network and let's say you decide to use your preferred PORT: well, you can do the following steps.

On the machine where the device is plugged please run:

adb devices

adb tcpip <PORT>

Example of PORT is 5555.

On the remote machine you need to deactivate 'Discover USB devices', 'Discover network targets' and 'Port forwarding' and then run:

adb connect IP:PORT

*IP is your android device IP(not the first machine IP) which can get from adb shell ip -f inet addr

And you are ready to debug on remote machine.

林果皞
  • 7,539
  • 3
  • 55
  • 70
Pakkes
  • 51
  • 1
  • 2
5

This is how I made it work from host macOS with emulator to macOS client.

A: One line command

On host of emulator

 socat tcp-l:5560 tcp:localhost:5559

On client

 adb connect <IP>:5560

B: With a tunnel

on host

host$ adb kill-server
host$ adb -a nodaemon server

on client

client$ adb kill-server
client$ ssh -L 5037:localhost:5037 <host-IP>

open second shell on client

client$ adb kill-server  # I observe first it kills client adb
client$ adb kill-server  # then it kills server adb, do it maybe once more
client$ adb devices      # show devices on server now

Now I see host emulator in Android Studio as well

enter image description here

hannes ach
  • 16,247
  • 7
  • 61
  • 84
2

My situation required using a VM that is on a different network, but that I rmd into (an Azure VM). The VM and my local laptop are both running Windows 10. First, I had to install USB Redirector RDP Edition on my local machine (costs $80, but there might be free alternatives), then install the Google Android USB driver on the VM and the Universal Adb Driver on the VM. I'm now able to load an Android Studio App the project in Android Studio on the VM, connect an Android device on my laptop, and debug the app on the device.

Demitrian
  • 3,200
  • 1
  • 24
  • 41
Bugmonster
  • 21
  • 1