Cache your Android SDK with Travis

Enjoy faster builds by not doing unnecessary downloads.

If you use Travis to build your Android app and followed the official guide, you may have noticed that your builds are… well, slow 🙂 For instance, a very small app I’m working on would take about 10 minutes to build (going up to 15 minutes on some occasions.)

Apart from the “normal” slowness of Gradle builds, one reason is that, by default, Travis will download and install the Android SDK and its components - for Every. Single. Build. Yes you read that correctly. Also remember that by default a build is executed for every commit 😱 I mean, never mind the slowness, but what a waste of energy and bandwidth!

The solution of course is to include the Android SDK in the cache so, except for the first time, nothing will be downloaded/installed.

After doing that on my little app, builds now take about 2 minutes (most of which are spent on the actual Gradle execution.)

Now how do you do this?

Don’t use the default Travis Android SDK

It’s probably possible to cache the Android SDK included with the Travis Android images, but it’s better to do it manually because:

  • you control where to install it
  • the latest version available with Travis is quite old.

So we’re going to download the latest Android SDK, unzip it, and then install components using the new sdkmanager tool:

As you can see (line 4), we check if the SDK zip already exists before downloading it — since the destination path is included in the cache, this ensures it will be downloaded only the first time.

We then unzip it (line 5) with the -n flag to skip files that are already present, and with -qq to not pollute the logs.

Then we install/update components — this will not do anything if they are already up to date. Note: the echo y | part is there to “accept” the licenses. We also pipe that to /dev/null to not pollute the logs with the licenses.

Cache the SDK

Now you simply need to include the SDK path in the cache section of the build file (also include the path where you download the zip):

Last but not least, in order for your build to use this SDK, you need to set the ANDROID_HOME environment variable:

And that’s it! Enjoy faster builds 👍

Here’s the complete .travis.yml file for reference: