Android Gradle Dependencies by Flavors

In a previous post about Gradle Flavors and Build Types, we mentioned that we can set different dependencies inside dependencies{} block by flavors, by build type or both combined. Below we will see an example for each situation.

By flavors

  • <flavorName>Compile
    apply plugin: 'com.android.application'
    
    android {
        compileSdkVersion 23
        buildToolsVersion "23.0.3"
        defaultConfig {
            applicationId "com.example.diana.buildvariants"
            minSdkVersion 15
            targetSdkVersion 23
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        }
    
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
    
            qa {
                applicationIdSuffix ".qa"
    
                // signingConfigs as debug key needed. Otherwise, the app won't run and it will ask
                // you to sign the apk
                signingConfig signingConfigs.debug
            }
        }
    
        productFlavors {
            free {
                applicationId "com.example.myapp.free"
                versionName "1.0-free"
                minSdkVersion 23
            }
    
            paid {
                applicationId "com.example.myapp.paid"
                versionName "1.0-paid"
            }
        }
    }
    
    dependencies {
        paidCompile 'com.android.support:design:23.4.0'
        compile fileTree(dir: 'libs', include: ['*.jar'])
        compile 'com.android.support:appcompat-v7:23.4.0'
        compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha3'
        testCompile 'junit:junit:4.12'
        androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
        androidTestCompile 'com.android.support.test:runner:0.5'
        androidTestCompile 'com.android.support:support-annotations:23.4.0'
    }
    

By build type

  • <buildTypeName>Compile>
    apply plugin: 'com.android.application'
    
    android {
        compileSdkVersion 23
        buildToolsVersion "23.0.3"
        defaultConfig {
            applicationId "com.example.diana.buildvariants"
            minSdkVersion 15
            targetSdkVersion 23
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        }
    
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
    
            qa {
                applicationIdSuffix ".qa"
    
                // signingConfigs as debug key needed. Otherwise, the app won't run and it will ask
                // you to sign the apk
                signingConfig signingConfigs.debug
            }
        }
    
        productFlavors {
            free {
                applicationId "com.example.myapp.free"
                versionName "1.0-free"
                minSdkVersion 23
            }
    
            paid {
                applicationId "com.example.myapp.paid"
                versionName "1.0-paid"
            }
        }
    }
    
    dependencies {
        qaCompile 'com.android.support:design:23.4.0'
        compile fileTree(dir: 'libs', include: ['*.jar'])
        compile 'com.android.support:appcompat-v7:23.4.0'
        compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha3'
        testCompile 'junit:junit:4.12'
        androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
        androidTestCompile 'com.android.support.test:runner:0.5'
        androidTestCompile 'com.android.support:support-annotations:23.4.0'
    }
    

By both flavor and build type

  • <flavorName><BuildTypeName>Compile.
  • In this case you have to define the custom compile in configurations{} block in order to be recognized in dependencies{} block.
    configurations {
      paidQaCompile
    }

    Final result:

    apply plugin: 'com.android.application'
    
    android {
        compileSdkVersion 23
        buildToolsVersion "23.0.3"
        defaultConfig {
            applicationId "com.example.diana.buildvariants"
            minSdkVersion 15
            targetSdkVersion 23
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        }
    
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
    
            qa {
                applicationIdSuffix ".qa"
    
                // signingConfigs as debug key needed. Otherwise, the app won't run and it will ask
                // you to sign the apk
                signingConfig signingConfigs.debug
            }
        }
    
        productFlavors {
            free {
                applicationId "com.example.myapp.free"
                versionName "1.0-free"
                minSdkVersion 23
            }
    
            paid {
                applicationId "com.example.myapp.paid"
                versionName "1.0-paid"
            }
        }
    }
    configurations {
        paidQaCompile
    }
    
    dependencies {
        paidQaCompile 'com.android.support:design:23.4.0'
        compile fileTree(dir: 'libs', include: ['*.jar'])
        compile 'com.android.support:appcompat-v7:23.4.0'
        compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha3'
        testCompile 'junit:junit:4.12'
        androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
        androidTestCompile 'com.android.support.test:runner:0.5'
        androidTestCompile 'com.android.support:support-annotations:23.4.0'
    }
    

 

Menu