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')
First add below gradle code snippet to your app/build.gradle
flavorDimensions "env"
productFlavors {
dev {
dimension "env"
}
pre {
dimension "env"
}
prod {
dimension "env"
}
}
Second, add below gradle code snippet to your module/build.gradle
flavorDimensions "env"
productFlavors {
register("dev")
register("pre")
register("prod")
}
Sync your project, and then you can find productFlavors were configured success,like below picture
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')
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With