Building Android applications on Travis CI

  • Tutorial
In this article, I would like to show how you can build open-source Android projects (Maven) on Travis CI for free.

What do we have?


  • Open source android project on github
  • The project should be assembled by Maven
  • Account at Travis CI

What do we want to get?


Free assembly of the project on a commit with a progress report by mail. At the same time, we want to run integration tests on the emulator and use additional SDK components (addons, extras, etc.)

How do we achieve this?


As a basis, we take ready-made scripts on github: https://github.com/serso/android-common .
The main script that Travis CI uses to build is .travis.yml:
language: java
jdk: oraclejdk7
before_install:
  - chmod +x $PWD/.travis_install_android.sh
  - $PWD/.travis_install_android.sh
  - export ANDROID_HOME=$PWD/android-sdk-linux
  - export PATH=${PATH}:${ANDROID_HOME}/tools:${ANDROID_HOME}/platform-tools
  - chmod +x $PWD/.travis_install_android_dependencies.sh
  - $PWD/.travis_install_android_dependencies.sh
  - chmod +x $PWD/.travis_start_emulator.sh
  - $PWD/.travis_start_emulator.sh
before_script:
  - chmod +x $PWD/.travis_wait_for_emulator.sh
  - $PWD/.travis_wait_for_emulator.sh
script: mvn install -Pwith-tests

It indicates the base language of the project - Java, then the jdk version - I use JDK 7 from Oracle.
Before starting the build, we need to install the Android SDK, as well as its components (API, extras, addons, etc.). The bash scripts .travis_install_android.sh and .travis_install_android_sdk.sh are responsible for this . Next, we need to upload some Android artifacts to the local Maven repository - the .travis_install_android_dependencies.sh script , run the emulator - .travis_start_emulator.sh and wait for it to start .travis_wait_for_emulator.sh .

Android installation

#!/bin/sh
# Script installs Android SDK
sudo apt-get update -qq
if [ `uname -m` = x86_64 ]; then sudo apt-get install -qq libstdc++6:i386 lib32z1; fi
wget -O android-sdk.tgz http://dl.google.com/android/android-sdk_r22.0.4-linux.tgz
tar xzf android-sdk.tgz
export ANDROID_HOME=$PWD/android-sdk-linux
export PATH=${PATH}:${ANDROID_HOME}/tools:${ANDROID_HOME}/platform-tools
sudo apt-get install expect
chmod +x $PWD/.travis_install_android_sdk.sh
$PWD/.travis_install_android_sdk.sh

Description: update the list of applications, in the case of 64-bit architecture - install additional packages (see discussion here ). Next, we pump out, unzip the Android SDK and set the environment variables (since the environment variables in the bash script will be reset after its execution, we will have to set them again in .travis.yml).

Install Android SDK components

#!/usr/bin/expect -f
# Script installs Android SDK components
spawn android update sdk --filter tools,platform-tools,build-tools-19.0.0,extra-android-support,android-17,sysimg-17,addon-google_apis-google-17,android-19,sysimg-19,addon-google_apis-google-19,addon-google_apis-google-19,extra-google-play_billing,extra-google-m2repository,extra-google-analytics_sdk_v2,extra-google-gcm,extra-google-google_play_services,extra-google-play_apk_expansion,extra-android-m2repository --no-ui --force --all
expect "Do you accept the license *:"
send -- "y\r"
expect "Do you accept the license *:"
send -- "y\r"
expect "Do you accept the license *:"
send -- "y\r"
expect "Do you accept the license *:"
send -- "y\r"
expect "Do you accept the license *:"
send -- "y\r"
expect "Do you accept the license *:"
send -- "y\r"
expect "Do you accept the license *:"
send -- "y\r"
interact
# WORKAROUND: for some reason we need to download following extras separately (otherwise we will get PkgVersion=2 instead of PkgVersion=2.0.0)
spawn android update sdk --filter extra-google-admob_ads_sdk,extra-google-play_licensing --no-ui --force --all
expect "Do you accept the license *:"
send -- "y\r"
interact

Description: We are trying to install the SDK components we need. Here's a little trick - android update sdk will offer us to agree with the licenses for some components, and since we will not be able to respond (the script is executed remotely), we will have to respond automatically using the spawn command (which was prudently installed before calling this script).
For some reason unknown to me, android differently deflates admob and play_licensing components. If you add them to the first update, then instead of PkgVersion = 2.0.0 there will be PkgVersion = 2, which does not suit us very much (since this variable is responsible for the version of the artifact in the Maven repository).

Install artifacts in the Maven repository

#!/bin/sh
# Scripts installs Android SDK artifacts in local Maven repository
git clone git://github.com/serso/maven-android-sdk-deployer.git
cd ./maven-android-sdk-deployer/
git checkout tags/api-19
mvn install -P4.2
cd ..

Description: download the Maven Android SK Deployer fork using the api-19 tag and run its assembly for Android version 4.2 (I haven’t tested it on 4.4 yet). Why do we pump fork instead of the original? There is no tag in the original, and therefore any changes made to the project code may break our assembly.

Emulator launch

#!/bin/sh
# Scripts starts Android emulator with name 'Default'
echo no | android create avd --force -n Default -t android-17 --abi armeabi-v7a
emulator -avd Default -no-skin -no-audio -no-window &

Description: create a virtual device with the name Default and run it in a separate process.

Next, Travis launches a test install of the project, at the end of which the before_script phase is called, in which we wait for the emulator to start.

Assembly

The final chord is the launch of the assembly. In my case, it is just a call to mvn install with the with-tests profile, which will run the integration tests.
script: mvn install -Pwith-tests


Conclusion


After the project has been successfully configured, each commit should lead to the launch of the assembly with subsequent notification to the mail. As an example, you can see the Android Common Library project with working assemblies in Travis CI .
If you have questions or suggestions - welcome to comment. Thanks for attention.

Also popular now: