Continuous Integration for Android with Travis CI

Gears Photo credit: tallkev via VisualHunt.com / CC BY

The benefits of Continuous Integration in software development have been widely described and explored. If you've landed here, chances are that you're already convinced of its advantages, but need guidance on how to integrate it successfully. Hence, we'll just get straight to the point.

On YPlan's mobile team, we host our code on GitHub, and use Travis CI to build, test and deploy our iOS and Android apps. David has described our iOS setup in a previous post. Now it's time for the green droids.

In this article we'll cover the minimum required setup we need to build an Android project with Travis CI.

Since blindly copying setups (and code) doesn't help learning, we'll be exploring the .travis.yml file contents so that you get a true understanding of what you're doing.

On a side note, the official documentation is excellent and very extensive. We encourage you to consult it if you want to gain a deeper knowledge.

The setup file

The .travis.yml file contains the project-specific CI configuration. We'll need to include this file in the project root.

It uses the YAML serialization standard. If you're familiar with JSON, YAML is not too different from it.

Defining the environment

Travis CI assumes our project is built with a JVM build tool like Maven or Gradle. It'll detect this by searching for a pom.xml or a build.gradle file in the root folder, respectively.

From this point on, we'll assume our project is built with Gradle.

Language

To tell Travis we're building an Android project, we need to set the language field.

language: android

Java JDK

The JDK we want to build with can be set in the jdk field.

jdk: oraclejdk7

The full list of provided JDKs can be found here.

Android SDK components

The SDK components are those we've managed a zillion times in the SDK Manager. We can set the list of those we want to be installed in the android.components field.

android:
  components:
  # This will use the latest versions of platform-tools and tools
  - platform-tools
  - tools

  # The BuildTools version used by our project (buildToolsVersion in our build.gradle file)
  - build-tools-24.0.0

  # The SDK version used to compile our project (compileSdkVersion in our build.gradle file)
  - android-23

  # Maven repository to resolve Android Support dependencies
  - extra-android-m2repository

The exact component ids need to be specified. A full list of available components can be obtained by running android list sdk --no-ui --all --extended in a terminal.

Important: If we don't include platform-tools or tools, Travis will build our project with the Android environment which provides Android SDK Tools v24.0.0 (December 2014). Unfortunately, we can't specify which version of these components we want to use, so we have to choose between v24.0.0 or the latest version. Check this issue on the Travis CI project for more info on the matter.

Building

A minimum Travis CI build consists of two steps:

  1. install. Install required dependencies.
  2. script. Run the build script.

The full lifecycle can be found here.

By default, Travis CI builds our project with build connectedCheck. We can override this by setting our own script.

script: ./gradlew build anyOtherGradleTask

script accepts more than one script command and files as inputs, so the following is also valid.

script:
- ./scripts/script1.sh
- ./scripts/script2.sh

Note: The script step won't be stopped if one of the build commands fails. Instead, it accumulates the build result and, if one of the build commands fails, the overall build will be flagged as a failure.

Link with Travis CI

And that's it! Our project is ready to be built by Travis CI!

The final .travis.yml file is:

language: android
jdk: oraclejdk7
android:
  components:
  - platform-tools
  - tools
  - build-tools-24.0.0
  - android-23
  - extra-android-m2repository
script: ./gradlew build anyOtherGradleTask

All we need to do now is to connect our GitHub project with Travis CI.

  1. Go to Travis CI.
    • You'll need to use Travis CI Pro if your project is private. Remember, though, that Travis CI is free only for open-source projects.
  2. Sign in with your GitHub account.
  3. Go to your Travis CI profile page.
  4. Flick the desired repository switch on.
    • If you can't see your repository, click on Sync account.
  5. Trigger a build with a git push.
  6. Perform victory dance

Sample project

You can fork this sample project, which contains the minimum Travis CI setup covered in this article, and use it for testing purposes.

Conclusion

Travis CI is a simple, yet very flexible, CI service. Unlike other job-based CIs, it's based on a "build on changes" approach, so this might take some time getting used to.

On the bright side, it's connected to your GitHub account and, thus, is very well integrated with it. This gives you the confidence that every commit or tag you push and every pull request you create are automatically built and tested.

As CI doesn't stop here, Travis CI provides a lot more capabilities:

  • deploying artifacts
  • read/set environment variables
  • encrypting files and environment variables
  • and much more...

Stay tuned for more advanced Travis CI magic!

Further reading