2

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

Mahantesh M Ambi
  • 766
  • 1
  • 10
  • 25

2 Answers2

0

Option 1:

Finally I found out how to do this, I will explain it here for others facing same problem:

The key part is to set publishNonDefault to true in library build.gradle, Then you must define dependencies as suggested by user guide.

The whole project would be like this:

Library build.gradle:

apply plugin: 'com.android.library'

android {        
    ....
    publishNonDefault true
    productFlavors {
        market1 {}
        market2 {}
    }
}

project build.gradle:

apply plugin: 'com.android.application'

android {
    ....
    productFlavors {
        market1 {}
        market2 {}
    }
}

dependencies {
    ....
    market1Compile project(path: ':lib', configuration: 'market1Release')
    market2Compile project(path: ':lib', configuration: 'market2Release')
}

Now you can select the app flavor and Build Variants panel and the library will be selected accordingly and all build and run will be done based on the selected flavor.

If you have multiple app module based on the library Android Studio will complain about Variant selection conflict, It's ok, just ignore it.

enter image description here

Option 2:

Example solution:

Lib build.gradle

android {

    publishNonDefault true

    buildTypes {
        release {
        }
        debug {
        }
    }
    productFlavors {
        free {
        }
        paid {
        }
    }
}

App build.gradle

android {

    buildTypes {
        debug {
        }
        release {
        }
    }
    productFlavors {
        free {
        }
        paid {
        }
    }
}

configurations {
    freeDebugCompile
    paidDebugCompile
    freeReleaseCompile
    paidReleaseCompile
}

dependencies {

    freeDebugCompile project(path: ':lib', configuration: 'freeDebug')
    paidDebugCompile project(path: ':lib', configuration: 'paidDebug')
    freeReleaseCompile project(path: ':lib', configuration: 'freeRelease')
    paidReleaseCompile project(path: ':lib', configuration: 'paidRelease')

}
R.Desai
  • 1,112
  • 4
  • 16
0

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.

Alexandar
  • 115
  • 1
  • 7