Search code examples
androidgradledependency-management

Gradle: How to define common dependency for multiple flavors?


My app has 3 flavors (free, paid, special) and there's a dependency LIB_1 need only for free flavor and other dependency LIB_2 needed for both paid and special flavors.

So, my question is how to define these dependencies in build.gradle file?

Currently, i define them like this:

dependencies {
    freeImplementation 'LIB_1'
    paidImplementation 'LIB_2'
    specialImplementation 'LIB_2'
}

Is there a better way to define them instead of duplicating the same dependency for different flavors?


Solution

  • Yes, according to the documentation of android gradle dependency management, this is the only way of declaring flavor-specific dependencies.

    If you are in a multi-modular project (and you don't want to repeat those lines in each submodule), you can also define these dependencies using the subproject block in the build.gradle of the root project:

    subprojects {
        //all subprojects` config
    }
    //or
    configure(subprojects.findAll {it.name != 'sample'}) {
        // subprojects that their name is not "sample"
    }