Android build variants: declare dependencies for all but one variant

674 views Asked by At

I have 5 build variants. 4 different build variants ("internal") that need a given set of Gradle dependencies and 1 build variant ("external") that does not. The code that refers to these dependencies is only in the internal build variants' /src folders.

Can I more concisely declare that "all but this variant" need a given list of dependencies?

This is how we are approaching this now our app-level build.gradle file:

internalDevelopmentImplementation(name:'MyGreatDependency-1.1.0', ext:'jar')
internalDevelopmentImplementation(name:"MyGreatDependency-logger-1.1.0", ext:'aar')
internalDevelopmentImplementation(name:"MyGreatDependency-android-2.0.0", ext:'aar')
internalDevelopmentImplementation(name:"MyGreatDependency-extension-android-1.1", ext:'aar')

internalReleaseImplementation(name:'MyGreatDependency-1.1.0', ext:'jar')
internalReleaseImplementation(name:"MyGreatDependency-logger-1.1.0", ext:'aar')
internalReleaseImplementation(name:"MyGreatDependency-android-2.0.0", ext:'aar')
internalReleaseImplementation(name:"MyGreatDependency-extension-android-1.1", ext:'aar')

internalStageImplementation(name:'MyGreatDependency-1.1.0', ext:'jar')
internalStageImplementation(name:"MyGreatDependency-logger-1.1.0", ext:'aar')
internalStageImplementation(name:"MyGreatDependency-android-2.0.0", ext:'aar')
internalStageImplementation(name:"MyGreatDependency-extension-android-1.1", ext:'aar')

internalProdImplementation(name:'MyGreatDependency-1.1.0', ext:'jar')
internalProdImplementation(name:"MyGreatDependency-logger-1.1.0", ext:'aar')
internalProdImplementation(name:"MyGreatDependency-android-2.0.0", ext:'aar')
internalProdImplementation(name:"MyGreatDependency-extension-android-1.1", ext:'aar')

Then, the externalProd variant simply doesn't declare the dependency.

Is there a way to improve this?

1

There are 1 answers

0
Eddie Lopez On BEST ANSWER

Here's an idea: Use configuration inheritance. Create a configuration with the dependencies that are to be shared by most configurations, set the common dependencies there. Make others extend from this one. Something like:

configurations {
    // Add the bulk of dependencies to this one:
    commonImplementation {}

internalDevelopmentImplementation.extendsFrom(commonImplementation)
    internalReleaseImplementation.extendsFrom(commonImplementation)
    // And so on

Disclaimer: I haven't tested this. Check https://docs.gradle.org/current/javadoc/org/gradle/api/artifacts/ConfigurationContainer.html