在 Android/Gradle 中如何定义仅在构建特定 buildType/buildVariant/productFlavor (v0.10+) 时运行的任务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22743663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In Android/Gradle how to define a task that only runs when building specific buildType/buildVariant/productFlavor (v0.10+)
提问by Patrick Favre
Android Plugin for Gradle generates for every BuilType/Flavor/BuildVariant a task. The problem is that this task will be generated dynamically and thus won't be available as a dependency when defining a task like this:
Android Plugin for Gradle 为每个 BuilType/Flavor/BuildVariant 生成一个任务。问题是这个任务将动态生成,因此在定义这样的任务时将无法作为依赖项使用:
task myTaskOnlyForDebugBuildType(dependsOn:assembleDebug) {
//do smth
}
A proposed workaround from this answerwould be this
这个答案中提出的解决方法是这样的
task myTaskOnlyForDebugBuildType(dependsOn:"assembleDebug") {
//do smth
}
or this
或这个
afterEvaluate {
task myTaskOnlyForDebugBuildType(dependsOn:assembleDebug) {
//do smth
}
}
But both didn't work for me.
但两者都不适合我。
回答by Patrick Favre
Here is a full example on how to do this inspired by this post:(android plugin v.0.9.2 and gradle 1.11 at the time of writing)
这里是一个完整的例子,说明如何在这篇文章的启发下做到这一点:(撰写本文时的 android 插件 v.0.9.2 和 gradle 1.11)
We are going to define a task that only runs when we build a "debugCustomBuildType"
我们将定义一个仅在构建“debugCustomBuildType”时运行的任务
android {
...
buildTypes {
debugCustomBuildType {
//config
}
}
}
Define the task that should only be executed on a specific builtType/variant/flavor
定义只应在特定的 builtType/variant/flavor 上执行的任务
task doSomethingOnWhenBuildDebugCustom {
doLast {
//task action
}
}
Dynamically set the dependency when the tasks are added by gradle
gradle添加任务时动态设置依赖
tasks.whenTaskAdded { task ->
if (task.name == 'generateDebugCustomBuildTypeBuildConfig') {
task.dependsOn doSomethingOnWhenBuildDebugCustom
}
}
Here we use the "generateBuildConfig" task, but you can use any task that works for you (also see official docs)
这里我们使用“generateBuildConfig”任务,但您可以使用任何适合您的任务(另请参阅官方文档)
- processManifest
- aidlCompile
- renderscriptCompile
- mergeResourcess.
- mergeAssets
- processResources
- generateBuildConfig
- javaCompile
- processJavaResources
- assemble
- 进程清单
- 辅助编译
- 渲染脚本编译
- 合并资源。
- 合并资产
- 进程资源
- 生成构建配置
- java编译
- 进程Java资源
- 集合
Don't forget to use the buildTypeSpecific task (generateBuildConfig
vs. generateDebugCustomBuildTypeBuildConfig
)
不要忘记使用 buildTypeSpecific 任务(generateBuildConfig
vs. generateDebugCustomBuildTypeBuildConfig
)
And that's it. It's a shame this workaround isn't well documented because for me this seems like one of the simplest requirements for a build script.
就是这样。遗憾的是,这种解决方法没有得到很好的记录,因为对我来说,这似乎是构建脚本最简单的要求之一。
回答by Juozas Kontvainis
I achieved it like this:
我是这样实现的:
android {
ext.addDependency = {
task, flavor, dependency ->
def taskName = task.name.toLowerCase(Locale.US)
if (taskName.indexOf(flavor.toLowerCase(Locale.US)) >= 0) {
task.dependsOn dependency
}
}
productFlavors {
production {
}
other
}
task theProductionTask << {
println('only in production')
}
tasks.withType(JavaCompile) {
compileTask -> addDependency compileTask, "production", theProductionTask
}
}
To be frank, I don't which locale is used to generate names for compile taks so toLowerCase(Locale.US)
may be counterproductive.
坦率地说,我不知道使用哪个语言环境来生成编译任务的名称,因此toLowerCase(Locale.US)
可能会适得其反。
回答by Lo - 3 chars required
This is the only solution that worked for me:
这是唯一对我有用的解决方案:
afterEvaluate {
if (project.hasProperty("preReleaseBuild")){
tasks.preReleaseBuild.dependsOn bundleJSRelease
} else {
tasks.preDebugBuild.dependsOn bundleJSDebug
}
}
回答by artkoenig
tasks.whenTaskAdded { task ->
if (task.name.contains("assembleRelease")) {
task.getDependsOn().add({
// add your logic here
})
}
}