user2545386
user2545386

Reputation: 457

How can I run test before generate signed apk?

I consider that Android Studio will run test before generate signed apk.

But AS didn't do that for me.It's not nice before package my apk, I need run the tests myself.

I'm not sure if dependsOn or other way can help me.I'm not sure weather my build.gradle has mistakes.

Some relevant code in gradle maybe 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 didn't write testOption.

My directory is like this(content before them is package name):

enter image description here

Upvotes: 8

Views: 1973

Answers (2)

thaussma
thaussma

Reputation: 9886

To run all available tests, when building a release, make the task, that builds the release (e.g. assembleRelease) depend on the test tasks:

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

The afterEvaluate closure is executed after evaluation (when the android tasks have been created). At this time the android tasks can be referenced as variable.

Instead of testReleaseUnitTest you can just use test, which runs unit tests for all variants.

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

Upvotes: 9

judoole
judoole

Reputation: 1422

I'm not familiar with Android development, but I think you could achieve your intent with adding this somewhere in your build.gradle:

sign.dependsOn test

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

Upvotes: 2

Related Questions