Android – Run android unit tests & instrumentation tests on Jenkins (Gradle)


I have some unit and instrumentation tests in my androidTest folder.

I'm trying to run these tests on my local Jenkins.

I've successfully configured my project with Jenkins and I've also created an emulator for the instrumentation tests.

So far all the resources I've seen only focus on ant and earlier versions of Gradle. The syntax for writing tasks has slightly changed in the current version and I'm confused about this.

Here is my build.gradle file:

apply plugin: ''

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "myPackageName"
        minSdkVersion 16
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), ''

dependencies {
    compile ''
    compile ''

Best Solution

You can write a jenkis script like this:

stage('Build & Install') {
//Build the apk and the test apk which will run the tests on the apk
                sh 'chmod +x gradlew && ./gradlew --no-daemon --stacktrace clean :app:assembleDevDebug :app:assembleDevDebugAndroidTest'

 stage('Tests') {
//Start all the existing tests in the test package 
sh './gradlew --no-daemon --debug :app:connectedDevDebugAndroidTest' 

This should install the apk and the test apk into the device and start the test cases in the test apk on installation.

Here I have given DevDebug as I have a flavor constant called Dev and build type is Debug. If you don't have any of those, you don't use them.

Related Question