Art*_*ent 29 android android-gradle-plugin
我想基于当前的buildType在Android Gradle项目中动态添加依赖项.我知道我可以在依赖项中指定buildType:
compile project(path: ':lib1', configuration: 'debug')
Run Code Online (Sandbox Code Playgroud)
但是,如何使用当前的buildType来指定要导入的库的哪个变体,以便调试版本或发布版本自动导入库的调试版本或发行版本?我想要的是这样的(其中currentBuildType是一个包含当前使用的buildType名称的变量):
compile project(path: ':lib1', configuration: currentBuildType)
Run Code Online (Sandbox Code Playgroud)
我要导入的库项目已设置publishNonDefault true
,因此将发布所有buildType.
Ale*_*nov 12
添加一个任务,该任务取决于调用后的每个assembleXxx任务和属性设置
ext {
currentConfig = ""
}
task generateConfigProperty(dependsOn: 'installDebug') {
android.applicationVariants.all { variant ->
variant.outputs.each { output ->
def taskName = "taskindicate$output.name"
task "$taskName"() << {
project.ext.set("currentConfig", "$output.name")
}
output.assemble.dependsOn "taskindicate$output.name"
}
}
}
task getConfig(dependsOn: ['installDebug', 'generateConfigProperty']) << {
println("My config is $currentConfig")
}
Run Code Online (Sandbox Code Playgroud)
从答案中得出了想法
Sho*_*oky 12
您可以使用
if(gradle.startParameter.taskNames.contains("assembleExample")) {
// do stuff
}
在buildConfig
评估块之前将设置该变量
在Gradle的配置阶段,我找不到一种干净的方法来获取当前的构建类型.相反,我分别为每个构建类型定义依赖关系:
debugCompile project(path: ':lib1', configuration: 'debug')
releaseCompile project(path: ':lib1', configuration: 'release')
Run Code Online (Sandbox Code Playgroud)
如果你有许多构建类型和许多项目依赖项,这可能会非常冗长,但是可以添加一个函数来使依赖项成为一个单行程序.您需要将其添加到主Gradle构建文件中:
subprojects {
android {
dependencies.metaClass.allCompile { dependency ->
buildTypes.each { buildType ->
"${buildType.name}Compile" project(path: ":${dependency.name}", configuration: buildType.name)
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以在Gradle模块中添加项目依赖项,如下所示:
allCompile project(':lib1')
Run Code Online (Sandbox Code Playgroud)
如果您还使用构建风格,则必须调整解决方案.有关该功能的文档,请参阅此链接:http: //tools.android.com/tech-docs/new-build-system/user-guide#TOC-Library-Publication
请注意,Android小组正在针对此行为进行改进:https: //code.google.com/p/android/issues/detail?id = 52962
这个很简单:
android {
applicationVariants.all { variant ->
variant.buildType.name // this is the value!
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:显然在某些时候使用 gradle 更新,这不起作用,正如我在下面的评论中提到的那样。所以我建议检查其他选项。
归档时间: |
|
查看次数: |
31475 次 |
最近记录: |