Multi-flavor compile dependencies in gradle

Is there a way to have compile dependencies by multiple flavors in Android Studio (build.gradle)?

I have 2 flavorGroups, and in each 2 variants. Out of the 4 possible combinations I would like to be able to depend on a lib only if I’m both in latest and in free flavor. latestCompile or freeCompile works, but latestFreeCompile doesn’t. this is the relevant part of my build.gradle:

  • Android Use Done button on Keyboard to click button
  • The container 'Android Dependencies' references non existing library 'facebook-android-sdk\facebook\bin\com_facebook_android.jar'
  • Android USB debugging in VirtualBox
  • Porting iOS app to Android
  • app-release-unsigned.apk is not signed
  • How to automatically detect the need for iScroll?
  • android {
        defaultConfig {
            minSdkVersion 7
            targetSdkVersion 19
            versionCode 15
            versionName "1.9." + versionCode
        flavorGroups 'sdk', 'cost'
        productFlavors {
            latest {
                flavorGroup 'sdk'
                minSdkVersion 8
            sdk7 {
                flavorGroup 'sdk'
                minSdkVersion 7
                versionName android.defaultConfig.versionName + ".sdk7"
            free {
                flavorGroup 'cost'
            pro {
                flavorGroup 'cost'
    dependencies {
        // this works:
        freeCompile files('libs/StartAppInApp-2.2.1.jar')
        // and I would like something like this:
        latestFreeCompile '' // minSdkVersion:8

    If I would use:

    latestCompile ''

    then it would be included in latestPro as well (not needed)
    and if I’d use:

    freeCompile ''

    then it would be included in sdk7Free as well (although it needs SDK 8)

    Related posts:

    What's the difference between windowBackground and background for activities style?
    PopupMenu click causing RecyclerView to scroll
    Get Android Context in PCL project
    Whatsapp Message Layout - How to get time-view in the same row
    VS code for Android. Is it possible? hostname in certificate didn't match android
  • Implementing Ripple effect outside ImageButton
  • Android GCM push notification for localization application
  • Using listpreference and getting the key works but no ok button
  • Saving Android application data in Google Cloud
  • Memory leakage in event listener
  • Cordova / Phonegap: Live update codebase
  • 2 Solutions collect form web for “Multi-flavor compile dependencies in gradle”

    I had the same problem. I resolved that with some gradle code in my build.gradle:

    // global variables
    ext {
        buildType = ""
        groupCost = ""
        groupSdk = ""
    def splitCamelCase(String word) {
        def result = []
        int nextStart = 0;
        for (int i = 1; i < word.length(); i++) {
            if(word.charAt(i).isUpperCase()) {
                result.add(word.substring(nextStart, i));
                nextStart = i;
        return result;
    // start parameters
    println "Start parametes: tasks = " + gradle.startParameter.getTaskNames()
    gradle.startParameter.getTaskNames().each { task ->
        // This line is needed to skip other projects' tasks
        // You can safely remove it if you have only one project
        if(!task.startsWith(':<your_application_name>:')) return;
        def taskParts = splitCamelCase(task.split(":").last());
        def groupCostPrefix = taskParts[taskParts.size() - 3];
        def groupSdkPrefix = taskParts[taskParts.size() - 2];
        def buildTypePrefix = taskParts[taskParts.size() - 1];
        if("Debug".startsWith(buildTypePrefix)) {
            buildType = 'debug';
        else if("Release".startsWith(buildTypePrefix)) {
            buildType = 'release';
        else {
            return; // do not process tasks that are not ending with proper build type.
        if("Free".startsWith(groupCostPrefix)) {
            groupCost = 'free';
        else if("Pro".startsWith(groupCostPrefix)) {
            groupCost = 'pro';
        if("Sdk7".startsWith(groupSdkPrefix)) {
            groupSdk = 'froyo';
        else if("Latest".startsWith(groupSdkPrefix)) {
            groupSdk = 'latest';

    Then all you need is to add following code inside your ‘dependencies’ section:

    if(groupSdk == 'latest' && groupCost == 'free') {
        compile ''

    Same issue here, but Pawel’s solution didn’t work because gradle dependencies have other issues in which it starts building not only the selected flavors/build type, but all of them and it requires a more dynamic solution.

    Still I found this issue tracker:

    There is also a reference to fixing the rebuild-all bug I mentioned above, but I didn’t try it yet.

    And implemented this solution (as per issue tracker reply #60):

        Map<String, Dependency> customDeps = new HashMap<String, Dependency>()
        customDeps.put('flavor1GrpXflabor1GrpYDebugCompile', dependencies.project(path: ':lib', configuration: 'debug'))
        customDeps.put('flavor1GrpXflavor1GrpYReleaseCompile', dependencies.project(path: ':lib', configuration: 'release'))
        customDeps.put('flavor2GrpXflavor1GrpYDebugCompile', dependencies.project(path: ':other_lib', configuration: 'debug'))
        customDeps.put('flavor2GrpXflavor1GrpYReleaseCompile', dependencies.project(path: ':other_lib', configuration: 'release'))
        configurations.all() { config ->
            Dependency d = customDeps.get(
            if (d != null) {
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.