为什么我不能在构建脚本中使用gradle任务connectedDebugAndroidTest? [英] Why can't I use gradle task connectedDebugAndroidTest in my build script?
问题描述
我可以从我的构建脚本中引用 connectedCheck 任务(来自android插件):
connectedCheck.finalizedBy AndroidShowTestResults
但试图使用 connectedDebugAndroidTest 也可以使用android插件)
connectedDebugAndroidTest.finalizedBy AndroidShowTestResults
给我
错误:(48,0)找不到属性'connectedDebugAndroidTest'
如果我尝试
任务connectedDebugAndroidTest<< {print'123'}
诅咒我
错误:无法添加任务':app:connectedDebugAndroidTest'作为具有该名称的任务。
我不明白为什么我不能引用 connectedDebugAndroidTest ?
可用的gradle任务如下所示:
android插件推迟添加几项任务,尤其是那些在其中具有buildType或风味名称的任务,直到配置阶段的非常晚期阶段。这反过来意味着如果您试图通过名称来引用这些尚未添加的任务,那么您可能会看到不存在类型的错误消息。如果您想添加延迟创建任务的依赖关系,那么您应该等到配置完成:
gradle.projectsEvaluated {
connectedDebugAndroidTest.finalizedBy AndroidShowTestResults
code $ <$ $ p
或者,您可以将侦听器添加到task-图形事件,因此只要将任务添加到任务图中就可以完成任务:
tasks.whenTaskAdded {task - >
if(task.name =='connectedDebugAndroidTest'){
task.finalizedBy AndroidShowTestResults
}
}
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:
解决方案 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
}
}
这篇关于为什么我不能在构建脚本中使用gradle任务connectedDebugAndroidTest?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!