Android App From Scratch Part 6 — Continuous Integration with Travis-CI

Faruk Toptaş
Android Bits
Published in
4 min readDec 22, 2018

In this tutorial series, I will try to create an RSS Reader app step by step. Through this series I will explain:

  1. How to use Model-View-Presenter in an Android App
  2. Implementing must have libraries
  3. Implementing App Logic
  4. Creating unit tests with JUnit
  5. Creating Android Instrumentations tests
  6. Continuous Integration with Travis-CI

Continuous integration systems let you automatically build and test your app every time you check in updates to your source control system. I will explain how to automate

  • Build Apk
  • Run JUnit tests
  • Deploy Apk to Github releases

There are various tools to integrate CI to your application.

  • Travis-CI
  • CircleCI
  • Fastlane
  • Jenkins
  • Bitbucket Pipelines
  • Github Actions (Not open to every developer yet)

I will use Travis-CI for our app. At first, you need to signup to travis-ci.org with your Github account.

Under the repositories page, activate the toggle for the repo you want to enable CI.

You can see your app listed in your dashboard.

Create .travis.yml file in your project root folder. Paste the contents below.

language: android
jdk: oraclejdk8

android:
components:
- platform-tools
- tools

- build-tools-27.0.3

- android-27

- extra-google-google_play_services
- extra-google-m2repository
- extra-android-m2repository
- addon-google_apis-google-19


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

script:
- ./gradlew assembleDebug

I used Android SDK version 27 and 27.0.3 for build tools. You can update these lines depending on your project.

Now you can push this file to Github. After pushing your commit you will see the yellow dot on the right side of your commit message.

By clicking on the yellow dot you will be navigated to the build logs.

If the build passes you will see the green build-passing badge.

After a successful build, you can see information about the build. This is the output of assembleDebug task.

I created JUnit tests in Part4. I will trigger these tests from Travis-ci. The command below runs the JUnit test suite:

I created run.sh file to run all tests then create an output apk.

./gradlew -Dtest.single=me.toptas.rssreader.UnitTestSuite clean test
./gradlew assembleDebug

Make run.sh executable with the command below

chmod +x run.sh

Push the files you committed to Github to see test results.

Tests running by travis-ci

The next part is deploying to Github releases. After the assembleDebug task an apk will be generated under build/outputs/apk folder. With the deploy section in .travis.yml file I will add scripts for deploying.

Before adding deploy scripts I need a Github API key to let travis-ci upload the apk. Go to https://github.com/settings/tokens/new to create a new token with scopes you want.

Copy your token. Then add it as an Environment Variable to your repo settings in travis-ci.

Add deploy scripts to .travis.yml

deploy:
provider:
releases
api-key: $GITHUB_API_KEY
file: $TRAVIS_BUILD_DIR/app/build/outputs/apk/debug/app-debug.apk
skip_cleanup: true
name: dev-build-$TRAVIS_TAG
body: Automatic build of $TRAVIS_BRANCH ($TRAVIS_COMMIT) built by Travis CI on $(date +'%F %T %Z').
prerelease: true
overwrite: true
target_commitish: $TRAVIS_COMMIT
on:
tags:
true

after_deploy:
- rm -rf $TRAVIS_BUILD_DIR/app/build/outputs

I enabled Pre-release for auto releases. You can disable by removing the prerelease: true line.

Releases are dependent on tags with the rule below:

on:
tags:
true

Travis-ci supports different conditions to trigger deploy. If you want to trigger deployment only for a specific branch use below:

on:
branch:
master

Whenever you want to create a release, push a tag to Github.

After pushing the tag my release is ready.

Happy codings :)

If you liked the article, please 👏👏👏 so more people can see it! Also, you can follow me on Medium

--

--