Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add dependency to specific productFlavor and buildType in gradle

I'm wondering how to add dependency to specific productFlavor and buildType in gradle. For example I have productFlavor free and build type release, how can I add a dependency on the assembleFreeRelease task?

I've tried many variants but neither works.

For example I tried:

task('release', dependsOn: assembleProductionRelease) {
} 
// error: Could not find property 'assembleProductionRelease' on root project 'app'.

Or:

task('release', dependsOn: 'assembleProductionRelease') {
}

Here there is no error but the task is executed for every flavor and build type, very confusing.

like image 406
lukstei Avatar asked Oct 25 '13 12:10

lukstei


People also ask

How would you 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.

How do I add dependency to my build Gradle file?

To add a dependency to your project, specify a dependency configuration such as implementation in the dependencies block of your module's build.gradle file.

What is a Buildtype in Gradle?

A build type determines how an app is packaged. By default, the Android plug-in for Gradle supports two different types of builds: debug and release . Both can be configured inside the buildTypes block inside of the module build file.

How do I sync Gradle dependencies?

Simply open the gradle tab (can be located on the right) and right-click on the parent in the list (should be called "Android"), then select "Refresh dependencies". This should resolve your issue.


2 Answers

There is built-in support for flavor and buildType dependencies.

dependencies {
   flavor1Compile "..."
   freeReleaseCompile "..."
}

http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Sourcesets-and-Dependencies

like image 51
Sergii Pechenizkyi Avatar answered Sep 30 '22 17:09

Sergii Pechenizkyi


These task are generated dynamically based on your Android plugin configuration. At the time of configuration they are not available to you yet. You can defer the creation of your task in two ways:

Wait until the project is evaluated.

afterEvaluate {
    task yourTask(dependsOn: assembleFreeRelease) {
        println "Your task"
    }
}

Lazily declaring the task dependency as String.

task yourTask(dependsOn: 'assembleFreeRelease') {
    println "Your task"
}
like image 27
Benjamin Muschko Avatar answered Sep 30 '22 19:09

Benjamin Muschko