Gradle: Add Dependency for a Specific Flavour of The Library

Gradle: add dependency for a specific flavour of the library

In your library you need to tell gradle to build every time every variant:

android {
publishNonDefault true
}

Then in your application, since recently I guess, you can do this:

dependencies {
(...)
devCompile project(path: ':lib', configuration: 'devDebug') // or 'devRelease'
storeCompile project(path: ':lib', configuration: 'storeRelease') // or 'storeDebug'
}

Found in the official documentation under Library Publication.

Edit:

Since version 0.14.3 (2014/11/18), you can now have Flavor-buildType-Compile directive as well:

In your build.gradle before the android {} scope add the following:

configurations {
devDebugCompile
devReleaseCompile
storeDebugCompile
storeReleaseCompile
}

Then you can declare and use different versions of your library per Flavor-BuildType:

dependencies {
(...)
devDebugCompile project(path: ':lib', configuration: 'devDebug')
devReleaseCompile project(path: ':lib', configuration: 'devRelease')
storeDebugCompile project(path: ':lib', configuration: 'storeDebug')
storeReleaseCompile project(path: ':lib', configuration: 'storeRelease')
}

Edit:

Dependency management between modules has changed since Android Gradle Plugin 3.0.0. It automatically tries to matches flavours between your app and the libraries/modules it depends on.

See the documentation for the whole explanation!

Use gradle module as a dependency with specific flavor

You should define missingDimensionStrategy for both app1 and app2.

For example:

// app1/build.gradle

defaultConfig {
missingDimensionStrategy 'library', 'flavor1'
}

// app2/build.gradle

defaultConfig {
missingDimensionStrategy 'library', 'flavor2'
}

Android Gradle 3.0.0 - add library dependency for specific flavors

I think you can achieve it by using something like this:

dependencies {
// use only mylib for debug and release
releaseImplementation project(path: ':mylib')
debugImplementation project(path: ':mylib')

// this will be used by all the flavor
implementation "com.android.support:appcompat-v7:27.0.2'
}

Import a specific flavour from a library module

For the future record, I found a solution to my problem by declaring the java.srcDirs and res.srcDirs according to the feature state.


I declare a gradle external property declaring if the feature is enabled or not.

project.ext {
hasLottie = true
}

More info on this blog post


And on my library gradle file:

apply plugin: 'com.android.library'

android {
compileSdkVersion 28

publishNonDefault true

defaultConfig {
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "1.0"
}

sourceSets {
main {
if (hasLottieSupport.toBoolean()) {
java.srcDirs += 'lottieYes/java'

} else {
java.srcDirs += 'lottieNo/java'
res.srcDirs += 'lottieNo/res'
}
}
}

buildTypes {
debug {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}

release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
implementation 'com.android.support:appcompat-v7:28.0.0'

if (hasLottieSupport.toBoolean()) {
implementation 'com.airbnb.android:lottie:2.5.6'
}
}

Link dependencies for specific flavors with dimensions

I was going by this section of the Android Studio manual which states that you need to declare explicitly any configurations that for flavor/build type combinations. In this case, that means you need to add the following to your build script before the dependencies {} block:

configurations {
freeLightweightImplementation
freeHeavyImplementation
premiumLightweightImplementation
premiumHeavyImplementation
}


Related Topics



Leave a reply



Submit