Mendhak
Mendhak

Reputation: 8775

Run one built-in gradle task before another

I am running some Android instrumentation tests which require that the app I am testing be a fresh install - the app should not already exist on the phone.

I generally do this by running

./gradlew uninstallAll
./gradlew connectedInstrumentTest

However, sometimes I forget to uninstall and my tests fail.

I would like to force uninstallAll to run automatically when I run connectedInstrumentTest. How can I do this?

In my build.gradle, I have tried

connectedInstrumentTest.doFirst {
    uninstallAll.execute()
}

But this gives me

Could not find property 'connectedInstrumentTest' on project ':myapp'.

I tried

connectedInstrumentTest.dependsOn(uninstallAll)

I get the same error.

This likely comes down to my lack of understanding of Gradle. I have searched for this but the only examples seem to be around custom tasks, not built-in tasks that already come with Android projects.

Upvotes: 2

Views: 1773

Answers (2)

Rene Groeschke
Rene Groeschke

Reputation: 28653

usually

connectedInstrumentTest.dependsOn(uninstallAll)

should work, but I think android plugin creates some tasks after the whole buildscript is evaluated. you can try to put the snippet above in an afterEvaluate block:

project.afterEvaluate{
    connectedInstrumentTest.dependsOn(uninstallAll)
} 

cheers, René

Upvotes: 1

Mendhak
Mendhak

Reputation: 8775

@Opal's comment led me to a bit of reading and I eventually got this working

tasks.whenTaskAdded { task ->

    if(task.name.equals("connectedInstrumentTest")){
        task.dependsOn(uninstallAll)
    }
}

I believe the problem was trying to add the dependency too early, but putting it into tasks.whenTaskAdded seems to work just fine. Now when I run connectedInstrumentTest, the app is uninstalled first.

Upvotes: 2

Related Questions