Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define different dependencies for different product flavors

People also ask

What are buildTypes and product Flavours in Gradle?

Once the new project is created, by default it consists of two build types/variants - debug, release. Debug is the build type that is used when we run the application from the IDE directly onto a device. A release is the build type that requires you to sign the APK.

What are build variants?

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.

How do you use productFlavors?

Creating Product Flavor is pretty much easy in Android Studio, we simply need to add productFlavors block inside the Android block in the application-level build. gradle file. It's that simple to create 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'
}

Fast forward to mid-2018. You will need to add flavorDimensions.

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

    flavorDimensions "dimensionName"
    productFlavors {
        pro {
            dimension "dimensionName"
        }
        free {
            dimension "dimensionName"
        }
    }
}

dependencies {
    implementation 'com.android.support:support-v4:22.2.0'
    freeImplementation 'com.google.android.gms:play-services-ads:15.0.1'
}

Also, take note:

Configuration 'compile' is obsolete and has been replaced with 'implementation' and 'api'. It will be removed at the end of 2018. For more information see: http://d.android.com/r/tools/update-dependency-configurations.html


You need to manually add configuration for each flavor. Example

configurations {
    proCompile
    freeCompile
}

dependencies {
    compile 'com.parse.bolts:bolts-tasks:1.3.0'

    proCompile 'com.android.support:design:23.1.1'
    freeCompile 'com.parse:parse-android:1.12.0'
}

Edit: I recommend using one of the other techniques!

An alternative to the accepted answer is this:

    ext {
        flavorType = ""
    }


    gradle.startParameter.getTaskNames().each { task ->

        if(task.contains("flavor1")){
            flavorType = "flavor1"
        } else if (task.contains("flavor2")){
            flavorType = "flavor2"
        } else {
            flavorType = "flavor3"
        }

    }

 if(flavorType == 'flavor1' || flavorType == 'flavor2') {
        compile 'com.android.support:support-v4:18.0.+'
    }

Simple:

dependencies {

    ....
    ....

    gradle.startParameter.getTaskNames().each { task ->
        if(task.contains("free")) {
             implementation 'com.google.android.gms:play-services-ads:17.2.0'
        }
    }

    ....
    ....
}

or just:

FreeImplementation 'com.google.android.gms:play-services-ads:17.2.0'