wayne piekarski Using ADB from a remote build machine with a local Android device over SSH

 
Share Blog article posted November 2015

The ADB debugging tool is normally used to communicate with Android devices connected to your local machine. However, there are times when you are using a powerful remote build server via SSH. Instead of constantly copying the APK file manually to your laptop and using adb install, you can use an SSH tunnel and automatically forward the ports to make it look like the device is connected locally. This works because adb is actually two processes, a client, and a server, and they use a local port 5037 to communicate.

The first step is to make sure you have the same version of Android SDK Platform-tools installed on both machines. This is important otherwise you might get the error message

adb server version (32) doesn't match this client (34); killing...
Once you have installed this, make sure it is the only version in your $PATH. You might have other versions that are in the $PATH as well, and these might take precendence.

Check the versions on both machines, you should get the same output on both:

adb --version
  Android Debug Bridge version 1.0.32
  Revision eac51f2bb6a8-android
If the output is not the same, you need to ensure you are running the version you downloaded earlier. You can check with a command like this:
which adb
/Users/wayne/android-sdk/platform-tools/adb
You need to watch out here, because when ADB goes to launch the server, it might be using a different $PATH than the which command. You can use which -a to see what other adb binaries might be present.

Kill off any adb instances on both the local and remote machines, since old ones will interfere, using one of these commands:

adb kill-server
killall adb
Also make sure you are not running Android Studio on either machine, because it will automatically try to restart adb and it might restart a different version. Once you have the tunnel working, you can then start up Android Studio safely.

On the local machine with the Android device connected, run the following commands and substitute the hostname of your build server:

adb devices
  List of devices attached
  ABCDEF12345
ssh -R 5037:localhost:5037 remote-build-machine.hostname.com

On the remote machine, you can now use any shell to access ADB. Make sure you do this after the SSH command is run, or it will start up a local adb server and not work.

adb devices
  List of devices attached
  ABCDEF12345

Both machines can now run adb commands and everything will be redirected to the local phone. If you want to invert the situation, where you have an Android device connected to a remote machine, and you want to access it via a local machine, you need to use SSH differently:

ssh -L 5037:localhost:5037 remote-build-machine.hostname.com
Note that -R sets up a redirection from a remote machine, whereas -L sets up the inverse from a local machine.


Share Blog article posted November 2015


Google Developer Advocate 2014-2023


X-Plane plugins and apps for flight simulation


IoT water meter monitoring


IoT computer vision monitoring


Tiny and cheap offline Wikipedia project 2017


Outdoor augmented reality research
Tinmith 1998-2007


Outdoor augmented reality 3D modelling
Tinmith 1998-2007


Outdoor augmented reality gaming
ARQuake 1999-2007


Scanned physical objects outdoors
Hand of God 3D 2006


Google Developer Advocate 2014-2023


X-Plane plugins and apps for flight simulation


IoT water meter monitoring


IoT computer vision monitoring


Tiny and cheap offline Wikipedia project 2017


Outdoor augmented reality research
Tinmith 1998-2007


Outdoor augmented reality 3D modelling
Tinmith 1998-2007


Outdoor augmented reality gaming
ARQuake 1999-2007


Scanned physical objects outdoors
Hand of God 3D 2006


Contact Wayne Piekarski via email wayne AT tinmith.net for more information

Last Updated 2023