Sebastien
Sebastien

Reputation: 4295

Android configuration specific to each combination of 2 flavor dimensions

I am trying to add a second flavor dimension to an existing Android project. I already had a dimension for different environments (DEV, BETA, PROD), each with their own backend API and their own application id (to be able to install apps connected to several environments on the same device). And now I want to add another dimension for 2 variants of my app, a general one with all the features, and a specific one with a subset of features. And in addition to that, I still have the default debug and release build types I want to keep.

So here is what my configuration in build.gradle looks like so far:

android {
    compileSdkVersion 30
    defaultConfig {
        applicationId "com.example"
        minSdkVersion 21
        targetSdkVersion 30
        versionCode 56
        versionName "1.1.6"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        manifestPlaceholders = [
                appAuthRedirectScheme: 'com.example'
        ]
    }
    signingConfigs {...}
    buildTypes {
        debug {...}
        release {...}
    }
    flavorDimensions "env", "variant"
    productFlavors {
        dev {
            dimension "env"
            applicationIdSuffix ".dev"
            resValue 'string', 'backend_url', 'https://dev.example.com/api/v1/'
            manifestPlaceholders = [
                    appAuthRedirectScheme: 'com.example.dev'
            ]
        }
        beta {
            dimension "env"
            applicationIdSuffix ".beta"
            resValue 'string', 'backend_url', 'https://beta.example.com/api/v1/'
            manifestPlaceholders = [
                    appAuthRedirectScheme: 'com.example.beta'
            ]
        }
        prod {
            dimension "env"
            resValue 'string', 'backend_url', 'https://example.com/api/v1/'
            manifestPlaceholders = [
                    appAuthRedirectScheme: 'com.example'
            ]
        }
        general {
            dimension "variant"
            applicationId "com.example"
        }
        specific {
            dimension "variant"
            applicationId "com.example.specific"
        }
    }
    ...
}

As you can see, I will be able to have an application variant specific to each environment/variant flavor by combining applicationId and applicationIdSuffix.

But I also need to have the corresponding mapping in manifestPlaceholders, knowing that the appAuthRedirectScheme placeholder is not integrated inside of my project's manifest, but in the openId Appauth dependency, so I can't just have several manifests in the various flavor directories like I have read elsewhere.

Is there a way to define a build setting in build.gradle that is specific to each flavor dimension combination? In other words, I would like to have different values of manifestPlaceholders for devGeneral, devSpecific, betaGeneral, betaSpecific, prodGeneral and prodSpecific.

Upvotes: 2

Views: 1061

Answers (1)

blurkidi
blurkidi

Reputation: 165

I don't specially advice this solution, but you could try the following approach:

applicationVariants.all { variant ->
    if(variant.productFlavors.get(0).name == "dev") {
        if (variant.buildType.name == "debug" ) {
            variant.getMergedFlavor().manifestPlaceholders = [
                    appAuthRedirectScheme: 'com.example.dev'
            ]
        }
        if (variant.buildType.name == "release" ) {
            variant.getMergedFlavor().manifestPlaceholders = [
                    appAuthRedirectScheme: 'com.example.dev.foo'
            ]
        }
    }
    if(variant.productFlavors.get(0).name == "beta") { ... }
    if(variant.productFlavors.get(0).name == "prod") { ... }
}

Upvotes: 2

Related Questions