[英]Gradle dependency based on both build type and flavor
I have several build types: debug
, release
. 我有几种构建类型:
debug
, release
。 I also have two flavors pub
and dev
. 我也有两种口味的
pub
和dev
。
pub
flavored application depends on a pub
library, the similar goes for dev
flavor. pub
风味的应用程序取决于pub
库,类似于dev
风格。 Now I'd like the debug
build type app depend on debug
build of the library. 现在我想
debug
构建类型应用程序依赖于库的debug
版本。 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? 有没有办法根据flavor和build类型指定条件项目依赖?
EDIT: To avoid confusion here follow relevant build.gradle
files from the project that I'm currently using. 编辑:为避免混淆,请遵循我当前使用的项目中的相关
build.gradle
文件。
project/common/build.gradle (the library) 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 (one of the app modules using the library) 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")
}
Build plugin 3.xx uses variant-aware dependency resolution so devDebug
variant of an app module will automatically use devDebug
variant of its library module dependency. 构建插件的3.xx使用的变体感知依赖分辨率,以便
devDebug
应用程序模块的变种会自动使用devDebug
它的库模块依赖的变种。 To answer the question, this would be enough: 要回答这个问题,这就足够了:
implementation project(':common')
Read more here: https://developer.android.com/studio/build/dependencies.html#variant_aware 在这里阅读更多内容: https : //developer.android.com/studio/build/dependencies.html#variant_aware
I was able to find a solution here: https://github.com/JakeWharton/u2020/blob/master/build.gradle 我能在这里找到解决方案: 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 有关原始代码不足的原因,请访问: 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")
}
First define the various build types: 首先定义各种构建类型:
buildTypes {
pubRelease {
//config
}
devRelease {
//config
}
}
Create a task that will be executed only for a specific buildType and flavor: 创建仅针对特定buildType和flavor执行的任务:
task pubReleaseTask << {
//code
}
task devReleaseTask << {
//code
}
You can add the dependency dynamically: 您可以动态添加依赖项:
tasks.whenTaskAdded { task ->
if (task.name == 'pubRelease') {
task.dependsOn pubReleaseTask
}
if (task.name == 'devRelease') {
task.dependsOn devReleaseTask
}
}
Take a look at Multi-flavor variants You shouldn't use buildTypes for this. 看看多味道变体您不应该使用buildTypes。 But you can define multi-flavors:
但是你可以定义多种口味:
flavorDimensions "first", "second"
productFlavors {
pub {
flavorDimension "first"
}
dev {
flavorDimension "first"
}
deb {
flavorDimension "second"
}
rel {
flavorDimension "second"
}
}
And then you can add dependencies to your libs like this 然后你可以像这样添加你的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")
Follow up @dooplaye's example, assume you only want to compile leanback in one flavor, you could refer to below snippet: 跟进@ 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')
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.