Integrating Travis CI with Android Project (API Level 30)

Milinda Nandasena
App Dev Community
Published in
4 min readJan 29, 2021

Continuous Integration (CI) helps developers to integrate code many times a day into a shared repository. An automated build then verifies each check-in, allowing teams to spot issues early. You can have a test suite consisting of integration tests and unit tests in the case of Android. When you push any commit to GitHub or make a pull request, CI helps you to perform all of your tests. This will then provide you details about whether nothing has been broken by the new piece of code that you have built.

CI is a building block to do a continuous deployment process. With continuous deployment, once you make any stable improvements in the codebase, you can publish your application. You write a code during continuous deployment, push it to GitHub, run tests on a CI server, and changes are automatically deployed on the play-store. So, it makes the release-cycle smaller and smaller.

Travis CI is a free open-source tool that can be easy to get started in a few minutes and it’s one of the few CI tools out there that support Android properly.

One of the great things you can do is that you can test your build across different API levels, with just a few lines of code automatically.

When you refer to the Travis CI documentation, there are not enough details on to how configuring the Android application in API level vice.

To learn how to write a .travis.yml file, visit the Travis documentation site.

For my newest Android project(IdeaHub), I have set up Continuous Integration (CI). As usual, in the root directory of my project (which is hosted on GitHub), I added a .travis.yml file and followed their docs accordingly so that my app is tested on each commit.

If you integrate your project with Travis CI, also first you should add the following file to the root directory of your project.

Now I will explain the above code snippet, then you can get a clear idea. OK..!!

Let’s go…

  1. To configure the language of the project file and the JDK version:
language: android
jdk: oraclejdk8

2. You can also specify the list of Android SDK components to be installed as below, and we must also deal with google licenses. By default, Travis CI will allow all the licenses requested, but it is also possible to specify the whitelist of licenses to be approved, as follows:

android:  
licenses:
- 'android-sdk-preview-license-.+'
- 'android-sdk-license-.+'
- 'google-gdk-license-.+'
components:
- tools
- build-tools-30.0.2
- android-30
- android-22
- extra-google-google_play_services
- extra-google-m2repository
- extra-android-m2repository
- sys-img-armeabi-v7a-android-22

3. The following script should be applied with before_install for the exec-permission to your gradlew script:

before_install:  
- chmod +x gradlew

4. Next, Starting the emulator on Travis. This also helps such as running instrumentation tests to see if all the tests are passed.

At the moment, Travis supports the emulation of Android 22 and below, as per the Android Travis documentation. But this could change if you visit the documents in the future.

before_script:  
- echo no | android create avd --force -n test -t android-22 -- abi armeabi-v7a
- emulator -avd test -no-audio -no-window &
- android-wait-for-emulator
- adb shell input keyevent 82 &

5. Travis uses,

gradle build connectedCheck

Command for building a project, performing lint checks, and performing UI tests.
So let’s add this line:

script:  
- ./gradlew clean build
- ./gradlew test
- ./gradlew build check

6. Finally, you should be adding the following script to avoid uploading the cache after every build.

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

Okay… Now, you have a better idea about how our code snippet is working.

After doing all these steps, when you push your project to Github then Navigate on Travis CI dashboard, you will search for your project. Below is shown your project looks after enabling it.

Finally, your build will start building until it succeeds.
This is the status you have to get.

For more information visit Travis Android Documentation.

Note: In here, I have done only the tested part with Travis CI and after that, we can add our deployment script to the .travis.yml file. Then you can publish your release to the play store. In the future post, I will explain how to do that.

In conclusion, Continuous Integration into our projects eliminates many of the project’s bugs.

Good luck. Cheers.

--

--