Some times we develop Android on a remote Linux machine (or WSL). We need to use adb in remote machine and connect to the local Android device.
Here is how we can achieve this:
- Connect physical devices to / run emulators on local machine.
- Run adb in nodaemon server mode on local machine.
- Create a TCP port forwarding from local machine to remove machine using socat (or ssh).
- Use adb in remote to connect local devices.
Prepare the Environment
Install ADB
Install ADB on both local and remote machine. Please make sure:
- ADB in local machine and remote machine are the same version.
- On remote machine, ADB in command line and Android Studio are the same one. Normally Android Studio will install Android SDK automatically on first start. Then we can add the directory to PATH environment.
1 | # show adb path |
https://developer.android.com/studio/releases/platform-tools
https://developer.android.com/studio/command-line/adb
(Windows Only) Configure Firewall for ADB
On Windows, we need to enable ADB network access in firewall settings.
(WSL Only) Install socat
For WSL, install socat first. It is used to do the port forwarding.
1 | sudo apt update && sudo apt install -y socat |
(Remote Linux Machine Only) Configure SSH Key
If you are using remote Linux machine, it is recommended to configure ssh key instead of input password every time.
Start ADB and Port Forwarding Manually
We can start ADB and port forwarding manually or use a script to run automatically.
It is recommended to use a script to do it. But I will show you how to run it manually first so that we can know what we actually do and it will help us debug if something goes wrong.
(Optional) Kill Existing Process
If you have started ADB or port forward before, you need to kill ADB and port forwarding process if exists on both local and remote machine.
For WSL:
Run in windows:
1 | adb kill-server |
Run in WSL:
1 | adb kill-server |
For remote linux machine:
Run in local:
1 | adb kill-server |
Run in remote:
1 | adb kill-server |
Check Local Device Status
Run adb devices
on local machine to see if the device can be connected successfully.
1 | adb devices |
Start adb server mode on local machine
Run in Windows or local Linux machine:
1 | adb -a -P 5037 nodaemon server start |
Setup TCP port forwarding
- We can use socat to do port forwarding if the local and remote machine are in the same network and can connect directly.
- We can also use ssh to do port forwarding if we can access remote machine with ssh.
- Don’t forget to configure firewall if exists.
For WSL, we chose socat to do port forwarding. Run in WSL:
1 | # get host Windows IP |
For remote Linux machine, we chose ssh to do port forwarding. Run in local Linux machine:
1 | # change this to your remote Linux machine user and address, make sure you have already configured ssh key, or you can input password manaully |
Start ADB on Remote Machine and Verify
Run in WSL or remote Linux machine to see if it works:
1 | adb devices |
Start the Environment Automatically for WSL
Usage
- Install adb on Windows and change
$WSL_HOST_ADB
to the adb location following the example format. - Install adb and socat on WSL. Make sure you are using the same adb in terminal and Android Studio. And make sure you installed the same version of adb in Windows and WSL.
- Add the following content to your WSL
~/.bashrc
and runsource ~/.bashrc
to refresh. - Run
wsl-start-adb-daemon
/wsl-stop-adb-daemon
to start/stop adb environment.
1 | WSL_HOST_ADB='/mnt/d/AndroidSdk/platform-tools/adb.exe' |
Start the Environment Automatically for Remote Linux Machine
Usage
- Setup ssh key for remote machine.
- Install adb on local machine.
- Install adb on remote machine and change
REMOTE_ADB_PATH
value to the adb location. Make sure you are using the same adb in terminal and Android Studio. And make sure you installed the same version of adb in local and remote machine. - Add the following content to your local
~/.bashrc
and runsource ~/.bashrc
to refresh. - Run
startRemoteAdb USER HOST
/stopRemoteAdb USER HOST
to start/stop adb environment.
1 | REMOTE_ADB_PATH='$HOME/Android/Sdk/platform-tools/adb' |