How to Add your First Android Job to Jenkins

How to Add your First Android Job to Jenkins

AndroidQ/A
Fix bugs faster! Log Collection Made Easy
START NOW!

New: We’ve just released a new ebook: the Jenkins Starter Guide, which contains all of the information in this series of four posts and more. Head on over to our Resources page to download.


In case 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: What Is Jenkins and Why Should You Be Using It?

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 an example application using Bugfender SDK in Android. 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.

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

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 to install Jenkins in your server.

Configuring Android App Builds in Jenkins

Step 1: Install Java JDK

sudo apt-get install java-8-openjdk

Step 2: Install Android SDK

Go to https://developer.android.com/studio/index.html#downloads, and grab the link listed in the table for Get just the command line tools > Linux. Don’t download anything, just copy the link.

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

Be A Superhero

Fend off bugs and keep your users safe.

Use Bugfender for Free Today

Step 3: Configure Jenkins

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

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

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

Step 4: Create an Android Job

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

Step 5: Downloading Your Code to Jenkins

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: https://github.com/bugfender/BugfenderSDK-android-sample.git.

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.

Step 6: Configuring Jenkins Build Triggers

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.

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.

Step 7: Build

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:

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

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.

Start Fixing Bugs Faster Now for Free

Bugfender is the best remote logger for mobile apps.

Sign Up

Accelerate Gradle (optional)

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:

mkdir -p /var/lib/jenkins/.gradle

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

Running Unit Tests

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

Running Integration Tests

In order to set up UI tests, you will need to run an emulator. Even though Jenkins has an Android Emulator Plugin, 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:

Step 1: Download the emulator. In order to list the SDKs available, run:

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

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.

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

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

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

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

Step 4: Then we use Supervisord to run the Android emulator as a system service, always in the background. In order to install it:

sudo apt-get install supervisor

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

[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

Step 5: Once this is done, restart supervisord to apply the changes:

sudo service supervisor restart

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:

sudo -iu jenkins android-sdk/platform-tools/adb devices

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

Code:

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

Next Steps

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:


For more posts in this series, check out:

Or for our complete ebook on Jenkins, head over to our Resources page.

This post was co-authored by Sarabeth Flowers Lewis.

Expect the Unexpected! Debug Faster with Bugfender
START FOR FREE

Trusted By

/assets/images/svg/customers/highprofile/deloitte.svg/assets/images/svg/customers/highprofile/volkswagen.svg/assets/images/svg/customers/projects/slack.svg/assets/images/svg/customers/projects/vorwerk.svg/assets/images/svg/customers/cool/riachuelo.svg/assets/images/svg/customers/highprofile/ford.svg/assets/images/svg/customers/highprofile/schneider_electric.svg/assets/images/svg/customers/cool/websummit.svg

Already Trusted by Thousands

Bugfender is the best remote logger for mobile and web apps.

Get Started for Free, No Credit Card Required