DEV Community

Cover image for Run android emulator on CircleCI
Jeevan Kishore
Jeevan Kishore

Posted on

Run android emulator on CircleCI

Running an android emulator on a CI such as CircleCI has been a challenge for a lot of people who want to automate their test cases due to the fact that android emulator require hardware acceleration which is unavailable on debian terminals

We managed to get a headless emulator running on CircleCI with their iOS terminals.

iOS terminals need to be set up to accommodate the emulators, they have dependencies such as SDK's, NDK's, platform tools,

We would have to do the following:

  • Configure ANDROID_SDK_ROOT
  • Download SDK Tools and unzip
  • Create license directory and agree to all licenses
  • Set SDKManager path
  • Install the following
    • "platform-tools"
    • "platforms;android-29"
    • "build-tools;29.0.2"
    • "ndk-bundle"
    • "system-images;android-29;google_apis;x86_64"
    • "emulator"

Let's create a sh to do that

scripts/install-android-tools.sh

 if [ -d $ANDROID_SDK_ROOT ] then echo "Directory $ANDROID_SDK_ROOT already exists so we're skipping the install. If you'd like to install fresh tools, edit this script to invalidate the CI cache." exit 0 fi mkdir -p $ANDROID_SDK_ROOT cd $ANDROID_SDK_ROOT curl https://dl.google.com/android/repository/sdk-tools-darwin-4333796.zip -o sdk-tools.zip unzip sdk-tools.zip mkdir -p "$ANDROID_SDK_ROOT/licenses" echo "24333f8a63b6825ea9c5514f83c2829b004d1fee" > "$ANDROID_SDK_ROOT/licenses/android-sdk-license" echo "84831b9409646a918e30573bab4c9c91346d8abd" > "$ANDROID_SDK_ROOT/licenses/android-sdk-preview-license" echo "d975f751698a77b662f1254ddbeed3901e976f5a" > "$ANDROID_SDK_ROOT/licenses/intel-android-extra-license" SDKMANAGER=$ANDROID_SDK_ROOT/tools/bin/sdkmanager $SDKMANAGER "platform-tools" $SDKMANAGER "platforms;android-29" $SDKMANAGER "build-tools;29.0.2" $SDKMANAGER "ndk-bundle" $SDKMANAGER "system-images;android-29;google_apis;x86_64" $SDKMANAGER "emulator" echo "y" | sudo $SDKMANAGER --install "ndk;20.0.5594570" --sdk_root=${ANDROID_SDK_ROOT} 
Enter fullscreen mode Exit fullscreen mode

Let's write a command block on circle ci to set path, run scripts and cache

 android-sdk-dependencies: description: "Install and set android SDK" steps: - run: name: set ANDROID_SDK_ROOT command: | echo 'export ANDROID_SDK_ROOT=$HOME/android-tools' >> $BASH_ENV - restore_cache: key: android=tools-v1-{{ checksum "scripts/install-android-tools.sh" }}-{{ arch }} - run: name: install android tools command: | sh scripts/install-android-tools.sh echo 'export PATH=$ANDROID_SDK_ROOT/tools/bin:$PATH' >> $BASH_ENV echo 'export PATH=$ANDROID_SDK_ROOT/tools:$PATH' >> $BASH_ENV echo 'export PATH=$ANDROID_SDK_ROOT/platform-tools:$PATH' >> $BASH_ENV echo 'export PATH=$ANDROID_SDK_ROOT/emulator:$PATH' >> $BASH_ENV source $BASH_ENV sdkmanager --list - save_cache: key: android=tools-v1-{{ checksum "scripts/install-android-tools.sh" }}-{{ arch }} paths: - /Users/distiller/android-tools 
Enter fullscreen mode Exit fullscreen mode

All that's left to do now, is to create the emulator and boot it up as a background job

 create-launch-android-emulator: description: "create and launch android emulators" steps: - run: name: create AVD command: echo "no" | avdmanager --verbose create avd --force --name "Pixel_3a_API_29" --package "system-images;android-29;google_apis;x86_64" - run: name: start AVD command: emulator @Pixel_3a_API_29 -no-window -no-audio background: true - run: name: wait for emulator command: adb wait-for-device shell 'while [[ -z $(getprop dev.bootcomplete) ]]; do sleep 1; done;' 
Enter fullscreen mode Exit fullscreen mode

Your emulator is now up and running on CCI, here is the link for the complete configuration of CircleCI

If you have questions, let us know in the comments and we are looking forward for your feedback 🍻

Top comments (1)

Collapse
 
maanuanubhav999 profile image
anubhav_sharma

Hey Jeevan, thanks a lot for this article.

I have a question. if we wanted to simulate google sign in, is there any way we could archive this in circle ci?