How to run a test before creating a signed apk?

I believe that Android Studio will run the test before generating a signed apk.

But AS did not do this for me. Not bad, before I pack my apk, I need to run the tests myself.

I'm not sure if dependOn or some other way can help me. I'm not sure my version of build.gradle has errors.

Some relevant code in gradle might look like this:

defaultConfig { applicationId "com.xx.xx" versionCode getDefaultVersionCode() minSdkVersion 19 targetSdkVersion 19 } dependencies { testCompile 'org.robolectric:robolectric:3.0' testCompile 'junit:junit:4.12' } 

I did not write testOption .

My directory is similar to this (contents before their package name):

enter image description here

+6
source share
2 answers

To run all available tests when creating a release, the task that creates the release (for example, assembleRelease ) depends on the test tasks:

 android { // ... } afterEvaluate { assembleRelease.dependsOn testReleaseUnitTest, connectedAndroidTest } 

Closing afterEvaluate is done after evaluation (when android tasks were created). At this time, android tasks can be specified as variables.

Instead of testReleaseUnitTest you can simply use test , which runs unit tests for all options.

Keep in mind that by default there are no benchmarks for the release version of your application (build with assembleRelease ). Thus, in the above example, connectedAndroidTest runs control tests for the debug version only.

+5
source

I am not familiar with Android development, but I think that you could achieve your intention by adding this somewhere to your build.gradle:

 sign.dependsOn test 

Where the sign is the signature of the apk task (same name as from gradle tasks ).

+2
source

All Articles