Starting the Android Emulator from the terminal

I needed a way to start the Android emulator without having to open Android Studio. First I tried the standard emulator command, but that didn't work.

$ emulator -avd Pixel_XL_API_25
PANIC: Missing emulator engine program for 'x86' CPU.

So I searched the web and found out that I had to start a different emulator and symlink it somewhere else. I didn't like that idea. My next thought was "What does Android Studio do?". I watched my processes and started the emulator from Android Studio.

$ ps aux | grep qemu
... /usr/lib/android-sdk/emulator/qemu/linux-x86_64/qemu-system-x86_64 -netdelay none -netspeed full -no-snapshot-load -avd Pixel_XL_API_25

Nice, that looks promising.

$ /usr/lib/android-sdk/emulator/qemu/linux-x86_64/qemu-system-x86_64 -netdelay none -netspeed full -no-snapshot-load -avd Pixel_XL_API_25
/usr/lib/android-sdk/emulator/qemu/linux-x86_64/qemu-system-x86_64: error while loading shared libraries: libc++.so.1: cannot open shared object file: No such file or directory

Looks like Android Studio brings its own libraries and we need to prepare our environment so the emulator starts properly.

$ xargs --null --max-args=1 < /proc/ID_THE_OUR_QEMU_PROCESS/environ
# I will only show you the necessary lines
# ...
LD_LIBRARY_PATH=/usr/lib/android-sdk/emulator/lib64/qt/lib:/usr/lib/android-sdk/emulator/lib64/libstdc++:/usr/lib/android-sdk/emulator/lib64/gles_angle11:/usr/lib/android-sdk/emulator/lib64/gles_angle9:/usr/lib/android-sdk/emulator/lib64/gles_angle:/usr/lib/android-sdk/emulator/lib64/gles_swiftshader:/usr/lib/android-sdk/emulator/lib64
ANDROID_EMULATOR_LAUNCHER_DIR=/usr/lib/android-sdk/emulator
# ...

With this info I created a launch script for my emulator.

#!/usr/bin/env bash
export ANDROID_EMULATOR_LAUNCHER_DIR=/usr/lib/android-sdk/emulator
export LD_LIBRARY_PATH=/usr/lib/android-sdk/emulator/lib64/qt/lib:/usr/lib/android-sdk/emulator/lib64/libstdc++:/usr/lib/android-sdk/emulator/lib64/gles_angle11:/usr/lib/android-sdk/emulator/lib64/gles_angle9:/usr/lib/android-sdk/emulator/lib64/gles_angle:/usr/lib/android-sdk/emulator/lib64/gles_swiftshader:/usr/lib/android-sdk/emulator/lib64

EMULATOR86="$ANDROID_HOME/emulator/qemu/linux-x86_64/qemu-system-x86_64"
EMULATOR="$ANDROID_HOME/tools/emulator"
DEVICE=`$EMULATOR -list-avds | tail -1`

RUN="$EMULATOR86 -netdelay none -netspeed full -no-snapshot-load -avd $DEVICE"

echo "Run $RUN"

$RUN