Travis

Build an Android Project

This guide covers build environment and configuration topics specific to Android projects. Please make sure to read our Onboarding and General Build configuration guides first.

Android builds are not available on the macOS environment.

CI Environment for Android Projects #

Overview #

Android builds are officially supported only on our Trusty Build environment at this time; hence, you’ll need to explicitly specify dist: trusty in your .travis.yml file.

Travis CI environment provides a large set of build tools for JVM languages with multiple JDKs, Ant, Gradle, Maven, sbt and Leiningen.

By setting

language: android
dist: trusty

in your .travis.yml file, your project will be built in the Android environment which provides Android SDK Tools 25.2.3.

Here is an example .travis.yml for an Android project:

language: android
dist: trusty
android:
  components:
    # Uncomment the lines below if you want to
    # use the latest revision of Android SDK Tools
    # - tools
    # - platform-tools

    # The BuildTools version used by your project
    - build-tools-26.0.2

    # The SDK version used to compile your project
    - android-26

    # Additional components
    - extra-google-google_play_services
    - extra-google-m2repository
    - extra-android-m2repository

    # Specify at least one system image,
    # if you need to run emulator(s) during your tests
    - sys-img-x86-android-26
    - sys-img-armeabi-v7a-android-17

Install Android SDK Components #

In your .travis.yml, you can define the list of SDK components to be installed, as illustrated in the following example:

language: android
dist: trusty
android:
  components:
    - build-tools-26.0.2
    - android-26
    - extra

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).

Deal with Licenses #

By default, Travis CI will accept all the requested licenses, but it is also possible to define a white list of licenses to be accepted, as shown in the following example:

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

For more flexibility, the licenses can also be referenced with regular expressions (using Tcl syntax as expect command is used to automatically respond to the interactive prompts).

Pre-installed Components #

While the following components are preinstalled, the exact list may change without prior notice. To ensure the stability of your build environment, we recommend that you explicitly specify the required components for your project.

  • tools
  • platform-tools
  • build-tools-25.0.2
  • android-25
  • extra-google-google_play_services
  • extra-google-m2repository
  • extra-android-m2repository

Create and Start an Emulator #

Warning: At the moment, these steps are not fully supported by Travis CI Android builder.

If you feel adventurous, you may use the script /usr/local/bin/android-wait-for-emulator and adapt your .travis.yml to make this emulator available for your tests. For example:

# Emulator Management: Create, Start and Wait
before_script:
  - echo no | android create avd --force -n test -t android-22 --abi armeabi-v7a -c 100M
  - emulator -avd test -no-audio -no-window &
  - android-wait-for-emulator
  - adb shell input keyevent 82 &

Dependency Management #

Travis CI Android builder assumes that your project is built with a JVM build tool like Maven or Gradle that will automatically pull down project dependencies before running tests without any effort on your side.

If your project is built with Ant or any other build tool that does not automatically handle dependencies, you need to specify the exact command to run using install: key in your .travis.yml, for example:

language: android
dist: trusty
install: ant deps

Default Test Command for Maven #

If your project has pom.xml file in the repository root but no build.gradle, Maven 3 will be used to build it. By default it will use

mvn install -B

to run your test suite. This can be overridden as described in the general build configuration guide.

Default Test Command for Gradle #

If your project has build.gradle file in the repository root, Gradle will be used to build it. By default it will use

gradle build connectedCheck

to run your test suite. If your project also includes the gradlew wrapper script in the repository root, Travis Android builder will try to use it instead. The default command will become:

./gradlew build connectedCheck

This can be overridden as described in the general build configuration guide.

Caching #

A peculiarity of dependency caching in Gradle means that to avoid uploading the cache after every build you need to add the following lines to your .travis.yml:

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

Default Test Command #

If Travis CI cannot detect Maven or Gradle files, Travis CI Android builder will try to use Ant to build your project. By default, it will use

ant debug install test

to run your test suite. This can be overridden as described in the general build configuration guide.

Test against Multiple JDKs #

As for any JVM language, it is also possible to test against multiple JDKs.

Build Matrix #

For Android projects, env and jdk can be given as arrays to construct a build matrix.

Build Android projects on new build environments #

The dist: trusty build environment is the only supported build environment for Android but if you would like to build on newer build environments e.g. dist: jammy, you can exercise your access to the Travis CI build environments and install required packages and tools. An example .travis.yml config can be reviewed below:

os: linux
language: java
jdk: openjdk17

env:
 global:
  - ANDROID_HOME=$HOME/travis-tools/android
  - ANDROID_SDK_ROOT=$HOME/travis-tools/android

before_install:
 # PREPARE FOR ANDROID SDK SETUP
 - mkdir -p $HOME/travis-tools/android && mkdir $HOME/.android && touch $HOME/.android/repositories.cfg
 - cd $ANDROID_HOME && wget -q "https://dl.google.com/android/repository/commandlinetools-linux-10406996_latest.zip" -O commandlinetools.zip
 - unzip -q commandlinetools.zip && cd cmdline-tools
 - mv * tools | mkdir tools && cd $TRAVIS_BUILD_DIR

 # SETUP PATH(s)
 - export PATH=$ANDROID_HOME/cmdline-tools/tools/bin/:$PATH
 - export PATH=$ANDROID_HOME/emulator/:$PATH
 - export PATH=$ANDROID_HOME/platform-tools/:$PATH

install:
 # INSTALL REQUIRED ANDROID SDK TOOLS
 - sdkmanager --sdk_root=$ANDROID_HOME --list | awk '/Installed/{flag=1; next} /Available/{flag=0} flag'
 - yes | sdkmanager --sdk_root=$ANDROID_HOME --install "platform-tools" "platforms;android-33" "build-tools;33.0.2" "emulator" "system-images;android-33;google_apis;x86_64"
 - sdkmanager --list --sdk_root=$ANDROID_HOME | awk '/Installed/{flag=1; next} /Available/{flag=0} flag'
 # CREATE AVD
 - echo "no" | avdmanager --verbose create avd --force --name "my_android_33" --package "system-images;android-33;google_apis;x86_64" --tag "google_apis" --abi "x86_64"
 - sudo chmod -R 777 /dev/kvm
 # Start emulator in background and wait for it to start
 - adb kill-server && adb start-server &
 - sleep 15 # sleep values may require adjusting depending on the specific build environment
 - emulator @my_android_33 -no-audio -no-window &
 - sleep 60

script:
 - sdkmanager --list --sdk_root=$ANDROID_HOME | awk '/Installed/{flag=1; next} /Available/{flag=0} flag'
 - adb devices
 - .....

Examples #

Build Config Reference #

You can find more information on the build config format for Android in our Travis CI Build Config Reference.