Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save yazinsai/652f0e6e77c9594a2356dd6314a9c3d8 to your computer and use it in GitHub Desktop.
Save yazinsai/652f0e6e77c9594a2356dd6314a9c3d8 to your computer and use it in GitHub Desktop.
Installing and running Android Emulator on Amazon AWS EC2 (Ubuntu 16.04 / m5.xlarge)

Getting the Android Emulator running on EC2 📱

# @ your EC2 instance
sudo apt update
sudo apt install openjdk-8-jre unzip
wget https://dl.google.com/android/repository/sdk-tools-linux-4333796.zip
unzip sdk-tools-linux-4333796.zip -d android-sdk
sudo mv android-sdk /opt/
export ANDROID_SDK_ROOT=/opt/android-sdk
echo "export ANDROID_SDK_ROOT=/opt/android-sdk" >> ~/.bashrc
echo "export PATH=$PATH:$ANDROID_SDK_ROOT/tools" >> ~/.bashrc
source ~/.bashrc
cd /opt/android-sdk/tools/bin
./sdkmanager --update
./sdkmanager --licenses
./sdkmanager "system-images;android-25;google_apis;armeabi-v7a" "emulator" "platform-tools"
touch ~ubuntu/.android/repositories.cfg
mkdir /opt/android-sdk/platforms
/opt/android-sdk/tools/bin/avdmanager -v create avd -f -n MyAVD -k "system-images;android-27;google_apis;armeabi-v7a" -p "/opt/android-sdk/avd"

Install VNC

# @ your EC2 instance
sudo apt-get install ubuntu-desktop gnome-panel gnome-settings-daemon metacity nautilus gnome-terminal xfce4 vnc4server
vncserver
vncserver -geometry 1600x900

Now modify the first block in ~/.vnc/xstartup:

# Uncomment the following two lines for normal desktop:
unset SESSION_MANAGER
# exec /etc/X11/xinit/xinitrc
unset DBUS_SESSION_BUS_ADDRESS
startxfce4 &

See this article for more details on VNC setup

Running the Emulator (on VNC):

Setup local port forwarding to allow your VNC client to connect to the server:

# @ your local machine
ssh -L 5902:localhost:5902 emulator-aws

Now run this command inside the terminal on your VNC client:

# @ your VNC terminal
/opt/android-sdk/emulator/emulator -ports 5554,5555 -avd MyAVD -gpu swiftshader_indirect -show-kernel

Note: I added -skin 768x1280 to fix the resolution issue I was facing.

Optional: enabling/disabling certain HW features

echo "hw.audioInput=no" >> /opt/android-sdk/avd/config.ini
echo "hw.audioOutput=no" >> /opt/android-sdk/avd/config.ini
echo "hw.cpu.ncore=2" >> /opt/android-sdk/avd/config.ini
echo "hw.camera.back=none" >> /opt/android-sdk/avd/config.ini
echo "hw.camera.front=none" >> /opt/android-sdk/avd/config.ini
echo "hw.gsmModem=no" >> /opt/android-sdk/avd/config.ini
echo "hw.gps=no" >> /opt/android-sdk/avd/config.ini
echo "hw.accelerometer=no" >> /opt/android-sdk/avd/config.ini
echo "hw.battery=no" >> /opt/android-sdk/avd/config.ini
echo "hw.trackBall=no" >> /opt/android-sdk/avd/config.ini
echo "hw.dPad=no" >> /opt/android-sdk/avd/config.ini
echo "hw.sensors.proximity=no" >> /opt/android-sdk/avd/config.ini
echo "hw.sensors.magnetic_field=no" >> /opt/android-sdk/avd/config.ini
echo "hw.sensors.orientation=no" >> /opt/android-sdk/avd/config.ini
echo "hw.sensors.temperature=no" >> /opt/android-sdk/avd/config.ini
echo "hw.ramSize=4096" >> /opt/android-sdk/avd/config.ini

Notes

  • Fixing 100% CPU: Disabling the "Ok Google" Hotword detection (run this after the emulator has loaded):
./adb shell "su root pm disable com.google.android.googlequicksearchbox"
  1. We run ARM emulation on x86 instance.
  2. We must use ARM ABI, since x86 requires KVM and EC2 instance doesn't support HW virtualization.
  3. We must disable GUI (-no-window) and audio (-no-audio) when running the emulator.
  4. We must specify "-gpu swiftshader_indirect" when running the emulator to prevent a boot loop.
  5. First startup takes VERY LONG time. Use "-show-kernel" to see kernel messages and in secondary console window use "/opt/android-sdk/platform-tools/adb logcat" to see the system log while the emulator starts.

Creating another AVD

/opt/android-sdk/tools/bin/avdmanager create avd -n TestEmulator1 -k "system-images;android-27;google_apis;armeabi-v7a" -c 2000M --tag "google_apis" --device "Nexus 5X"
/opt/android-sdk/emulator/emulator -ports 5554,5555 -avd TestEmulator1 -gpu swiftshader_indirect -show-kernel -skin "1080x1920" -wipe-data
@grossamos
Copy link

For anybody running into the same issue:

/opt/android-sdk/tools/bin/avdmanager -v create avd -f -n MyAVD -k "system-images;android-27;google_apis;armeabi-v7a" -p "/opt/android-sdk/avd"

should be

/opt/android-sdk/tools/bin/avdmanager -v create avd -f -n MyAVD -k "system-images;android-25;google_apis;armeabi-v7a" -p "/opt/android-sdk/avd"

@Simbaclaws
Copy link

Simbaclaws commented Nov 14, 2023

what about doing this on a ubuntu server? Would the guide change a bit? I want to setup an ubuntu server at home where I can login to through vnc with a password from the outside. Preferably changing it into a systemd service that I can restart automatically and start on boot...

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