基于构建类型和风格的 Gradle 依赖 [英] Gradle dependency based on both build type and flavor
问题描述
我有几种构建类型:debug
、release
.我也有两种口味 pub
和 dev
.
I have several build types: debug
, release
.
I also have two flavors pub
and dev
.
pub
风格的应用程序依赖于 pub
库,dev
风格也类似.现在我希望 debug
构建类型应用程序依赖于库的 debug
构建.以下方法不起作用:
pub
flavored application depends on a pub
library, the similar goes for dev
flavor.
Now I'd like the debug
build type app depend on debug
build of the library. The following does not work:
pubReleaseCompile project(path: ':common', configuration: "pubRelease")
devReleaseCompile project(path: ':common', configuration: "devRelease")
pubDebugCompile project(path: ':common', configuration: "pubDebug")
devDebugCompile project(path: ':common', configuration: "devDebug")
注意:该库设置为编译所有变体.
Note: The library is set up to compile all variants.
有没有办法根据风格和构建类型指定有条件的项目依赖?
Is there a way to specify conditional project dependency based on both flavor and build type?
为了避免混淆,请遵循我当前使用的项目中的相关 build.gradle
文件.
To avoid confusion here follow relevant build.gradle
files from the project that I'm currently using.
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(使用库的应用模块之一)
project/parent/build.gradle (one of the app modules using the library)
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")
}
推荐答案
Android Plugin for Gradle 3.x.x
Build plugin 3.x.x 使用变体感知依赖项解析,因此应用模块的 devDebug
变体将自动使用其库模块依赖项的 devDebug
变体.要回答这个问题,这就足够了:
Android Plugin for Gradle 3.x.x
Build plugin 3.x.x uses variant-aware dependency resolution so devDebug
variant of an app module will automatically use devDebug
variant of its library module dependency. To answer the question, this would be enough:
implementation project(':common')
在此处阅读更多信息:https://developer.android.com/studio/build/dependencies.html#variant_aware
我在这里找到了解决方案:https://github.com/JakeWharton/u2020/blob/master/build.gradle
I was able to find a solution here: https://github.com/JakeWharton/u2020/blob/master/build.gradle
有关为什么我的原始代码不够用的更多信息,请访问:https://code.google.com/p/android/issues/detail?id=162285
More on why my original code does not suffice is available here: 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")
}
这篇关于基于构建类型和风格的 Gradle 依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!