Skip to content

New Android project with Jenkins

Semih edited this page Apr 28, 2017 · 4 revisions

Setting up an automatic build in Jenkins for an Android project is quite straightforward. Before starting, make sure that you have properly installed Jenkins and Docker. And it is advisable that you open this and this repositories in a separate tab as example cases.

Extra files

To simplify the Jenkins configuration process and make this configuration editable without having to use Jenkins interface, two shell script files are introduced. These are kedi.sh and build.sh. The idea is that Jenkins downloads these two files with the rest of the repository and then calls kedi.sh to start the process. Then, kedi.sh runs build.sh in a container where the magic happens.

kedi.sh

This file starts a new Docker instance and mounts working directory to this instance. Before this, it creates a random name for the Docker instance and it uses this name to remove the instance after the building process is done. This way the build is always done in a fresh environment. Other than that, it just calls the build script, build.sh.

#!/bin/bash -e

KEDI=$RANDOM

echo $KEDI

sudo docker run -v $(pwd):/builddir --name $KEDI ubuntu /bin/bash -c "/builddir/build.sh"

sudo docker rm $KEDI

Here, the #!/bin/bash -e line means that if any of the lines return a failure, the whole script returns a failure. Normally, the script returns whatever the very last line returns (that is, if no return value is specified). This is necessary to let Jenkins know if the build has failed.

build.sh

This file should contain what is necessary to build your script in a fresh (so fresh that even wget does not exist!) ubuntu environment. You can use the build scripts from example projects (see the very first paragraphs for the links or just switch tabs if you already have them opened). Although the contents of your build script might be different, it will probably be quite similar to what those projects have.

#!/bin/sh

cd /builddir

apt-get update

apt-get install -y default-jre default-jdk wget unzip

BUILD_DIR=$(pwd)

mkdir /sdk

cd /sdk

wget https://dl.google.com/android/repository/tools_r25.2.3-linux.zip

unzip tools_r25.2.3-linux.zip

(while sleep 3; do echo "y"; done) | ./tools/bin/sdkmanager "platform-tools"

(while sleep 3; do echo "y"; done) | ./tools/bin/sdkmanager "build-tools;25.0.2"

export LC_CTYPE=C.UTF-8

export ANDROID_HOME=/sdk/

cd $BUILD_DIR

./gradlew

./gradlew test

./gradlew assemble

echo "Success?"

This file is quite straightforward. It first downloads some packages from package manager, then it downloads SDK from google and unzips it. After it installs platform tools and build tools (those funny while loops are there to accept license agreements for you) and then it starts building and testing processes. Instrumented tests are not done in this script as this text is written. So, better check the most recent build script from the given sample project repositories.

export commands are really important. Without the LC_CTYPE variable you will get some encoding errors during the process. The ANDROID_HOME variable tells gradle where the SDK is located.

...to be continued

The Pipeline

Radiators

Clone this wiki locally