How to Add your First Android Job to Jenkins
Tags: jenkins, ci, android
Published: 2017-12-06 13:46:45

---
title: How to Add your First Android Job to Jenkins
published: true
description: Part three in our series on Jenkins
tags: jenkins, ci, android
cover_image: https://bugfender.com/wp-content/uploads/2017/11/bugfender-adding-android-job-to-jenkins.jpg
canonical_url: https://bugfender.com/blog/how-to-add-your-first-android-job-to-jenkins/
---

*This article was originally posted on our [blog](https://bugfender.com/blog/how-to-add-your-first-android-job-to-jenkins/).*

Incase you missed our first couple of posts in this series, or if you aren't sure just what Jenkins or CI is, head back and read: <a href="https://bugfender.com/blog/what-is-jenkins-and-why-should-you-be-using-it/">What Is Jenkins and Why Should You Be Using It?</a>

<hr style="margin: 40px 0;">

This is a step-by-step guide for how to set up Jenkins to build and test Android apps. In this demo we’ll use <a href="https://github.com/bugfender/BugfenderSDK-android-sample" rel="noopener" target="_blank">an example application using Bugfender SDK in Android</a>. You’ll obviously want use your own built Android application or library. The steps for adding your own Android project to Jenkins will be the same.

<em>Building iOS apps? Keep an eye out for the next article in this series coming soon.</em>

First off, you’ll need to prepare the machine to compile and run Android applications. If you haven’t already installed Jenkins, you can follow the steps in the previous post <link to Ubuntu article> to install Jenkins in your server.

<h2>Configuring Android App Builds in Jenkins</h2>

<h3>Step 1: Install Java JDK</h3>

<pre class="no-wrap">sudo apt-get install java-8-openjdk</pre>

<h3>Step 2: Install Android SDK</h3>

Go to <a href="https://developer.android.com/studio/index.html#downloads" rel="noopener" target="_blank">https://developer.android.com/studio/index.html#downloads</a>, and grab the link listed in the table for <strong>Get just the command line tools</strong> > Linux. Don’t download anything, just copy the link.

<pre class="no-wrap">sudo apt-get install unzip

# here you paste the link you grabbed in the developer.android.com site

sudo -iu jenkins wget https://dl.google.com/android/repository/sdk-tools-linux-3859397.zip

sudo -iu jenkins mkdir android-sdk

sudo -iu jenkins unzip sdk-tools-linux-3859397.zip -d android-sdk

# this step is important to accept the Android SDK license

yes |sudo -iu jenkins android-sdk/tools/bin/sdkmanager --licenses</pre>

<h3>Step 3: Configure Jenkins</h3>

Then, log in to Jenkins and configure the Android SDK that you just installed:

Open <code>https://your-ci-server-name.com</code> on your browser. You’ll see something like this:

<img src="https://bugfender.com/wp-content/uploads/2017/11/jenkins-3-1-welcome.png" alt="" width="1200" height="819" class="aligncenter size-full wp-image-4611" />

<ul>
	<li>Go to Manage Jenkins > Configure System</li>
	<li>Check "Environment variables"</li>
	<li>Add Name: ANDROID_HOME</li>
	<li>Add Value: /var/lib/jenkins/android-sdk</li>
	<li>Click "Apply" then “Save”</li>
</ul>

<img src="https://bugfender.com/wp-content/uploads/2017/11/jenkins-3-2-global-properties.png" alt="" width="798" class="aligncenter size-full wp-image-4613" style="padding: 10px; border: 1px solid #ccc;" />

<h3>Step 4: Create an Android Job</h3>

Now go back to the home page. Click on <strong>New Item</strong>. Enter your project name and select “Freestyle project.”

<img src="https://bugfender.com/wp-content/uploads/2017/11/jenkins-3-3-new-item.png" alt="" width="1200" height="819" class="aligncenter size-full wp-image-4616" />

<h3>Step 5: Downloading Your Code to Jenkins</h3>

Next, you need to add a link to your repository so that Jenkins can download your code. Specify the Git URL of your repository in the Source Code Management section. As mentioned, we’re going to use this sample repository: <a href="https://github.com/bugfender/BugfenderSDK-android-sample.git">https://github.com/bugfender/BugfenderSDK-android-sample.git</a>. 

If you have a Mercurial or Subversion repository, they work the exact same way. If you have a private repository, you can also create SSH keys to access your repository with the git protocol.

<img src="https://bugfender.com/wp-content/uploads/2017/11/jenkins-3-4-source-code-management.png" alt="" width="1200" height="819" class="aligncenter size-full wp-image-4619" />

<h3>Step 6: Configuring Jenkins Build Triggers</h3>

You can also specify Build Triggers that will build the project automatically for you. It is best to use a hook in order to trigger builds automatically when someone pushes code to the repository.

<img src="https://bugfender.com/wp-content/uploads/2017/11/jenkins-3-5-build-triggers.png" alt="" width="305" height="200" class="aligncenter size-full wp-image-4621" style="padding: 10px; border: 1px solid #ccc;" />

For BitBucket, Gitlab and most Git providers, you can use “Trigger builds remotely” option. This will give a URL that you can configure as a webhook in your provider to automatically start a build.

For GitHub users, the easiest is to find and install the “GitHub plugin” (in Manage Jenkins > Manage Plugins) and a GitHub specific option will appear. This will install the webhook for you.

<h3>Step 7: Build</h3>

Once you have the source code in Jenkins, it’s time to build. If your project has a Gradle wrapper, that’s what you should use. In our case, we’ll create an Invoke Gradle script build step like this:

<img src="https://bugfender.com/wp-content/uploads/2017/11/jenkins-3-6-build.png" alt="" width="1416" height="593" class="aligncenter size-full wp-image-4623" style="padding: 10px; border: 1px solid #ccc;" />

If you have an Ant-based project, there is also an <strong>Invoke Ant</strong> build step or for maximum flexibility you can use the <strong>Execute shell</strong> build step.

<img src="https://bugfender.com/wp-content/uploads/2017/11/jenkins-3-7-invoke-ant.png" alt="" width="344" height="222" class="aligncenter size-full wp-image-4624" style="padding: 10px; border: 1px solid #ccc;" />

Test your build by pressing “Save”

And that’s it. Your first Android project has been added to Jenkins. Now you may want to consider accelerating Gradle and setting up automated testing in Jenkins.

<h2>Accelerate Gradle (optional)</h2>

If you’re running builds based on Gradle, you can save some build time by running Gradle as daemon. In order to get it running do the following:

<pre>mkdir -p /var/lib/jenkins/.gradle

echo org.gradle.daemon=true | sudo -iu jenkins tee -a /var/lib/jenkins/.gradle/gradle.properties</pre>

<h2>Running Unit Tests</h2>

If you want to run unit tests, you can do so by adding another build step with the test Gradle task, like this:

<img src="https://bugfender.com/wp-content/uploads/2017/11/jenkins-3-8-invoke-gradle.png" alt="" width="744" height="285" class="aligncenter size-full wp-image-4716" style="padding: 10px; border: 1px solid #ccc;" />

<h2>Running Integration Tests</h2>

In order to set up UI tests, you will need to run an emulator. Even though Jenkins has an <strong>Android Emulator Plugin</strong>, we have found that it has not been updated and is no longer working with the latest versions of the Android SDK. Here is our suggested workaround:

<strong>Step 1:</strong> Download the emulator. In order to list the SDKs available, run:

<pre>sudo -iu jenkins android-sdk/tools/bin/sdkmanager --list --verbose</pre>

Quick note on selecting the right image for you: x86 based images run faster but also need hardware acceleration. You might have to enable KVM acceleration (sudo modprobe kvm) in your server and your server can not be a virtual machine. For this reason we’re choosing an ARM image, which runs slower but works.

<strong>Step 2:</strong> In our case, we’re looking to run our application on Android SDK level 25, so <strong>system-images;android-25;google_apis;armeabi-v7a</strong> seems suitable, then we install it this way:

<pre>sudo -iu jenkins android-sdk/tools/bin/sdkmanager 'system-images;android-25;google_apis;armeabi-v7a'</pre>

<strong>Step 3:</strong> Once installed, we create an Android Virtual Device (an emulator instance) with it:

<pre>echo no | $ANDROID_SDK_ROOT/tools/bin/avdmanager -v create avd --force --package 'system-images;android-25;google_apis;armeabi-v7a' --name Android25 --tag google_apis --abi armeabi-v7a</pre>

Step 4: Then we use <a href="http://supervisord.org" rel="noopener" target="_blank">Supervisord</a> to run the Android emulator as a system service, always in the background. In order to install it: 

<pre>sudo apt-get install supervisor</pre>

Then, create a configuration file in <code>/etc/supervisor/conf.d/emulator.conf</code>:

<pre>[program:emulator]

command=/var/lib/jenkins/android-sdk/emulator/emulator -avd Android25 -no-window -noaudio -no-boot-anim -accel on -ports 5556,5557

autostart=true

user=jenkins

environment=ANDROID_SDK_ROOT=/var/lib/jenkins/android-sdk</pre>

<strong>Step 5:</strong> Once this is done, restart supervisord to apply the changes:

<pre>sudo service supervisor restart</pre>

The emulator should start in the background. It might take 1-2 minutes. You will see the emulator device appear in the devices list when it’s ready:

<pre>sudo -iu jenkins android-sdk/platform-tools/adb devices</pre>

<strong>Step 6:</strong> In the Jenkins job, add an Execute shell build step like this:

<img src="https://bugfender.com/wp-content/uploads/2017/11/jenkins-3-9-execute-shell.png.png" alt="" width="744" height="262" class="aligncenter size-full wp-image-4743" style="padding: 10px; border: 1px solid #ccc;" />

Code:

<pre>ANDROID_SERIAL=emulator-5556

# wait for emulator to be up and fully booted, unlock screen

$ANDROID_HOME/platform-tools/adb wait-for-device shell 'while [[ -z $(getprop sys.boot_completed) ]]; do sleep 1; done; input keyevent 82'

./gradlew connectedAndroidTest</pre>

<h2>Next Steps</h2>

There are more options like running an Android emulator, installing the application, and running various tests on it.

In addition, there are some really interesting plugins that you can play with:

<ul>
	<li><a href="https://wiki.jenkins.io/display/JENKINS/Android+Emulator+Plugin" rel="noopener" target="_blank">Android Emulator Plugin</a> -- At the moment, this plugin is not properly working with latest Android SDK. But our best guess is that they will fix it and it will be working again soon. As an alternative, you can use an older SDK version.</li>
	<li><a href="https://wiki.jenkins.io/display/JENKINS/Android+Signing+Plugin" rel="noopener" target="_blank">Android Signing Plugin</a></li>
	<li><a href="https://wiki.jenkins.io/display/JENKINS/Google+Play+Android+Publisher+Plugin" rel="noopener" target="_blank">Google Play Android Publisher Plugin</a></li>
</ul>

<hr style="margin: 40px 0;">

For more posts in this series, check out:

<ul>
<li><a href="https://bugfender.com/blog/what-is-jenkins-and-why-should-you-be-using-it/">What Is Jenkins and Why Should You Be Using It?</a></li>
<li><a href="https://bugfender.com/blog/how-to-install-jenkins-in-ubuntu/">How to Install Jenkins in Ubuntu</a></li>
<li>How to Add Your First iOS Job (coming soon)</li>
</ul>

<hr style="margin: 40px 0;">

<p class="text-p">This post was edited by Sarabeth Flowers Lewis, a freelance writer and co-founder of Lewis Commercial Writing, specializing in SEO and direct response content creation. She currently works remotely with her ginger husband writing for tech, nonprofits, and real estate. See more of her work <a href="https://www.lewiscommercialwriting.com/" rel="noopener" target="_blank">here</a>.</p>

*This article was originally posted on our [blog](https://bugfender.com/blog/how-to-add-your-first-android-job-to-jenkins/).*