Running the Android Emulator on CI/CD Pipelines
- Create the Android emulator
- Run the Android emulator
- Check the Android emulator
- Install an application in Android emulator
- Run Android emulator on CI/CD pipelines
- References
Create the Android emulator
# Install required packages to create and run the Android emulator
echo "y" | sdkmanager --install 'system-images;android-33;default;x86_64'
# Create the Android emulator
echo "y" | avdmanager create avd -n android -d "$ANDROID_EMULATOR" -k 'system-images;android-33;default;x86_64' --force
Replace
$ANDROID_EMULATORwith the desired device such as 4.65in 720p (Galaxy Nexus), pixel_2, Nexus 10 etc.
You can check the available devices using the command
avdmanager list device.
The
androidafter-nis a custom name to the Android Virtual Device.
The
echo "y"will automatically answer "yes" to any prompt during the command execution, for example, to accept Android SDK licenses and to confirm the Android Virtual Device creation.
Change
android-33to any Android API Level, but be careful because some versions can not work as expected or will need additional configuration.
Change
defaulttogoogle_apisif you need Play Store (Google Play) to be installed on the Android emulator, but be careful because it can't work in some situations.
Run the Android emulator
nohup emulator -avd android -no-snapshot -no-window -no-audio -no-boot-anim -camera-back none -camera-front none -qemu -m 2048 > /dev/null 2>&1 &
The
androidafter-avdis the custom name given to the Android Virtual Device in the creation step.
The
nohupin the beginning allows the command to continue running even if the terminal is closed.
The
&in the end allows the command to continue running in the background.
Change the
-m 2048to the total amount of RAM nedeed for your use case.
If you're running on GitHub-hosted larger Linux runners, you need to enable hardware acceleration.
You can also try to run the Android emulator using this bash script that will check hardware acceleration.
Check the Android emulator
Before using the emulator, it's better to first check if it booted successfully.
adb wait-for-device shell 'while [[ -z $(getprop sys.boot_completed | tr -d "\r") ]]; do sleep 1; done; input keyevent 82'
adb devices
Install an application in Android emulator
adb install "$APP_APK_PATH"
Replace
$APP_APK_PATHwith the path to the.apkfile.
Run Android emulator on CI/CD pipelines
Below is the complete solution that will work on macOS agents in CI/CD environments.
# Install required packages to create and run the Android emulator
echo "y" | sdkmanager --install 'system-images;android-33;default;x86_64'
# Create the Android emulator
echo "y" | avdmanager create avd -n android -d "$ANDROID_EMULATOR" -k 'system-images;android-33;default;x86_64' --force
# Run the Android emulator
nohup emulator -avd android -no-snapshot -no-window -no-audio -no-boot-anim -camera-back none -camera-front none -qemu -m 2048 > /dev/null 2>&1 &
# Check the Android emulator
adb wait-for-device shell 'while [[ -z $(getprop sys.boot_completed | tr -d "\r") ]]; do sleep 1; done; input keyevent 82'
adb devices
# Install an application in Android emulator
adb install "$APP_APK_PATH"
The commands above were tested in Azure Pipelines running on macOS-14 agent.