Gradle dependency based on both build types and taste

I have several types of assembly: debug , release . I also have two flavors pub and dev .

pub flavored application depends on the pub library, which is similar for dev flavor. Now, I would like a debug type application to depend on the build of the debug 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 configured to compile all options.

Is there a way to specify a conditional project dependency based on both taste and build type?

EDIT: To avoid confusion, follow the appropriate build.gradle files from the project I am currently using.

project / common / build.gradle (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 application 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") } 
+44
android gradle
Jan 25 '15 at 15:03
source share
4 answers

Android Plugin for Gradle 3.xx

The build plugin 3.xx uses the dependency resolution of the devDebug variant, therefore the devDebug variant of the application module will automatically use the devDebug its library module. To answer the question, this will be enough:

 implementation project(':common') 

Read more here: https://developer.android.com/studio/build/dependencies.html#variant_aware

Original answer

I managed to find a solution here: https://github.com/JakeWharton/u2020/blob/master/build.gradle

More about why my source code is not enough 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") } 
+45
Apr 15 '15 at 22:09
source share

First define the various types of assembly:

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

Create a task that will be performed only for a specific type of buildType and taste:

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

Dynamic dynamic relationship:

 tasks.whenTaskAdded { task -> if (task.name == 'pubRelease') { task.dependsOn pubReleaseTask } if (task.name == 'devRelease') { task.dependsOn devReleaseTask } } 
+5
Mar 05 '15 at 6:10
source share

Take a look at options with several flavors. You should not use buildTypes for this. 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 libraries, such as

 pubRelCompile project(path: ':common', configuration: "pubRel") devRelCompile project(path: ':common', configuration: "devRel") pubDebCompile project(path: ':common', configuration: "pubDeb") devDebCompile project(path: ':common', configuration: "devDeb") 
+2
Mar 11 '15 at 10:00
source share

The following @dooplaye example, suppose you want to compile rollback in only one option, you can refer to the snippet below:

 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') } } } 
+1
Aug 15 '15 at 6:51
source share



All Articles