How to match the build type of an Android Gradle project dependency?
Asked Answered
O

1

7

I have a project with 2 modules: library and app.

The library module is of course a library and only has release and debug build types.

The app module has 4 flavors as well as the release and debug build types, giving it a total of 8 build variants. It also declares a dependency on the library module like so:

compile project(path:':library', configuration:'release')

I'd like to be able to set the library config based on the app's build variant: release build variants should use the release version of the library and debug build variants should use the debug version of the library.

The obvious answer is to list each of the 8 variants and put the proper configuration and it'll work, but that's not the optimal answer: It's ugly and clutters the build script too much.

I've attempted a few approaches with project.configurations.all{} and applicationVariants.all{} but I couldn't find a definitive way to set the dependency configuration.

Is there a cleaner way to do this?

Orgulous answered 24/5, 2017 at 20:11 Comment(0)
H
3

In case anyone still would be running into this (or similar) issue nowadays, you would want to use matchingFallbacks to specify which resolved buildType or flavour to fallback to in case the library you depend on does not have matching configurations.

https://developer.android.com/studio/build/build-variants#resolve_matching_errors

By default though there should be a debug and release profile so to fix the OP question you would just need to remove the explicit configuration setting in the dependency declaration:

// forces release configuration on library
compile project(path:':library', configuration:'release')

// Allows gradle to match buildType from library to 
// what the current module's buildType is:
compile project(path:':library')

Snippets from the dev site in case it moves in the future (.kts):

// In the app's build.gradle file.
android {
    defaultConfig {
        // Do not configure matchingFallbacks in the defaultConfig block.
        // Instead, you must specify fallbacks for a given product flavor in the
        // productFlavors block, as shown below.
    }
    buildTypes {
        getByName("debug") {}
        getByName("release") {}
        create("staging") {
            // Specifies a sorted list of fallback build types that the
            // plugin should try to use when a dependency does not include a
            // "staging" build type. You may specify as many fallbacks as you
            // like, and the plugin selects the first build type that's
            // available in the dependency.
            matchingFallbacks += listOf("debug", "qa", "release")
        }
    }
    flavorDimensions += "tier"
    productFlavors {
        create("paid") {
            dimension = "tier"
            // Because the dependency already includes a "paid" flavor in its
            // "tier" dimension, you don't need to provide a list of fallbacks
            // for the "paid" flavor.
        }
        create("free") {
            dimension = "tier"
            // Specifies a sorted list of fallback flavors that the plugin
            // should try to use when a dependency's matching dimension does
            // not include a "free" flavor. You may specify as many
            // fallbacks as you like, and the plugin selects the first flavor
            // that's available in the dependency's "tier" dimension.
            matchingFallbacks += listOf("demo", "trial")
        }
    }
}
Haupt answered 14/11, 2022 at 15:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.