Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle dependency based on both build type and flavor

Tags:

android

gradle

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") } 
like image 577
Eugen Pechanec Avatar asked Jan 25 '15 15:01

Eugen Pechanec


People also ask

What are buildTypes and product Flavours in Gradle?

Build variants are the result of Gradle using a specific set of rules to combine settings, code, and resources configured in your build types and product flavors. Although you do not configure build variants directly, you do configure the build types and product flavors that form them.

When would you use a product flavor in your build setup?

When to use Product Flavors. When we want to address the issue of having separate project code for each version of the app while still having one project code. Given a scenario where you have a free and a paid app you can limit features in the free and expose all the other features in the paid version of the app.

What is Buildtype in Gradle Android?

A build type determines how an app is packaged. By default, the Android plug-in for Gradle supports two different types of builds: debug and release . Both can be configured inside the buildTypes block inside of the module build file.

What is Flavordimensions?

A flavorDimension is something like a flavor category and every combination of a flavor from each dimension will produce a variant. In your case, you must define one flavorDimension named "type" and another dimension named "organization".


2 Answers

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') 

Read more here: https://developer.android.com/studio/build/dependencies.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") } 
like image 85
Eugen Pechanec Avatar answered Sep 20 '22 11:09

Eugen Pechanec


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:

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      } } 
like image 29
Mithun Avatar answered Sep 22 '22 11:09

Mithun