Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use different library module for each android flavor

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')
like image 826
Ranjithkumar Avatar asked May 30 '19 06:05

Ranjithkumar


Video Answer


2 Answers

  1. First add below gradle code snippet to your app/build.gradle

    flavorDimensions "env"
    productFlavors {
        dev {
            dimension "env"
        }
        pre {
            dimension "env"
        }
        prod {
            dimension "env"
        }
    }
    
  2. Second, add below gradle code snippet to your module/build.gradle

    flavorDimensions "env"
    productFlavors {
        register("dev")
        register("pre")
        register("prod")
    }
    
  3. Sync your project, and then you can find productFlavors were configured success,like below picture enter image description here

like image 85
caopeng Avatar answered Oct 20 '22 05:10

caopeng


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')
}
like image 44
Gabriele Mariotti Avatar answered Oct 20 '22 05:10

Gabriele Mariotti