Why can't I use gradle task connectedDebugAndr

2019-04-19 15:50发布

问题:

I can refer to connectedCheck task (which came from android plugin) from my build script:

connectedCheck.finalizedBy AndroidShowTestResults

But trying to use connectedDebugAndroidTest (which came from android plugin too)

connectedDebugAndroidTest.finalizedBy AndroidShowTestResults

gives me

Error:(48, 0) Could not find property 'connectedDebugAndroidTest' on project ':app'.

And if I try

task connectedDebugAndroidTest << {print '123'}

it curses me with

Error:Cannot add task ':app:connectedDebugAndroidTest' as a task with that name already exists.

I don't undestand why I cannot refer to connectedDebugAndroidTest?

Available gradle tasks are shown below:

回答1:

The android plugin defers the addition of several tasks especially those that have buildType or flavor names in them till a very late stage of the configuration phase. Which in turn means if you try to refer to these yet-to-be-added-tasks by name, you're likely to see a "does not exist" type error messages. If you want to add dependencies around deferred-created tasks, you should wait until configuration is complete:

gradle.projectsEvaluated {
    connectedDebugAndroidTest.finalizedBy AndroidShowTestResults
}

Alternatively, you can add a listener to task-graph events, so you can do stuff as soon as a certain task is added to task-graph:

tasks.whenTaskAdded { task ->
    if (task.name == 'connectedDebugAndroidTest') {
        task.finalizedBy AndroidShowTestResults
    }
}


回答2:

Try

task connectedTest(dependsOn: ["connectedDebugAndroidTest"]){

}
connectedTest.finalizedBy "AndroidShowTestResults"


回答3:

I think you should try to open test and rebuild.