Home > Net >  Dividing app into flavors with different source and libraries
Dividing app into flavors with different source and libraries

Time:06-07

I'm dividing my app into multiple flavors, some of them should have HMS and some of them should not, so far I've managed to sucessfully divide source code but I can't get this part of gradle config to work as I expect it:

if(rootProject.ext.useHuaweiLib == true) {
        implementation 'com.huawei.agconnect:agconnect-core:1.4.1.300'
        implementation 'com.huawei.hms:push:5.0.1.300'
 } 

Here I'm adding libraries only if useHuaweiLib is true, here is flavor division in app module:

flavorDimensions "purpose"

productFlavors {
    client {
        dimension "purpose"
        buildConfigField "boolean", "USE_HUAWEI_LIB", "false"
    }
    defaultConfig {
        dimension "purpose"
        buildConfigField "boolean", "USE_HUAWEI_LIB", "false"
    }
    huawei_client{
        dimension "purpose"
        buildConfigField "boolean", "USE_HUAWEI_LIB", "true"
        rootProject.ext.useHuaweiLib = true
    }

    sdk {
        dimension "purpose"
        buildConfigField "boolean", "USE_HUAWEI_LIB", "false"
    }
}

and variable declaration in root module:

ext {
    buildToolsVersion = "32.0.0"
    compileSdkVersion = 31
    minSdkVersion = 23
    targetSdkVersion = 30
    versionCode = 1
    releaseName = "1.0"
    versionName = "1.0.0"
    useHuaweiLib = false
}

As you can see the variable is false by default and is only set to true in huawei_client flavor, but for some reason even if I choose 'client' flavor I still get access to HMS api. I want to be able to select a flavors with different set of sources and libs.

CodePudding user response:

You can add the flavor as a prefix to all dependency imports. Ditch the if statement and just use this:

huawei_clientImplementation 'com.huawei.agconnect:agconnect-core:1.4.1.300'
huawei_clientImplementation 'com.huawei.hms:push:5.0.1.300'

This way, the two librarys will only be added to the huawei_client flavor.

  • Related