androidgradleandroid-gradle-pluginandroid-flavors

Use different library module for each android flavor


I want to use a different library module for each flavor.

For example:

My flavors

productFlavors {
    free{
     ....... 
     }
    paid{
     ....... 
     }
   }

What I tried

freeImplementation  project(path:':freeLib', configuration: 'free')//for free

paidImplementation  project(path:':paidLib', configuration: 'paid')//for paid

But I got compile error not able to use this

Note: It's not a duplicate question. I already tried some StackOverflow questions. It's outdated (they are using compile)

Reference - Multi flavor app based on multi flavor library in Android Gradle

Solution (from Gabriele Mariotti comments)

 freeImplementation project(path:':freeLib')
 paidImplementation project(path:':paidLib')

Solution

  • If you have a library with multiple product flavors, in your lib/build.gradle you can define:

    android {
        ...
        //flavorDimensions is mandatory with flavors.
        flavorDimensions "xxx"
        productFlavors {
            free{
                dimension "xxx"
            }
            paid{
                dimension "xxx"
            }
        }
        ...
    }
    

    In your app/build.gradle define:

    android {
        ...
    
        flavorDimensions "xxx"
        productFlavors {
            free{
                dimension "xxx"
    
                // App and library's flavor have the same name.
                // MatchingFallbacks can be omitted
                matchingFallbacks = ["free"]
            }
            paid{
                dimension "xxx"
    
                matchingFallbacks = ["paid"]
            }
        }
        ...
    }
    dependencies {
        implementation project(':mylib')
    }
    

    Instead, if you have separate libraries you can simply use in your app/build.gradle something like:

    dependencies {
        freeImplementation project(':freeLib')
        paidImplementation project(':paidLib')
    }