Gradle dependency based on both build type and flavor

AndroidGradle

Android Problem Overview


I have several build types: debug, release. I also have two flavors pub and dev.

pub flavored application depends on a pub library, the similar goes for dev flavor. Now I'd like the debug build type app depend on debug build of the library. The following does not work:

pubReleaseCompile project(path: ':common', configuration: "pubRelease")
devReleaseCompile project(path: ':common', configuration: "devRelease")
pubDebugCompile project(path: ':common', configuration: "pubDebug")
devDebugCompile project(path: ':common', configuration: "devDebug")

Note: The library is set up to compile all variants.

Is there a way to specify conditional project dependency based on both flavor and build type?

EDIT: To avoid confusion here follow relevant build.gradle files from the project that I'm currently using.

project/common/build.gradle (the library)

apply plugin: 'com.android.library'
apply plugin: 'com.jakewharton.hugo' // annotation-based code generated logs only in debug build

android {
  defaultPublishConfig "pubRelease"
  publishNonDefault true // four variants of the library are built

  buildTypes {
    debug {}
    release {
      minifyEnabled false
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
    }
  }
  productFlavors {
    pub {
      // custom build config fields
    }
    dev {
      // custom build config fields
    }
  }
}

dependencies {
  // ...
}

project/parent/build.gradle (one of the app modules using the library)

apply plugin: 'com.android.application'
apply plugin: 'com.jakewharton.hugo'

android {
  // ...

  signingConfigs {
    release {
      // ...
    }
  }

  buildTypes {
    release {
      signingConfig signingConfigs.release
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt'
      shrinkResources true
      minifyEnabled true
    }
    debug {
      versionNameSuffix '-debug'
    }
  }
  productFlavors {
    pub {
      // custom res values
    }
    dev {
      // custom res values
    }
  }
}

dependencies {
  // ...
  pubCompile project(path: ':common', configuration: "pubRelease")
  devCompile project(path: ':common', configuration: "devRelease")
}

Android Solutions


Solution 1 - Android

Android Plugin for Gradle 3.x.x

Build plugin 3.x.x uses variant-aware dependency resolution so devDebug variant of an app module will automatically use devDebug variant of its library module dependency. To answer the question, this would be enough:

implementation project(':common')

Read more here: https://developer.android.com/studio/build/dependencies.html#variant_aware

Original answer

I was able to find a solution here: https://github.com/JakeWharton/u2020/blob/master/build.gradle

More on why my original code does not suffice is available here: https://code.google.com/p/android/issues/detail?id=162285

Working solution:

configurations {
  pubDebugCompile
  devDebugCompile
  pubReleaseCompile
  devReleaseCompile
}

dependencies {
  pubReleaseCompile project(path: ':common', configuration: "pubRelease")
  devReleaseCompile project(path: ':common', configuration: "devRelease")
  pubDebugCompile project(path: ':common', configuration: "pubDebug")
  devDebugCompile project(path: ':common', configuration: "devDebug")
}

Solution 2 - Android

First define the various build types:

buildTypes {
    pubRelease {
        //config
    }
    devRelease {
        //config
    }
}

Create a task that will be executed only for a specific buildType and flavor:

task pubReleaseTask << {
    //code
}

task devReleaseTask << {
    //code
}

You can add the dependency dynamically:

tasks.whenTaskAdded { task ->
    if (task.name == 'pubRelease') {
        task.dependsOn pubReleaseTask
    }
    if (task.name == 'devRelease') {
        task.dependsOn devReleaseTask 
    }
}

Solution 3 - Android

Follow up @dooplaye's example, assume you only want to compile leanback in one flavor, you could refer to below snippet:

applicationVariants.all { variant ->
	def flavorString = ""
	def flavors = variant.productFlavors
	for (int i = 0; i < flavors.size(); i++) {
		flavorString += flavors[i].name;
	}

	if (flavorString.equalsIgnoreCase("pubdeb")) {
		dependencies {
			compile('com.android.support:leanback-v17:22.2.1')
		}
	}
}

Solution 4 - Android

Take a look at Multi-flavor variants You shouldn't use buildTypes for this. But you can define multi-flavors:

flavorDimensions "first", "second"

productFlavors {
    pub {
        flavorDimension "first"
    }
    dev {
        flavorDimension "first"
    }
    deb {
        flavorDimension "second"
    }
    rel {
        flavorDimension "second"
    }
}

And then you can add dependencies to your libs like this

pubRelCompile project(path: ':common', configuration: "pubRel")
devRelCompile project(path: ':common', configuration: "devRel")
pubDebCompile project(path: ':common', configuration: "pubDeb")
devDebCompile project(path: ':common', configuration: "devDeb")

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEugen PechanecView Question on Stackoverflow
Solution 1 - AndroidEugen PechanecView Answer on Stackoverflow
Solution 2 - AndroidMithunView Answer on Stackoverflow
Solution 3 - AndroidHsiao-TingView Answer on Stackoverflow
Solution 4 - AndroiddooplayeView Answer on Stackoverflow