基于构buildtypes和风格的Gradle依赖

我有几种构buildtypes: debugrelease 。 我也有两种口味的pubdev

pub风味的应用取决于一个pub库,类似去dev味道。 现在我想debug构buildtypes的应用程序依赖库的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") 

注意:库设置为编译所有变体。

有没有一种方法来指定基于味道和构buildtypes的条件项目依赖项?

编辑:为了避免在这里混淆从我目前正在使用的项目相关的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") } 

Android插件的Gradle 3.0.0

构build插件3.0.0使用变体感知的依赖关系parsing,所以应用程序模块的devDebug变体将自动使用其库模块依赖关系的devDebug变体。 要回答这个问题,这就足够了:

 implementation project(':common') 

阅读更多: https : //developer.android.com/studio/build/gradle-plugin-3-0-0-migration.html#variant_aware

原始答案

我可以在这里find一个解决scheme: https : //github.com/JakeWharton/u2020/blob/master/build.gradle

更多关于为什么我的原始代码不够用可在这里: https : //code.google.com/p/android/issues/detail?id=162285

工作scheme:

 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") } 

首先定义各种构buildtypes:

 buildTypes { pubRelease { //config } devRelease { //config } } 

创build一个只为特定的buildType和flavor执行的任务:

 task pubReleaseTask << { //code } task devReleaseTask << { //code } 

您可以dynamic添加依赖项:

 tasks.whenTaskAdded { task -> if (task.name == 'pubRelease') { task.dependsOn pubReleaseTask } if (task.name == 'devRelease') { task.dependsOn devReleaseTask } } 

看看多味变体你不应该使用buildTypes这个。 但是你可以定义多种风味:

 flavorDimensions "first", "second" productFlavors { pub { flavorDimension "first" } dev { flavorDimension "first" } deb { flavorDimension "second" } rel { flavorDimension "second" } } 

然后你可以添加依赖到你的库像这样

 pubRelCompile project(path: ':common', configuration: "pubRel") devRelCompile project(path: ':common', configuration: "devRel") pubDebCompile project(path: ':common', configuration: "pubDeb") devDebCompile project(path: ':common', configuration: "devDeb") 

跟进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') } } }