Featured Image
Software Development

Android automated testing with Jenkins and Firebase


Imagine 10 developers are working on same project. They push code on repository then manager generate the build and test it. While testing, manager found bugs in the build and he/she failed the build. After that developer fixed the bug and repeat the same process.

If we make this process automated, then we just need to develop feature and fix the bug if we find it after pushing code. We can achieve this by Jenkins and gcloud CLI. So let’s start how we can implement it.


Setting up Jenkins and Plugins

First you need to install Jenkins. You can download Jenkins from here.

Now add Github plug in,Gradle Plug in Android Emulator Plug in from Manage Jenkins > Manage Plugins.

Once the plugin added, create job in Jenkins and choose Freestyle project. We will Configure project letter.

Now download and setup gcloud. Please follow steps from this link.


Now come to Jenkins home page and click on Credential.

Add your credential as below

Scope: Global(Jenkins, nodes, items, all child item, etc)

Username: your github username

Password: your github password

Click on save


Configuring Jenkins Job for Android Automated Testing

Now we will configure job. Click Job and then from left menu click on Configure

In General section click on GitHub project and add project url(in my case it’s gitlab project url)

Now come to the Source code Management section and select Git

Add your Repository url and select your Credentials that you added.

You can also change Branches to build/

Select Build Triggers as per your requirements.


Now we will set Build in job Configure.

Click on Add build step and select Invoke Gradle script

Select Use Gradle Wrapper option

Add tasks like build — stacktrace. If you want to ignore lint add -x lint

build — stacktrace -x lint

build :app:assembleDebugAndroidTest (To create instrumentation test apk)

Now add Execute shell and write below Commands

For login gcloud

gcloud auth activate-service-account — key-file=/home/sanjay/todo-app-1ea25815be22.json

To create .json file check this

After that set your project

gcloud config set project todo-app-d2434(Project ID)

Running Instrumentation Tests on Firebase

To run instrumentation test on Firebase

gcloud firebase test android run — type instrumentation — app app-debug.apk — test app-debug-androidTest.apk — device model=Nexus6,version=21,locale=en,orientation=portrait — device model=Nexus7,version=19,locale=fr,orientation=portrait


Set the artifacts

Now go to the Post-build Actions

Add Archive the artifacts

In Files to archive add **/*.apk

Apply all changes and Build Now to test the implementations. This will fetch your code from git and create a build. Then upload both apk on the Firebase for the instrumentation testing.

Also read: Securing Jenkins: Be Aware of Anonymous User Read Access

author
Sanjay Kakadiya
I am an Android Developer working with Aubergine Solutions. I have expertise in Android, Android Things, Firebase, BLE, Sensors, Payment Gateways, Hardware communication, Rest API, Django Rest Framework, Amazon web services, Alexa Skills.