I’m currently contributing to BiteMoi to teach myself some Android. When we kicked off the project we started with a Continuous Integration spike and Travis CI was the winner. Travis CI is a free hosted Continuous Integration platform for the Open Source community. This tutorial will get you up and running with the Maven Android quickstart project and Travis CI.

Prerequisites:

  • Maven (>= 3.0.3)
  • Android SDK

What is the plan?

The objective here is to hook up a skeleton Maven Android project (hosted on GitHub) to Travis CI. We will then configure Travis CI to build our project when we push code changes.

The BiteMoi project grew from Android Maven quickstart skeleton code. If you want to generate your own project you can use the android-maven-plugin using the android-quickstart-archetype by doing something like the following:

$ mvn archetype:generate -DarchetypeArtifactId=android-quickstart -DarchetypeGroupId=de.akquinet.android.archetypes -DarchetypeVersion=1.0.8 -DgroupId=com.codenes -DartifactId=my-android-application -Dplatform=16

At this point, if you’re using your own Maven quickstart project you should be able to build it by executing the Maven install goal on the command line:

$ mvn install

Maven will try to download the internets but be patient, it won’t happen every time. If you created your own project, it’s time to host it on GitHub.

It’s time for introductions

To introduce your project to Travis CI you will need to create a file called .travis.yml in the project root directory. This file basically contains a bunch of commands that instructs Travis CI how to build your project. Paste the following into your .travis.yml file:

language: java
jdk: oraclejdk7
before_install:
- sudo apt-get update -qq
- if [ `uname -m` = x86_64 ]; then sudo apt-get install -qq --force-yes libgd2-xpm ia32-libs ia32-libs-multiarch; fi
- wget http://dl.google.com/android/android-sdk_r21.0.1-linux.tgz
- tar -xzf android-sdk_r21.0.1-linux.tgz
- export ANDROID_HOME=~/build/BiteMoi/BiteMeAndroid/android-sdk-linux
- export PATH=${PATH}:${ANDROID_HOME}/tools:${ANDROID_HOME}/platform-tools
- android update sdk --filter platform-tools,android-16,extra-android-support,android-17,sysimg-17 --no-ui --force

The commands we pasted are basically telling Travis CI to do the following:

  1. Spin up a Vagrant VM suitable for Java development
  2. Use Oracle JDK 7
  3. Get info about the latest packages available to me
  4. The Travis VM has a 64 bit OS but I need to install a lib that will allow me to run 32 bit Android SDK on it
  5. Download the Android SDK r21.0.1 Linux distro
  6. Explode the Android SDK distro
  7. Setup my ANDROID_HOME environment variable
  8. Put Android tools and platform-tools on my PATH
  9. Download Android platform 16 (we don’t want all platforms as we have limited space in the Travis CI environment)

Add the new file to Git in your root project directory and commit your project.

Travis CI Configuration

  1. Create a Travis CI account (you can sign in with GitHub)
  2. Navigate to your Travis Profile page
  3. Select and copy your Travis CI token

GitHub Configuration

  1. Navigate to your project on GitHub
  2. Select Settings
  3. Select Service Hooks
  4. Under the Available Service Hooks section select Travis
  5. Under User enter your Travis CI username
  6. Under Token paste your Travis CI token
  7. Check the Active checkbox
  8. Press the Update Settings button
  9. To trigger a Travis CI build from GitHub press the Test Hook button

When I first did this it took AGES for Travis CI to pick up my build. Do not despair. You can help things along by enabling the service hook via your Travis CI Profile page. Navigate to your project repository and turn on the Travis service hook for your project. Sometimes the Travis CI job queue is so big that your job will take a while to get to the top of the queue. This is really annoying as you won’t get instant feedback about your commit and you will have to wait a while to discover if you broke the build in the CI environment.

Travis CI Build

At this point you should have a successful build in Travis CI. See the console output here for the equivalent BiteMoi first green build (note the project has since been forked). By default, Travis picks up that we have a Maven project and executes the Maven install goal for us so we don’t need to put it in our .travis.yml file unless we want to execute a different goal.

Troubleshooting

It’s more than possible you don’t have a green build, to debug it you have a couple of options.

Basic

Inspect the Travis CI build console output and depending on what your error message is, try putting some basic debugging statements into your .travis.yml file e.g.

before_install:
- pwd // Print current working directory
- echo $PATH // Check your PATH environment variable
- mvn -v // What version of Maven is my Travis VM using?

And so on… You will quickly realize debugging in the Travis CI environment is a nightmare.

Better

When we develop code we want all of our environments to be as similar as possible. It is asking for trouble to develop on a Mac for example and then expect your build to work in the Travis CI (Linux) environment.

To set up a development environment that is close to the Travis CI VM we can use Vagrant and the Travis CI JVM Vagrant box to spin up our own development VM.

  1. Install Vagrant and VirtualBox
  2. Download the Travis CI JVM box (this file is ~3 gig)
  3. Add the Travis CI JVM box to Vagrant:
  4. $ vagrant box add travis-jvm /path/to/travis-jvm.box
  5. Create a directory to store your vagrant file:
  6. $ mkdir travis-dev
    $ cd travis-dev
  7. Initialise the VM:
  8. $ vagrant init travis-jvm
  9. Edit your Vagrantfile:
  10. $ vim Vagrantfile
  11. Insert the following in the Vagrant::Config.run do |config| section:
  12. config.ssh.username = "travis"
  13. Spin up the VM:
  14. $ vagrant up
  15. SSH to the VM:
  16. $ vagrant ssh

Once you SSH to your VM you are ready to Git clone your repo and start developing on your Travis development VM. Before you commit code, execute your Travis commands locally. If your builds are passing locally you can be pretty confident they will pass in the Travis CI environment.