Skip to content

Instantly share code, notes, and snippets.

@atyachin
Last active May 8, 2024 10:35
Show Gist options
  • Save atyachin/cf1690085173e1fabc07b9acc7af3de6 to your computer and use it in GitHub Desktop.
Save atyachin/cf1690085173e1fabc07b9acc7af3de6 to your computer and use it in GitHub Desktop.
Installing and running Android Emulator on Amazon AWS EC2 (Ubuntu 16.04 / m5.xlarge)
Update (2022): https://gist.github.com/atyachin/2f7c6054c4cd6945397165a23623987d
Steps for installing the Android Emulator from EC2 console:
-----------------------------------------------------------
sudo apt install default-jdk
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
re-login
cd /opt/android-sdk/tools/bin
/opt/android-sdk/tools/bin/sdkmanager --update
/opt/android-sdk/tools/bin/sdkmanager --licenses
/opt/android-sdk/tools/bin/sdkmanager "system-images;android-25;google_apis;armeabi-v7a"
/opt/android-sdk/tools/bin/sdkmanager "emulator"
/opt/android-sdk/tools/bin/sdkmanager "platform-tools"
touch /home/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-25;google_apis;armeabi-v7a" -p "/opt/android-sdk/avd"
Optional: eanbling/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
Running the Emulator:
---------------------
/opt/android-sdk/emulator/emulator -ports 5554,5555 -avd MyAVD -no-window -no-audio -gpu swiftshader_indirect -show-kernel
Fixing 100% CPU: Disabling the "Ok Google" Hotword detection:
-------------------------------------------------------------
./adb shell "su root pm disable com.google.android.googlequicksearchbox"
Note: run after Emulator loaded
Notes:
------
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.
@nhphong
Copy link

nhphong commented Jun 30, 2020

@kukabi
adb shell getprop sys.boot_completed returns 1 only indicates that the system has been booted but the services are just starting to load...
If you adb install right at that moment, there's a chance that necessary components for Package Manager are NOT ready yet. The result could be a dead-lock that causes your command adb install to get stuck. Eventually the Android WatchDog may restart your emulator.

Watchdog: *** WATCHDOG KILLING SYSTEM PROCESS: Blocked in handler on PackageManager (PackageManager)

I suggest that you open another console and run adb logcat to see what's going on with the system.

@kukabi
Copy link

kukabi commented Jun 30, 2020

@nhphong thanks a lot, I will test in a bit and update here.

@kukabi
Copy link

kukabi commented Jun 30, 2020

@nhphong Actually it worked this time, but it took 10 minutes to install the APK. I'm running adb install from another window, and the last time I didn't wait thinking the process was frozen. You may find the logcat output here. Do you know if there's a way to shorten this time?

@kukabi
Copy link

kukabi commented Jun 30, 2020

Though I'm guessing it's because it's an armeabi-v7a. I remember it was extremely slow to run one locally too.

@kukabi
Copy link

kukabi commented Jun 30, 2020

@nhphong So I got it all working on an EC2 instance (Ubuntu 18.04) including some passing instrumented tests but managed to get the WATCHDOG KILLING SYSTEM PROCESS error on a Docker instance (again, Ubuntu 18.04) on my laptop. Emulator restarted and I got adb: failed to install. What is a good way to be certain that the emulator is ready for adb install?

@nhphong
Copy link

nhphong commented Jul 1, 2020

@kukabi
Use this to check when the boot animation finished

adb shell getprop init.svc.bootanim

For example:

adb wait-for-device shell 'while [[ $(getprop init.svc.bootanim) != stopped ]]; do sleep 1; done;'

@nhphong
Copy link

nhphong commented Jul 1, 2020

Note that the above approach does not work if you start the emulator with -no-boot-anim option (for example, when you run a headless emulator on the memory)

@lasley
Copy link

lasley commented Jul 9, 2020

Thanks for this gist.

  1. We must use ARM ABI, since x86 requires KVM and EC2 instance doesn't support HW virtualization.

This is no longer the case- You can use x86 virtualization on any of the .metal EC2 instances, which provide direct HW access.

Here's the release announcement + case study: https://aws.amazon.com/jp/blogs/aws/new-amazon-ec2-bare-metal-instances-with-direct-access-to-hardware/

@jankowskimarcin
Copy link

Emulator on this solution is extremely slow do someone have experience with Amazon EC2 A1 Instances ?

@kukabi
Copy link

kukabi commented Aug 21, 2020

We ended up GitHub actions with a Mac OS X virtual machine and we're very happy. Here's the test workflow.

Emulator on this solution is extremely slow do someone have experience with Amazon EC2 A1 Instances ?

@jankowskimarcin
Copy link

@kukabi the point is to use Amazon infra

@brunoaduarte
Copy link

Is it possible to run multiple emulators > 20 in this infrastructure @atyachin ?

@xuguozhen
Copy link

Hi,All
I want to boot Android with UEFI in an ARM virtual machine,is there any feasible way?

@jaejinkorean
Copy link

I keep getting "Process system isn't responding". I was using t2.xlarge Ubuntu x86 instance

@atyachin
Copy link
Author

atyachin commented Jun 6, 2022

@Sarimsaljook
Copy link

Having an issue while running the emulator:

ERROR: Insufficient RAM free for launching emulator.
Please free up memory (close other programs and files),
or decrease the AVD RAM size and try again.

I have already tried everything regarding the ram size modifications in the config files to changing the storage setting on the Ec2 but it won’t work.

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