How to Add your First Android Job to Jenkins
Bugfender Dec 6 '17
This article was originally posted on our blog.
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: 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
Step 3: Configure Jenkins
Then, log in to Jenkins and configure the Android SDK that you just installed:
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.
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
[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:
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
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:
- Android Emulator Plugin -- 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.
- Android Signing Plugin
- Google Play Android Publisher Plugin
For more posts in this series, check out:
- What Is Jenkins and Why Should You Be Using It?
- How to Install Jenkins in Ubuntu
- How to Add Your First iOS Job (coming soon)
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 here.
This article was originally posted on our blog.