Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Add library module in particular flavor only

In my android application, I have 2 library modules that need to be included conditionally based on flavor. Following is the application structure
:app
:library1
:library2
And app has 2 flavors, so it will generate 2 apk's, one free version and another paid. So accordingly configured app build.gradle as follows:

android {
    flavorDimensions("billing_type")
    productFlavors {
        free {
            dimension "billing_type"
        }

        paid {
            dimension "billing_type"
        }
    }
}
dependencies {
    implementation project(path: 'flavor1')
    implementation project(path: 'flavor2')
}

As we can see, both library modules will be included in both flavors(free and paid). But i want to include library2 only in paid flavor. So how can i conditionally add library2 only in paid flavor? I followed some of the approaches mentioned in link

and made following changes:

android {
    flavorDimensions("billing_type")
    productFlavors {
        free {
            dimension "billing_type"
        }

        paid {
            dimension "billing_type"
        }
    }
}
configurations {
    freeImplementation
    paidImplementation
}
dependencies {
    freeImplementation implementation project(path: 'flavor1')
    paidImplementation implementation project(path: 'flavor2')
}

All the references online are for older grdale versions which uses compile to add library modules, But it has been deprecated since gradle plugin 3.0.0. So can anyone help me figure out how to conditionally add library module in particular flavor only in latest

like image 265
Mahantesh M Ambi Avatar asked Nov 14 '25 20:11

Mahantesh M Ambi


1 Answers

For Gradle versions 3.0 and higher

When your main project uses modules or library modules(AAR) that have flavor dimensions, your app doesn't know which one to use. You should Use missingDimensionStrategy in the defaultConfig block of your app's build.gradle file to specify the default flavor. For example :

missingDimensionStrategy 'dimension', 'flavor1', 'flavor2'

Please check this link for more details.

like image 79
Alexandar Avatar answered Nov 17 '25 18:11

Alexandar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!