Continuous Integration for Android with Travis CI

Ari SWS
DOT Intern
Published in
5 min readAug 20, 2019

Hi there, yesterday I just started setting up Continuous Integrations with Travis CI for some of my Android projects published on GitHub. The main idea of Continuous Integration is that you have a server to build your project, make sure that they are working correctly, then deploy them on wherever it is published. In this case, it will deploy them to the GitHub repository. Interesting, isn’t it? In this article, I will show you how to dealing with Travis CI for Continuous Integrations in your Android Projects. Let’s get started!

Definition

Before we get started, we need to know what Continuous Integrations is? What is it for? And how it’s work?

This what CI is based on Travis CI documentation:

Continuous Integration is the practice of merging in small code changes frequently — rather than merging in a large change at the end of a development cycle. The goal is to build healthier software by developing and testing in smaller increments. This is where Travis CI comes in.

Continuous Integration requires Developers to integrate the code frequently. What is it for? By integrating regularly, you can detect errors quickly, discover where things went wrong, and locate them more easily.

We can do Continuous Integration with tools like Jenkins, TeamCity, Bamboo, GoCD, GitLab CI, and Travis CI integrate with GitHub and other platforms. But in this part, we will talk about Travis CI only.

Setting Up Travis CI Account

The things that we need to do first is that, create Travis CI account using GitHub account. It will automatically integrate will your GitHub account though.

Go to Travis CI official page. Then create an account there. I will not show you how to do it, because I’m pretty sure that all of you know how to dealing with it.

After creating a Travis CI account, we need to give permission to manage our projects in our repository. Just click to switch it on, and you good to go. Here is an example.

And now it’s all done with setting up Travis CI with our GitHub repository. Now let’s move on to the next step.

Note: once you’ve switched on Travis on your repository, a build will be triggered every time a commit or a pull request is made. Without a .travis.yml file, the build will fail. Configuring the .travis.yml will be done on the next step.

Setting Up Travis Build

In order to start Travis build, we need to add a .travis.yml file to our projects. Just put it on the root of the project and push it into your GitHub repository. This file is very important because it will tell how Travis handles the builds.

First, create a .travis.yml file, and add these line of code.

language: android
sudo: required
jdk: oraclejdk8

As you expected, those line of code tells it is an android project that uses oracledjk8. And sudo: required means, a license needs to be added manually later.

Second, let’s add all the main modules we’re going to use.

android: 
components:
- tools
- platform-tools
- tools
- build-tools-29.0.1
- android-29
- extra-android-m2repository

Those lines show the components used in our project. The BuildTools version used by your project, and The SDK version used to compile your project. In this case, I use the current latest BuildTools version that is build-tools-29.0.1 and use android API level 29.

Third, we need to add gradlew permission needed.

There is two way to do this, you can add git update-index --chmod=+x gradlew then git commit -m "permission access for travis". And push it to your repository.

or

add these lines of code to your .travis.yml file.

before_install:
- chmod +x gradlew

And lastly, add this script to your .travis.yml file.

script:
- ./gradlew build

That is the command to build your project.

Now we are ready to go! Here is the full minimal .tavis.yml file.

language: android
sudo: required
jdk: oraclejdk8
android:
components:
- tools
- platform-tools
- tools
- build-tools-29.0.1
- android-29
- extra-android-m2repository
#optional, use it if only you haven't give permission yet.
#before_install:
# - chmod +x gradlew
script:
- ./gradlew build

Now, add it to your root project and push it into your GitHub repository. And let Travis do his job. Travis will automatically build the project for you. Just wait for it to be finished, and it will show you the result. Whether it passed or failed.

Here is the result example.

After the build is passed, you can add the build passing logo into your GitHub readme file.

Just click on the build passing logo on the right top, and copy the Result. Then add it into your readme file.

That’s it. All done. I hope it will help you figure out the importance of Continuous Integration and how to do it on Travis CI for your android projects.

You can read the complete documentation here.

This is the simple usage of Travis CI in Android, you can visit my repository here:

References:

--

--