This is a howto on running an Android Virtual Device (Emulator) on a remote machine for debugging and development.

This applies to an Ubuntu 20.04 environment and uses SSH tunneling and port forwarding.

In this post:

References

Setup and installation

On the remote PC only the barebones of the Android SDK is required.

Prerequisites

  • JAVA JDK 8:
java -version

java version “1.8.0_221” Java(TM) SE Runtime Environment (build 1.8.0_221-b11) Java HotSpot(TM) 64-Bit Server VM (build 25.221-b11, mixed mode)

Obtain sources and unpack

The sources need to be placed in a particular directory structure.

First obtain the sources. Each download requires accepting T&Cs to access the download button/link.

Access the Android Studio Downloads and download the latest commandlinetools-linux-* .zip file, e.g. commandlinetools-linux-7583922_latest.zip.

From the SDK Platform Tools page, download the SDK Platform-Tools for Linux, which was platform-tools_r31.0.3-linux.zip at the time of writing.

In this example, the directory under which the sources will be unpacked is

/opt/android/sdk

and the downloaded files are located in

~/Downloads

Some juggling is required to set up the correct paths and unpack the commandline tools correctly:

cd /opt/android/sdk
mkdir cmdline-tools
cd cmdline-tools/
unzip ~/Downloads/commandlinetools-linux-7583922_latest.zip
mv cmdline-tools/ tools

resulting in the following structure:

── sdk
   └── cmdline-tools
       └── tools
           ├── bin
           ├── lib
           ├── NOTICE.txt
           └── source.properties

Then the platform-tools need to be added:

cd /opt/android/sdk
unzip ~/Downloads/platform-tools_r31.0.3-linux.zip

after which the structure appears thus

├── cmdline-tools
│   └── tools
└── platform-tools

Update the $PATH environment variable in .bashrc

cd /opt/android/sdk
echo "export PATH=${PWD}/platform-tools:${PWD}/cmdline-tools/tools:${PWD}/cmdline-tools/tools/bin:"'${PATH}' | tee -a ~/.bashrc
source ~/.bashrc

On my system I did NOT need to set the $JAVA_HOME environment variable.

Finally test that the elements are correctly installed and accessible:

adb --version

Android Debug Bridge version 1.0.41
Version 31.0.3-7562133
Installed as /opt/android/sdk/platform-tools/adb

sdkmanager --version

5.0

Configure and update

Run the following commands to configure and install necessary components.

sdkmanager --update

To show packages, issue

sdkmanager --list

In my case, I require an older version, android-23 so this needs to be downloaded, including google_apis:

sdkmanager "platforms;android-23"
sdkmanager "system-images;android-23;default;x86_64"
sdkmanager "system-images;android-23;google_apis;x86_64"

Note: for later versions Google Play Services are also required, e.g.

sdkmanager "system-images;android-29;google_apis_playstore;x86_64"

Install the Emulator and build tools

Issue the following to install the emulator:

sdkmanager --channel=3 emulator

and from the --list command, find the specification for the latest build tools version, e.g.

sdkmanager "build-tools;31.0.0"

Creating an AVD

Create an AVD based on the image downloaded previously (some prompts will require responses):

avdmanager create avd -n avdBuild23B \
  -k "system-images;android-23;google_apis;x86_64" \
  -g "default"

The following lists available AVDs:

avdmanager list avd

Launching an AVD via X11 forwarding

Access the remote PC via SSH using -o ForwardX11=yes

ssh -o ForwardX11=yes marc@192.168.178.103

then run the AVD with -gpu guest switch:

cd /opt/android/sdk/tools
./emulator -avd avdBuild23B -gpu guest

This should case the AVD to be opened on the local PC:

80f85f4bc777bafe74830045b3857110.png

af46c97cd78ca60b1c96fd6161b90078.png

Development environment: run app on remote PC

To be able to access the remote PC’s AVD from Android Studio on the local PC, two steps are required, forwarding ports and connecting adb appropriately.

SSH Tunnel

Set up a SSH tunnel on the local PC:

ssh -NL 5556:localhost:5554 -L 5557:localhost:5555 marc@192.168.178.103

and in a separate console, restart adb and connect to the port:

killall adb
adb connect localhost:5557

This last step should have the effect of making the AVD accessible to Android Studio:

63e8eabfb7caa0faf3f778e2ca21de10.png