Speeding up Android builds with Cirrus CI

Fedor Korotkov
CirrusLabs
Published in
2 min readJan 17, 2018

In our previous blog post we discussed Gradle caching and how Cirrus CI can make your Gradle builds 6 times faster by using built-in HTTP Build Cache. In this blog post we will see how Cirrus CI can be configured to run Android integration tests using ButterKnife project as an example.

Cirrus CI UI

To run Android integration tests, first of all, we need a running Android emulator. We’ve created a few docker containers with Android tools and emulators pre-installed. Since we are going to do testing using ButterKnife project and ButterKnife requires at least Android API level 18, we are going to use cirrusci/android-sdk:18 image that contains only tools and system images for that particular API version.

Here is a snippet of .cirrus.yml file to use this Docker image:

container:
image:
cirrusci/android-sdk:18
cpu: 4
memory: 10G

Since we are using a Docker image with a pre-installed Android system image we can start using it right away to create a device:

create_device_script: >
echo no | avdmanager create avd --force \
-n test \
k "system-images;android-18;default;armeabi-v7a"

Once device is created we need to start an emulator in the background:

start_emulator_background_script:
- $ANDROID_HOME/emulator/emulator -avd test -no-audio -no-window

While emulator is booting we can use this time effectively and assemble ButterKnife:

assemble_script: ./gradlew assemble assembleAndroidTest

Before running tests let’s double check the Android emulator has started while we were building ButterKnife (and let’s wait if the emulator is not ready):

wait_for_emulator_script: adb wait-for-device

That’s all! Now we can run our integration tests! And here is the full .cirrus.yml file:

container:
image:
cirrusci/android-sdk:18
cpu: 4
memory: 10G

check_android_task:
create_device_script:
>
echo no | avdmanager create avd --force \
-n test \
-k "system-images;android-18;default;armeabi-v7a"
start_emulator_background_script: >
$ANDROID_HOME/emulator/emulator \
-avd test \
-no-audio \
-no-window
assemble_script: ./gradlew assemble assembleAndroidTest
wait_for_emulator_script:
- adb wait-for-device
- adb shell input keyevent 82
check_script: ./gradlew check connectedCheck

Cirrus CI allowed to avoid spending time on useless recurring work like installing dependencies and waiting for an emulator to start. All of this allowed to get maximum performance out of a CI build: an Android emulator is booting and Gradle is assembling within 10 seconds after a Cirrus CI build is initialized.

If we compare Cirrus CI build times to build times of a CI that ButterKnife is currently using then we’ll see that Cirrus CI is 3 to 4 times faster.

We are highly encourage you to try out Cirrus CI. It’s free for Open Source projects and very easy to setup!

Follow us on Twitter and if you have any questions don’t hesitate to ask.

--

--