Mobile Software Test Automation: Building Android Mobile App in Jenkins

In the last post, we walked through creating a Jenkins job that would test an already built app.  This may be useful in some instances, but what we really need in order to round out a fully automated test solution for our mobile apps is to test against an app right after it has been built, but before the app goes through its upload process to HockeyApp, TestFairy, or whatever other process you use.  So let's get started...

Building an Android app

Since it is the official build tool for Android, I am going to show you how to build an Android app using Gradle.  I am going to assume that your Android project is already built using gradle scripts, and this is simply using the command line to tap into an existing build process.  

create new job

Start by creating a new freestyle Jenkins job.  You can name this whatever you want.  I also recommend clicking the "Discard old build" option using the log rotation strategy.  It's not essential, but this will help reduce clutter and save space.  So far, it should look something like this:

configure source code management

Now, you will want to configure your SCM.  This is likely Git, so enter your URL, credentials, and branch you build from.  Some use the default branch of 'master' as the head of dev, others create a branch named 'develop', and so on.  The branch you should use here is whatever your company uses as the main branch for this mobile project.  Although I don't know each of your projects, I would assume it contains some submodules, so in this Jenkins job in the SCM section, under "Additional Behaviours", add "Additional sub-modules behaviours" and check "Recursively update submodules."  

configure build triggers

The next step is to configure your build triggers.  You will want to decide what should cause an app to build, and then a subsequent test to run.  Do you want this based off specific time(s) throughout the day?  Maybe you want to poll for changes and start a build every time a change is checked in?  The answer will greatly depend on your current or desired workflow.  But to be fair, building based off a time slot is not ideal for continuously building software.  If you want to get the most out of a CI setup, and you want your developers to know as immediately as possible when code they checked in broke some functionality, then you should opt for the SCM polling option.  For this setup, you will need to set up a polling cycle.  There is a good description in Jenkins for how to set this up, but for the quick and dirty version, if you want to poll your SCM for changes every 15 minutes enter this in the schedule box:


H/15 * * * *

configure build steps

Now for the final step.  Under the "Build" heading, select the option to add "Execute Shell."  We will first run a gradle clean, and then we will assemble a debug build.  To do this, add the following two lines to your shell:


../../../../../Applications/gradle/bin/gradle clean
../../../../../Applications/gradle/bin/gradle assembleDebug

If you want to display the changes that were made, add another "Execute Shell" box and add:


echo ${CHANGES}

Now, in order for the automated tests to run, we need a resigned version of the app.  To do this, we will need one final "Execute Shell" box with the following two lines:


cd ~/path/to/your/automation/project/folder

calabash-android resign ~/path/to/build/location/build/outputs/apk/yourAppName.apk

move built app to the automation project folder

You may not want to do this and could simply point the calabash-android run command to the built app's location (the same location we used with the resign option above), but I prefer to move the app from that location to my automation project folder with the mv command in another shell window, mainly because it helps keep me organized.  I also add in the major app version and build number, but i'll save that for another day.  The command to do this is:


mv -f ~/path/to/build/location/build/outputs/apk/yourAppName.apk ~/path/to/your/automation/project/folder/android-apks/

Click "Save" and you should be done.  Now comes the easy part...

Triggering Tests to run after build

Remember the job we created last week to test the Android app?  We need to modify that one a little bit.  So click on that job and click "Configure" from the left hand navigation menu.  Under the "Build Triggers" section, select the option for "Build after other projects are built," and type in the name of the Android build project we just created.  That's all there is to it!

Wrapping up

That should complete our session on creating a Jenkins job to build an Android app using existing gradle scripts, moving the app to our automation folder, and then triggering our test job to run when a build completes.  Great job if you have followed along this far!  Next week I will finish the mobile automation section up with creating an iOS build, just like we did with Android today.  See you next week.