📜 ⬆️ ⬇️

Build Android applications on Travis CI

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

What do we have?



What do we want to get?


The free assembly of the project on kommit with the report on performance on mail. At the same time, we want to run integration tests on the emulator and use additional components of the SDK (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 specifies the base language of the project - Java, then the jdk version - I use JDK 7 from Oracle.
Before we start building, 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 download some Android artifacts into the local Maven repository - the .travis_install_android_dependencies.sh script, run the emulator - .travis_start_emulator.sh and wait for its launch .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 the discussion here ). Next, we download, 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).
')
Installing 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 a little trick - android update sdk will offer us to accept licenses for some components, and since we will not be able to respond (the script is executed remotely), we will have to respond automatically with the help of the spawn command (which was prudently installed before calling this script).
For some unknown reason, android differently extorts the components of admob and play_licensing. 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).

Installing 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: we extort Maven Android SK Deployer fork via the api-19 tag and launch its build for Android version 4.2 (I haven’t yet checked it on 4.4). Why do we extort the fork instead of the original? There is no tag in the original, and therefore any changes made to the project code may break the assembly.

Running emulator

 #!/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 named Default and run it in a separate process.

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

Assembly

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


Conclusion


After the project is successfully configured, each commit should lead to the start of the assembly, followed by notification to the mail. As an example, you can see the Android Common Library project with working assemblies in Travis CI .
If you have any questions or suggestions, welcome to comments. Thanks for attention.

Source: https://habr.com/ru/post/200646/


All Articles