In part one of our series, we established the process around creating an Android application using Red Hat’s Mobile Application Platform and adding unit tests. Now, we’ll walk through how to use Jenkins for continuous integration on this app.
Pre-requisites for this tutorial
- RHMAP Instances
- Jenkins installed on Fedora
- Android Studio setup on a development machine.
The following are the topics covered in today’s post:
- Setup Jenkins to run Android Unit Test
- Install Jenkins plugins
- Install Android SDK and Tools
- Setup SSH on RHMAP and Jenkins
- Create an Android Emulator from command line
- Create a Jenkins project to run Android Unit Tests
Setup Jenkins to run Android Unit Test
This tutorial assumes Jenkins and git tools are installed on a Fedora machine.
Install Jenkins plugins
We need to install all the plugins needed to run Android build. From the Jenkins portal go to Manage Jenkins → Manage plugins→ Available and select the following
- Android Emulator Plugin
- Git plugin
- Gradle Plugin
Click Install without restart.
Install Android SDK and Tools
The first step is to download and install the android sdk and tools. Follow these steps:
tar zxvf android-sdk_r24.4.1-linux.tgz
Now that the android sdk installed, we need to setup the environment variables to point to the sdk. In a terminal either enter
Or depending on your terminal shell type you can edit
/etc/profile.d/android.sh and add in the above commands.
Next we need to run commands to install different android tools:
- Update the android sdk and accept agreements
android update sdk --no-ui
- Install the Android SDK build-tools
- Find the item number
android list sdk --all
- Look for a matching version xx.xx.xx to your build.gradle file
- Install the right version build tools
android update sdk -u --all --filter <#item number >
- Find the item number
- Repeat the same process to install the correct Android SDK, by matching the SDK number in gradle.build in compileSdkVersion
- Give Jenkins access to the sdk
sudo chmod -R 755 /opt/android-sdk-linux
- We also need to setup Jenkins to point to the sdk. From the jenkins web portal Manage Jenkins→ Configure system→ Android sdk root→ type in
/opt/android-sdk-linuxand select “Automatically install Android components when required”
- Add a specific gradle version to Jenkins configuration
- Gradle plugin in Jenkins is not enough, you must also go to:
- Jenkins→ Manage Jenkins→ Configure Jenkins→ Configure system.
- Under “Gradle Installations” type in name (it appears as version in project configuration), check “Install automatically” and select version. Then you can select that “Gradle version” in project configuration.
- If your fedora machine is running on a 64 bit machine you need to install 32bit libraries
yum install libstdc++.i686
yum install zlib.i686
- Install JDK and set JAVA_HOME
yum install java-1.8.0-openjdk-devel
Setup SSH on RHMAP and Jenkins
For Jenkins to be able to perform a git clone of our RHMAP application we need to create and upload the SSH public key to RHMAP. In a terminal on your Fedora machine generate a new key using ssh-keygen and when asked enter
/var/lib/jenkins/.ssh/<anyname> as the location of the keys. Now that the keys are generated we need to make sure they are owned by Jenkins and not the user we are logged into Fedora.
Run the following command:
chown Jenkins /var/lib/jenkins/.ssh/<anyname>.*
Now that we have keys generated, log into RHMAP instance and go to admin→ users →Create and create a new user ID = jenkins with a valid password an email address. Give user Jenkins the right access to read any projects needed
Then logout and login as “Jenkins” user and upload the public key in Settings→ SSH Key Management→ Add New Key. At this point we have SSH git access setup on RHMAP portal but we still need to create an SSH profile in Jenkins. From the Jenkins portal go to Jenkins→ Credentials→ Global Credentials→ Add Credential→ Fill in the form as seen below
Create an Android Emulator from command line
On your fedora machine we need to create an android emulator that can be used by Jenkins. In a terminal run
- List the available targets
android list target
- Create an avd for one of the targets, this command choses id=17 for abi armeabi-v7a
android create avd -n android-23-emulator -t 17 --abi default/armeabi-v7a
Create a Jenkins project to run Android Unit Tests
Time to create a new Jenkins project that can build and run Android tests. From the Jenkins portal click New Item→ Freestyle project and fill in the fields as shown below
- Set Git to use jenkins credentials
- Use Gradle Wrapper with Tasks clean, assemble, test, connectedAndroidTest
- Install APK file
- Archive artifacts
- Publish JUnit test result reports for both Android unit test and Instrumented unit tests
Save configuration and Build Now. If the build succeeds from the Status page you should be able to see your artifacts and test results.
Last updated: March 16, 2023