Run android emulator on CircleCI
Tags: javascript, circleci, android, ios
Published: 2020-09-25 09:44:03

---
title: Run android emulator on CircleCI
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/16yg5j9jmkdi3c7t0teu.png
published: true
description: Run all your tests on CircleCI
tags: javascript, CircleCI, android, ios
---
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
```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}
```

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

```yml
  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
```

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

```yml

  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;'

```

Your emulator is now up and running on CCI, [here](https://github.com/Jeevan-Kishore/e2e-detox/blob/master/.circleci/config.yml) 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 🍻