Skip to content

Instantly share code, notes, and snippets.

@nhtua
Last active May 14, 2024 19:27
Show Gist options
  • Save nhtua/2d294f276dc1e110a7ac14d69c37904f to your computer and use it in GitHub Desktop.
Save nhtua/2d294f276dc1e110a7ac14d69c37904f to your computer and use it in GitHub Desktop.
Run a Headless Android Device on Ubuntu server (no GUI)
#!/bin/bash -i
#using shebang with -i to enable interactive mode (auto load .bashrc)
set -e #stop immediately if any error happens
# Install Open SDK
apt update
apt install openjdk-8-jdk -y
update-java-alternatives --set java-1.8.0-openjdk-amd64
java -version
# Install SDK Manager
# you can find this file at https://developer.android.com/studio/index.html#downloads - section command line only
cd ~ && wget https://dl.google.com/android/repository/sdk-tools-linux-4333796.zip
ANDROID_HOME=/opt/androidsdk
mkdir -p $ANDROID_HOME
apt install unzip -y && unzip sdk-tools-linux-4333796.zip -d $ANDROID_HOME
echo "export ANDROID_HOME=$ANDROID_HOME" >> ~/.bashrc
echo 'export SDK=$ANDROID_HOME' >> ~/.bashrc
echo 'export PATH=$SDK/emulator:$SDK/tools:$SDK/tools/bin:$SDK/platform-tools:$PATH' >> ~/.bashrc
source ~/.bashrc
# Install Android Image version 28
yes | sdkmanager "platform-tools" "platforms;android-28" "emulator"
yes | sdkmanager "system-images;android-28;google_apis;x86_64"
emulator -version
echo "INSTALL ANDROID SDK DONE!"
echo "run 01.emulator-up.sh [new device name] to start emulator"
#!/bin/bash -i
#using shebang with -i to enable interactive mode (auto load .bashrc)
#this script was inspired from https://docs.travis-ci.com/user/languages/android/
set -e #stop immediately if any error happens
avd_name=$1
if [[ -z "$avd_name" ]]; then
avd_name="avd28"
fi
#check if emulator work well
emulator -version
# create virtual device, default using Android 9 Pie image (API Level 28)
echo no | avdmanager create avd -n avd28 -k "system-images;android-28;google_apis;x86_64"
# start the emulator
emulator -avd avd28 -no-audio -no-window &
# show connected virtual device
adb devices
@Zebiano
Copy link

Zebiano commented Mar 12, 2021

I'm surprised this actually worked, thank you very much!

I did get a warning saying the emulator is out of date. Is there an easy way to update it?

I also installed adb with apt install adb as I wasn't being able to test for devices. After installing it I saw the emulator, but when I tried to run 01.emulator-up.sh again, I got a emulator: command not found error. Any idea on how to fix that?

And lastly, how can I actually turn the emulator off?

EDIT: I'm an idiot, forgot to run commands with sudo. That fixed the emulator: command not found error. Yet running 01.emulator-up.sh yields

Error: Android Virtual Device 'avd28' already exists.remote repository...
Use --force if you want to replace it.
null

Any idea on what's going on here? I've since killed the emulator with sudo kill <pid>.

@nhtua
Copy link
Author

nhtua commented Mar 12, 2021

you can add --force to line 17 to delete the existing one and create another new one. It will a little slow you down but it should work.

To stop, you need command

# get emulator name
adb devices

# stop emulator
adb -s emulator-name emu kill

REF https://stackoverflow.com/a/20155436/1235074

@Armatix
Copy link

Armatix commented Jul 2, 2021

For the future:
if you get this error:
emulator: ERROR: x86_64 emulation currently requires hardware acceleration!
add -no-accel to your command:
emulator -avd avd28 -no-audio -no-window -no-accel &

@duhd1993
Copy link

Thanks for putting this together. Is it possible to start an Android GUI that I can connect to with something like VNC?

@rcrandall72
Copy link

-no-accel

Thank you armatix! This saved me a few headaches today!

@joaociocca
Copy link

joaociocca commented Jan 21, 2022

when I get to the yes | sdkmanager "platform-tools" "platforms;android-28" "emulator" part, I get a Warning: Failed to read or create install properties file. error... any ideas?

I got to create an avd, but when I try to run it, I get another error: PANIC: Cannot find AVD system path. Please define ANDROID_SDK_ROOT

@santobollove
Copy link

@joaociocca

Warning: Failed to read or create install properties file.

This Command solved my issue

sudo chown $USER:$USER $ANDROID_HOME -R

@drizzt
Copy link

drizzt commented Aug 24, 2022

it's not really headless since Android SDK qemu-system-x86_64 requires some X libraries and Qt

@mirao
Copy link

mirao commented Oct 8, 2022

Two more things I needed to do in a virtual machine (KVM) with Ubuntu 22.04.1 server to get working emulator in headless mode

  • sudo gpasswd -a $USER kvm
  • sudo apt install libgl1

@Djuk1c
Copy link

Djuk1c commented Dec 24, 2022

I cannot install anything, it gives me the
Error: Could not access the Package Manager. Is the system running?
EDIT:
Its because the emulator hasnt booted completely, for anyone reading you can check that with:
adb shell getprop init.svc.bootanim

@cvasilivg
Copy link

To take advantage of the use of the argument variable "avd_name", you have to modify line 17 and line 20

echo no | avdmanager create avd -n avd28 -k "system-images;android-28;google_apis;x86_64"

to:

echo no | avdmanager create avd -n $avd_name -k "system-images;android-28;google_apis;x86_64"

Finally:

emulator -avd avd28 -no-audio -no-window &

to:

emulator -avd $avd_name -no-audio -no-window &

@hdesai-dave
Copy link

@slickroot
Copy link

Is there a way to install an apk in this script?

@sandipndev
Copy link

Do anyone else continuously get: E0215 06:40:33.890416 14040 FrameBuffer.cpp:3736] Failed to find ColorBuffer:0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment