Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining dependencies for multiple variants

Tags:

android

gradle

Say we have four build types: debug, qa, beta, and release.

We can define dependencies for specific variants like so:

dependencies {
    // These dependencies are only included for debug and qa builds
    debugCompile 'com.example:lib:1.0.0'
    qaCompile 'com.example:lib:1.0.0'
}

Is there a way to compile these dependencies for multiple variants without repeating the artifact descriptor?

For example, I would like to do something like this:

dependencies {
   internalCompile 'com.example:lib:1.0.0'
}

Where internalCompile would specify that the library is included for both debug and qa builds.

I believe that the solution lies within defining a new Gradle configuration, but if I create an internalCompile configuration I am unsure how to ensure that those dependencies are only compiled for qa and debug builds.

like image 507
Bryan Herbst Avatar asked Jan 20 '16 18:01

Bryan Herbst


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.

What is flavorDimensions?

A flavorDimension is something like a flavor category and every combination of a flavor from each dimension will produce a variant. In your case, you must define one flavorDimension named "type" and another dimension named "organization".

What is missingDimensionStrategy?

missingDimensionStrategy(dimension: String, requestedValue: String) Specifies a flavor that the plugin should try to use from a given dimension in a dependency.

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.


1 Answers

extendsFrom

The names of the configurations which this configuration extends from. The artifacts of the super configurations are also available in this configuration.

configurations {
    // debugCompile and qaCompile are already created by the Android Plugin
    internalCompile
}

debugCompile.extendsFrom(internalCompile)
qaCompile.extendsFrom(internalCompile)

dependencies {
    //this adds lib to both debugCompile and qaCompile
    internalCompile 'com.example:lib:1.0.0'
}

Alternatively:

You can create a collection of artifact descriptors and use it with multiple configurations.

List internalCompile = ["com.example:lib:1.0.0",
               "commons-cli:commons-cli:1.0@jar",
               "org.apache.ant:ant:1.9.4@jar"]

List somethingElse = ['org.hibernate:hibernate:3.0.5@jar',
                  'somegroup:someorg:1.0@jar']

dependencies {
    debugCompile internalCompile
    qaCompile internalCompile, somethingElse
}
like image 183
RaGe Avatar answered Oct 29 '22 16:10

RaGe