Run your Android CI in Google Cloud Build

Martin Bonnin
Aug 28, 2018 · 4 min read
Setting up docker containers is not as hard as you may think!

1. Fork and clone the tutorial repo

You will need to fork the repo first to have rights to install github triggers (see below):

git clone
git clone

2. Create a new Google Cloud project and enable Cloud Build API

Go to and create a new project. The project for this tutorial is named cloudbuildsample.

3. Create your docker cloud-builder image and upload it to the Google container registry

# If you haven't already, install gcloud from these instructions: 
cd android-cloud-build-sample/cloud-builder
gcloud auth login
gcloud config set project cloudbuildsample
gcloud builds submit --config cloudbuild.yaml 
# You now have a docker image with gradle and the android SDK 
# uploaded to the google container registry

# Install Dependencies
RUN apt-get update \
    && apt-get install -y wget zip unzip \
    && mkdir -p /opt/android-sdk-linux

ENV ANDROID_HOME /opt/android-sdk-linux

# Download Android SDK tools
RUN wget -q "" -O \
    && unzip -q -d $ANDROID_HOME \
    && rm


# Install Android SDK components
RUN echo y | sdkmanager --install 'platforms;android-27' "build-tools;27.0.3" "platform-tools"

4. Speedup: cache your .gradle files !

This is where having lower level access to the infrastructure running the images comes in handy. You can tell Google Cloud Build to fetch a previous version of the gradle cache. Not only does this speed up the build but it makes it more reliable when maven, jcenter or any other hosting provider goes down. This unfortunately happens often and it’s frustrating to see a failed build just because dependencies fail to download.

# Install the wrapper script
COPY gradle-build /bin/
# unzip command might fail the first time if does not 
# exist. That's okay
unzip -o -q 
./gradlew $@
zip -qr .gradle
echo "Exit status is: $status"
exit $status

5. Create the github triggers

Tell Google Cloud build to start a build on every push to your Github repository.

# get the cache from cloud storage
- name:
  args: ['cp', 'gs://gradle_cache_$PROJECT_ID/', '']
# build the app
- name: '$PROJECT_ID/android-builder'
  entrypoint: 'gradle-build'
  args: ['-g', '.gradle', 'assemble']
# push the cache to cloud storage
- name:
  args: ['cp', '', 'gs://gradle_cache_$PROJECT_ID/']

6. That’s it, you’re done !

Next time you push something to your repo, Google Cloud Build will build it for you:

Yay ! All Green !

Wrap up


  • Per-minute pricing. There is no extra cost if no-one is pushing during the weekends.
  • 2 hours of free build time/day for small projects (including private projects).
  • Google Cloud scale. You can use up to 32 CPUS/28GB RAM machines if you really want to.
  • Complete control of your docker images and the build process.
  • Easy integration with other Google Cloud products. Storing artifacts in Cloud storage is a matter of adding a new step.
  • You can cache your .gradle directory!


  • We didn’t find a solution that automatically displays the status of pull requests. You’ll most likely have to set up the status hook handlers yourself.
  • It’s not possible to start multiple builds for the same commit like Travis CI build matrices.
  • There’s a bit of overhead to setup and maintain everything.


The home for videos that matter

Martin Bonnin

Written by


The home for videos that matter