5

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

For example:

  • free flavor -> I need to use the free library module
  • paid flavor -> I need to use the paid library module

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')
Ranjith Kumar
  • 13,385
  • 9
  • 95
  • 126

1 Answers1

12

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')
}
Gabriele Mariotti
  • 192,671
  • 57
  • 469
  • 489