How to Define Different Dependencies for Different Product Flavors

How to define different dependencies for different product flavors

To define a flavor specific dependency you can use proCompile instead of compile in your dependency section. When you run gradle properties you get an overview of automatic created configurations.

The correct build file looks like this:

buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.3'
}
}
apply plugin: 'com.android.application'

repositories {
mavenCentral()
}

android {
compileSdkVersion 22
buildToolsVersion "22.0.1"

defaultConfig {
minSdkVersion 10
targetSdkVersion 22
}

productFlavors {
pro {
packageName "de.janusz.journeyman.zinsrechner.pro"
}
free { }
}
}

dependencies {
compile 'com.android.support:support-v4:22.2.0'
freeCompile 'com.google.android.gms:play-services-ads:7.5.0'
}

specific flavor dependency

This section of the Android Studio manual indicates that you need to explicitly declare variant configurations before you use them, i.e. with this:

configurations {
freeDebugImplementation
paidDebugImplementation
freeReleaseImplementation
paidReleaseImplementation
}

I don't know whether that's still the case, but worth a shot.

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
}

Gradle: How to define common dependency for multiple flavors?

Yes, according to the documentation of android gradle dependency management, this is the only way of declaring flavor-specific dependencies.

If you are in a multi-modular project (and you don't want to repeat those lines in each submodule), you can also define these dependencies using the subproject block in the build.gradle of the root project:

subprojects {
//all subprojects` config
}
//or
configure(subprojects.findAll {it.name != 'sample'}) {
// subprojects that their name is not "sample"
}

Android flavors with different dependencies and same class file

Lite version is not compiling because java cannot find LockPatternActivity. For that thing you can use following solution.

You need to have a class in flavor pro

public final class LockPatternHelper {
private LockPatternHelper() {}

public static void showLockPattern() {
Intent intent = new Intent(LockPatternActivity.ACTION_VERIFY_CAPTCHA, null, this, LockPatternActivity.class);
startActivityForResult(intent, PATTERN_ID);
}
}

In flavor lite it should be same class with empty method showLockPattern().

public final class LockPatternHelper {
private LockPatternHelper() {}

public static void showLockPattern() {}
}

After this you can call LockPatternHelper.showLockPattern() from your customFragment.java

Every flavor will use it's helper and lite version will not contain unnecessary 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!



Related Topics



Leave a reply



Submit