Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute Android Tests with gradle before each "release" build?

I have some android instrumented tests under AndroidTests directory in Android Studio. Of course I can manually execute the tests but I need to execute all the tests in the suite after each build ("release" type of build, not during normal debug build). I need to do that because I'd like to validate the new code against the tests before releasing the new app's apk. How to do that? I google it but I didn't find a proper solution yet.

Any idea?

like image 924
Daniele Avatar asked Dec 05 '22 16:12

Daniele


1 Answers

Finally I managed to do that. I have added the following configuration to my build.gradle file in my android studio project:

// execute android tests before realising a new apk
tasks.whenTaskAdded { task ->
    if (task.name == 'assembleRelease')
        task.dependsOn('connectedAndroidTest')
}

android {
signingConfigs {
    release {
        keyAlias 'key'
        keyPassword 'password'
        storeFile file('/path/to/release_keystore.jks')
        storePassword 'password'
    }

}
buildTypes {
    debug {
        signingConfig signingConfigs.release
    }
    release {
        signingConfig signingConfigs.release
    }
}

After doing that I'm able to run all android tests when building a release apk. If the tests are failing no apk is built.

like image 121
Daniele Avatar answered Dec 10 '22 11:12

Daniele