Part 2 - Setting up Android build on Jenkins

This is 2nd article in series of articles for setting up an Android build. I hope you have read the previous one here Part 1 – Setting up Android build on Jenkins

Requirements for this tutorial is that you have a working Android Projectwhich builds fineinside Android Studio. Source code for this sample is athttps://github.com/vnextcoderblog/trav. So lets crack on !

Login to Jenkins CI server.

Create the Job

Click 'Create New Jobs" on the home screen below . Once you do that select Freestyle Project and click OK

jenkins-ci-1

jenkins ci 2.png

Setup Project Source

Select Github project and set Project URL as the root where gradlew or gradlew.bat resides.

In my case it is there at https://github.com/vnextcoderblog/trav. Additionally setup GIT in SCM and GIT repository URL ashttps://github.com/vnextcoderblog/trav. In branches to build, you can choose master as of now "*/master " which is just descendent of the GIT Repo root.

jenkins ci 3.png

Setup Build triggers

Build Triggers are parameters which would trigger the build. if you dont setup any , the build need to be triggered manually . We would setup a SCM Polling to poll every 10 mins for any SCM changes and trigger the build. H/10 * * * * means it would be run every 10 mins.

jenkins ci 4.png

Build Step - Update Android SDK

Jenkins doesn't specify any pre-build steps like Circle CI or Travis-CI if you are big fan of those Cloud solutions, However, Jenkins has whole lot of customization over and above those nasty YML files. Under Jenkins, you can run virtually any Shell script / tool .

First buildstep for any Android build should be to get all the build, platform-tools, etc which are needed for the project build. so lets create a Shell script build

Add build step --> Execute Shell

add below line on the step

echo yes | android update sdk --no-ui --all --filter tools,platform-tools,build-tools-24.0.3,android-24,extra-google-m2repository,extra-google-google_play_services,extra-android-support

Now, this step would make sure all the build tools, source libs, m2repo everything is up to date on the $ANDROID_HOME folder before the Gradle build actually kicks in. Do note that --filter only works with --all option . "echo yes" would make sure that the License agreements are accepted automatically by the build. So far I haven't found a way to configure this task to not update the android sdk in case it is already updated. This seems to be a feature request and I have requested the samehttps://code.google.com/p/android/issues/detail?id=224879. Many Stackoverflow questions on this topic are still unanswered.

Build Step - Accept Android License

Add execute shell script step to accept license automatically -

mkdir "$ANDROID_HOME/licenses" || true
echo -e "\n8933bad161af4178b1185d1a37fbf41ea5269c55" > "$ANDROID_HOME/licenses/android-sdk-license"
echo -e "\n84831b9409646a918e30573bab4c9c91346d8abd" > "$ANDROID_HOME/licenses/android-sdk-preview-license"

Build Step - Gradle build.

TheGradle wrapper plugin works same way as the gradle wrapper from within the Android studio. Do select tick mark "make gradlew executable" . This would make sure that execute permissions are applied on the gradlew script after SCM checkout. Under the tasks add "clean build" string to execute clean and build tasks.

jenkins-ci-5

2016-10-16-4

Post-build Action(s).

Jenkins allows to archive the artifacts or even push them to Play store (custom plugins needed). At the moment we wouldarchive the apk files . Click Post-build actions --> add Post-build action --> Archive the artifacts . in the Files to archive enter "**/*.apk". This is a glob which would find out any *.apk files under any file depth and archive them .

Save the build now.

2016-10-16 (5).png

Trigger build.

Click build Now from the build configuration screen. next go to Console Output and which should showthe real build run output

2016-10-16 (6).png

2016-10-16 (7).png

If all goes well you should see "BUILD SUCCESSFUL" message in end like below .

Trav finished

Ref

  1. Accepting android SDK licenses
  2. Android update feature
  3. http://stackoverflow.com/questions/5730815/unable-to-locate-tools-jar - in case you run into 'tools.jar' not found errors, you must remember jdk is required for the builds rather than jre.