我有几种构建类型:debug
,release
。
我还有两种pub
和dev
。
pub
风格的应用程序依赖于pub
库,类似于dev
风格。
现在我希望debug
构建类型应用程序依赖于库的debug
构建。以下不起作用:
pubReleaseCompile project(path: ':common', configuration: "pubRelease")
devReleaseCompile project(path: ':common', configuration: "devRelease")
pubDebugCompile project(path: ':common', configuration: "pubDebug")
devDebugCompile project(path: ':common', configuration: "devDebug")
注意:库已设置为编译所有变体。
有没有办法根据flavor和build类型指定条件项目依赖?
编辑:为避免混淆,请遵循我目前正在使用的项目中的相关build.gradle
文件。
project / common / build.gradle (图书馆)
apply plugin: 'com.android.library'
apply plugin: 'com.jakewharton.hugo' // annotation-based code generated logs only in debug build
android {
defaultPublishConfig "pubRelease"
publishNonDefault true // four variants of the library are built
buildTypes {
debug {}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
productFlavors {
pub {
// custom build config fields
}
dev {
// custom build config fields
}
}
}
dependencies {
// ...
}
project / parent / build.gradle (使用该库的其中一个应用模块)
apply plugin: 'com.android.application'
apply plugin: 'com.jakewharton.hugo'
android {
// ...
signingConfigs {
release {
// ...
}
}
buildTypes {
release {
signingConfig signingConfigs.release
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt'
shrinkResources true
minifyEnabled true
}
debug {
versionNameSuffix '-debug'
}
}
productFlavors {
pub {
// custom res values
}
dev {
// custom res values
}
}
}
dependencies {
// ...
pubCompile project(path: ':common', configuration: "pubRelease")
devCompile project(path: ':common', configuration: "devRelease")
}
答案 0 :(得分:45)
构建插件3.0.0使用变体感知依赖项解析,因此app模块的devDebug
变体将自动使用其库模块依赖项的devDebug
变体。要回答这个问题,这就足够了:
implementation project(':common')
在此处阅读更多内容:https://developer.android.com/studio/build/gradle-plugin-3-0-0-migration.html#variant_aware
我能够在这里找到解决方案:https://github.com/JakeWharton/u2020/blob/master/build.gradle
此处提供了有关原始代码不足的原因:https://code.google.com/p/android/issues/detail?id=162285
工作解决方案:
configurations {
pubDebugCompile
devDebugCompile
pubReleaseCompile
devReleaseCompile
}
dependencies {
pubReleaseCompile project(path: ':common', configuration: "pubRelease")
devReleaseCompile project(path: ':common', configuration: "devRelease")
pubDebugCompile project(path: ':common', configuration: "pubDebug")
devDebugCompile project(path: ':common', configuration: "devDebug")
}
答案 1 :(得分:5)
首先定义各种构建类型:
buildTypes {
pubRelease {
//config
}
devRelease {
//config
}
}
创建一个仅针对特定buildType和flavor执行的任务:
task pubReleaseTask << {
//code
}
task devReleaseTask << {
//code
}
您可以动态添加依赖项:
tasks.whenTaskAdded { task ->
if (task.name == 'pubRelease') {
task.dependsOn pubReleaseTask
}
if (task.name == 'devRelease') {
task.dependsOn devReleaseTask
}
}
答案 2 :(得分:2)
看看Multi-flavor variants 你不应该使用buildTypes。 但是你可以定义多种口味:
flavorDimensions "first", "second"
productFlavors {
pub {
flavorDimension "first"
}
dev {
flavorDimension "first"
}
deb {
flavorDimension "second"
}
rel {
flavorDimension "second"
}
}
然后你可以像这样添加你的libs依赖
pubRelCompile project(path: ':common', configuration: "pubRel")
devRelCompile project(path: ':common', configuration: "devRel")
pubDebCompile project(path: ':common', configuration: "pubDeb")
devDebCompile project(path: ':common', configuration: "devDeb")
答案 3 :(得分:1)
跟进@ dooplaye的例子,假设你只想编译一种风格的leanback,你可以参考下面的代码:
applicationVariants.all { variant ->
def flavorString = ""
def flavors = variant.productFlavors
for (int i = 0; i < flavors.size(); i++) {
flavorString += flavors[i].name;
}
if (flavorString.equalsIgnoreCase("pubdeb")) {
dependencies {
compile('com.android.support:leanback-v17:22.2.1')
}
}
}