Codementor Events

Android : Continuous Integration (Travis CI) with Java

Published Dec 24, 2018Last updated Jan 12, 2019
Android : Continuous Integration (Travis CI) with Java

It's very good for the developer to know the build status of his application. THis makes it better for an application to be monitored 24hours.

Today we are going to explore how to add continuous integration in an Android Java project. There are a lot of Continuous integration tools such as Travis CI, Circle CI, Jenkins, Bamboo, TeamCity etc. For our case, we are going to use TravisCI, but you can also feel free to explore the rest.

The following are the steps you take :

  1. First and foremost start by creating your project in Android Studio.
  2. After creating the project, navigate to the Left top corner, change the drop down from Android to Project. Below is the screenshot:
    Screen Shot 2018-12-24 at 19.58.40.png
  3. Then create a file in the root folder of your project called .travis.yml, for my case it's called ConvergeLevelApp.
  4. Open the .travis.yml file and include the following :
    a) Set the language of the project in the file and JDK version:
language: android
jdk: oraclejdk8

b). Install Android SDK components as below, you can even define the list of SDK components to be installed :

android:
  components:
    - build-tools-26.0.2
    - android-26
    - extra

Note.
The exact component names must be specified (filter aliases like add-on or extra are also accepted). To get a list of available exact component names and descriptions run the command sdkmanager --list (preferably in your local development machine).

c). Then we have to also deal with the licenses,but by default, Travis CI will accept all the requested licenses, but it is also possible to define a whitelist of licenses to be accepted, below is a full code snippet:

language: android
android:
  components:
    - build-tools-26.0.2
    - android-26
    - add-on
    - extra
  licenses:
    - 'android-sdk-preview-license-52d11cd2'
    - 'android-sdk-license-.+'
    - 'google-gdk-license-.+'

d). Next, Starting the emulator on Travis. This also helps so much in several circumstances, such as running instrumentation tests to see if all the tests are passing and so much more. At the moment, Currently, Travis supports **android 22 ** emulation and below , as per the documentation Android Travis. But this may change in the future feel free to visit the documentation.
Below is the code snippet for emulation:

# Emulator Management: Create, Start and Wait
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 &

e). Below is the final output of the .travis.yml file.

language: android
jdk: oraclejdk8
env:
  global:
    - ANDROID_API_LEVEL=25
    - ANDROID_BUILD_TOOLS_VERSION=25.0.0
android:
  licenses:
    - 'android-sdk-preview-license-.+'
    - 'android-sdk-license-.+'
    - 'google-gdk-license-.+'
  components:
    - tools
    - tools
    - platform-tools
    - extra-google-google_play_services
    - extra-google-m2repository
    - extra-android-m2repository
    - build-tools-25.0.0
    - android-22
    - sys-img-armeabi-v7a-android-22


before_install:
  - yes | sdkmanager "platforms;android-27"
  - yes | sdkmanager "platforms;android-28"

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 &

After doing all these steps.

  1. Navigate to travis link Travis CI and signup with your github account.Below is the screenshot.
    Screen Shot 2018-12-24 at 20.20.12.png

  2. Ensure that you have already pushed your entire project on Github, so that it can be visible on your Travis CI account.Check this link for more details How to link Android Studio with Github.
    After pushing your project on Github , Navigate on Travis CI dashboard , you will search your project and you switch your project ON to enusre that you have enabled Travis in your project. Below is how your project should look after enabling it.
    Screen Shot 2018-12-24 at 20.29.39.png

  3. Then lastly your build will start building until it succeeds.
    This is the status you have to get.
    Screen Shot 2018-12-24 at 20.27.51.png

If you get build failed, that means you were not successful while building the project. In the engineering industry this means there's a problem in your project such as code maintainability issues, features breaking or crashing and so many other bugs.

In conclusion, with Continuous Integration in our projects this reduces a lot of the bugs in the project.
For more information visit Travis Android documentation

Good luck. Cheers.

Discover and read more posts from Lutaaya Huzaifah Idris
get started
post commentsBe the first to share your opinion
Victor Hugo dos Santos
4 years ago

helped me a lot, thanks man

Show more replies