Setting up Travis CI in Android

If you are developing using TDD you should run your tests to be sure that you don’t break something in every TDD phase. But we are not perfect and we can commit changes that break our system and we should have something that can alert us if we are doing something wrong asap. This is going to be a short guide on getting set and ready with Travis CI in Android. Before we continue, you need to have an Android Studio project already set up. Now let’s get down to business.

Prerequisites

Create a github repo for your project

Head over to the root of your project and create a .travis.yml (More on this later)

Still at the root of your project, create a folder/directory called licenses; this directory will help in exporting your license agreements such as sdk license agreement from your local machine to the build environment.

Now head over to your sdk location and locate a folder/directory called licenses, copy its contents then go ahead and paste them inside the licenses folder/directory you created previously at your project root.

You’re now set up, just do a git init, add all your files to git then push your code to Github.

Setting up Travis CI

Here is where Continous Integration comes into play. Every time that you push changes into a branch in your system you can check if everything is ok before you merge this branch into Master (assuming you are using git). What to check? Well, this is up to you and your team members to decide.

I keep my promises! , lets now talk about .travis.yml mentioned earlier. So this is just a file that tells your build machine how it should be configured and also provides the necessary tools and information to get your build system up and running.

So how does this file look like? Here’s the code, you can copy paste it but ensure you go through it to have a grasp of what’s going on.

.travis.yml

YAML

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

language: android

sudo: required

jdk: oraclejdk8

env:

global:

-ANROID_API_LEVEL="your api level"

-ANDROID_BUILD_TOOLS_VERSION="your build tools version"

android:

components:

-tools

-platform-tools

-build-tools-$ANDROID_BUILD_TOOLS_VERSION

-android-$ANDROID_API_LEVEL

-extra-google-google_play_services

-extra-google-m2repository

-addon-google_apis-google-$ANDROID_API_LEVEL

licences:

-'android-sdk-preview-license-.+'

-'android-sdk-license-.+'

-'google-gdk-license-.+'

before_script:

-mkdir-p"$ANDROID_HOME/licenses"

-cp./licenses/*"$ANDROID_HOME/licenses"

before_cache:

-rm-f$HOME/.gradle/caches/modules-2/modules-2.lock

-rm-fr$HOME/.gradle/caches/*/plugin-resolution/

cache:

directories:

-$HOME/.gradle/caches/

-$HOME/.gradle/wrapper/

-$HOME/.android/build-cache

script:

-./gradlewbuildcheck

before_install:

-chmod+xgradlew

You can decide to paste it directly to your code then commit and push the changes or paste it in your Github Repo then pull the changes, either of these two will work fine.

Travis provides the service for free but your repo in Github must be public and the free URL is different from the premium URL.

After visiting Travis, sign in with Github. Search for your Github repo then add it. Travis will go to your repo, locate the .travis.yml file and do the rest. Hopefully, if you followed every step correctly, your build should be successful and the repo should turn green with the badge at the top indicating build passing.

Bonus

It’s awesome looking at that badge from your repo. So to add it to your repo, go ahead and click on it. A pop up will show with the url of the badge. Copy the url then paste it in your README file. Here’s an example of how the file should look like inside your README file.

6 Replies to “Setting up Travis CI in Android”

I have not used Travis before, but in my opinion Circle CI has an easier config file to use, and uses a Docker Images to build also. How would you compare the two in terms of ease of use and build times?

What i like about Travis CI is the flexibility it gives you when configuring your build environment. Surely, running your builds in a docker container is faster than a virtual environment, but Travis allows you specify whether your builds should run in Google Compute Engine or Docker. I’m not sure if Circle Ci has the same flexibility. In terms of comparing the two, i would say it’s a matter of implementation and both tools are powerful in a unique way.

Thanks for the comments @Simiyu, @Juma. From my understanding, i stand to be corrected; the work of any build system be it travis, circle ci, jenkins is to ensure your application runs successfully without any crashes. This is irregardless of whether it has tests(unit tests or instrumentation tests). It thus helps in preventing merging changes that break your application. In order to run your tests in the build system you have to include some tasks to do that in the .yml files. Personally i use jacoco to run these tests then generate a report.

Post navigation

About Me

Hello I am Juma Allan, Founder and Author of Android Study. I am currently working as the Lead Android Engineer at dLight Solar, an Open Source Enthusiast and a big time Android Lover, learning Go and sharing my knowledge with others.

“If you don’t build your dream someone will hire you to help build theirs”