基于构建类型和风格的Gradle依赖 [英] Gradle dependency based on both build type and flavor
问题描述
我有几种构建类型: debug
, release
。
我也有两种口味 pub
和 dev
。
pub
风格化应用程序依赖于 pub
库,类似于 dev
味道。
现在我希望 debug
构建类型应用程序依赖库的 debug
构建版本。以下不起作用:
pubReleaseCompile项目(路径:':common',配置:pubRelease)
devDeleaseCompile项目(路径:':common',配置:'devRelease')
pubDebugCompile项目(路径:':common',配置:pubDebug)
devDebugCompile项目(路径:':common',配置:devDebug)
注意:库设置为编译所有变体。
有没有一种方法可以基于flavor和build类型来指定条件项目依赖项?
编辑:为避免混淆,请按照我目前使用的项目中的相关 build.gradle
文件。
project / common / build.gradle (库)
apply plugin:'com.android .library'
apply plugin:'com.jakewharton.hugo'//基于注解的代码仅在调试版本中生成日志
android {
defaultPublishConfigpubRelease
publishNonDefault true //构建库的四种变体
uildTypes {
debug {}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.txt'
}
}
productFlavors {
pub {
// custom建立配置字段
}
dev {
//定制生成配置字段
}
}
}
依赖关系{
// ...
}
项目/父项/构建.gradle (使用库的应用程序模块之一)
apply plugin:'com.android.application'
apply plugin:'com.jakewharton.hugo'
android {
// ...
signingConfigs {
release {
// ...
}
}
buildTypes {
版本{
signingConfig signingConfigs.release
proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-project.txt'
shrinkResources true
minifyEnabled true
}
debug {
versionNameSuffix '-debug'
}
}
productFlavors {
pub {
//自定义分值
}
dev {
//定制资源值
}
}
}
依赖项{
// ...
pubCompile项目(路径:':通用',配置:pubRelease)
devCompile项目(路径:':common',配置:devRelease)
}
Gradle 3.0.0的Android插件
Build plugin 3.0.0使用变体感知的依赖关系解决方案,所以应用模块的变体会自动使用它的库模块依赖关系的变体 devDebug
。要回答这个问题,这就足够了:
实施项目(':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
工作解决方案: p>
配置{
pubDebugCompile
devDebugCompile
pubReleaseCompil e
devReleaseCompile
依赖关系{
pubReleaseCompile项目(路径:':common',配置:'pubRelease')
devReleaseCompile项目(路径: ':common',配置:'devRelease')
pubDebugCompile项目(路径:':common',配置:pubDebug)
devDebugCompile项目(路径:':common',配置:'devDebug' )
}
I have several build types: debug
, release
.
I also have two flavors pub
and dev
.
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?
EDIT: To avoid confusion here follow relevant build.gradle
files from the project that I'm currently using.
project/common/build.gradle (the library)
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 (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.0.0
Build plugin 3.0.0 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')
Read more here: https://developer.android.com/studio/build/gradle-plugin-3-0-0-migration.html#variant_aware
Original answer
I was able to find a solution here: https://github.com/JakeWharton/u2020/blob/master/build.gradle
More on why my original code does not suffice is available here: https://code.google.com/p/android/issues/detail?id=162285
Working solution:
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屋!