'Android Studio - Assigning multiple value to ManifestPlaceholders in Gradle

I have two environment of my project one Prod another one is Staging. So whenever I have to build any of the environment, I have to change multiple keys like map key, label name and other things in manifest. So I have searched and find out some of the solutions and manifestPlaceholders is one of them.

Now what I want to do is to assign multiple value in manifestPlaceholders. So can I put multiple values in it and yes then how to put multiple values in it. Here is the code for the manifestPlaceholders

buildTypes {
    debug {
        manifestPlaceholders = [ google_map_key:"your_dev_key"]
    }
    release {
        manifestPlaceholders = [ google_map_key:"prod_key"]
    }
}


Solution 1:[1]

I have solved my problem as below code by adding multiple manifestPlaceholders values. Added this to my module build.gradle.

productFlavors {
        staging {
            applicationId "xxxxxxxxxxx"
            manifestPlaceholders = [ google_map_key:"xxxxxxxxxx", app_label_name:"xxxxxxx"]
            buildConfigField 'String', 'BASE_URL', '"xxxxxxxxxx"'
        }
        prod {
            applicationId "xxxxxxxxxxx"
            manifestPlaceholders = [ google_map_key:"xxxxxxxxxx", app_label_name:"xxxxxxx"]
            buildConfigField 'String', 'BASE_URL', '"xxxxxxxxxx"'
        }
    }

EDIT: You can use resValue also as Emanuel Moecklin suggested in comments.

productFlavors {
            staging {
                applicationId "xxxxxxxxxxx"
                manifestPlaceholders = [ google_map_key:"xxxxxxxxxx", app_label_name:"xxxxxxx"]
                buildConfigField 'String', 'BASE_URL', '"xxxxxxxxxx"'
                resValue "string", "base_url", "xxxxxxxxxx"
            }
            prod {
                applicationId "xxxxxxxxxxx"
                manifestPlaceholders = [ google_map_key:"xxxxxxxxxx", app_label_name:"xxxxxxx"]
                buildConfigField 'String', 'BASE_URL', '"xxxxxxxxxx"'
                resValue "string", "base_url", "xxxxxxxxxx"
            }
        }

Solution 2:[2]

You can easily set/change multiple manifestPlaceholders values. You can either define all values at once, as in your answer, or one by one.

defaultConfig {
    // initialize more values
    manifestPlaceholders = [ google_map_key:"xxxxxxxxxx", app_label_name:"xxxxxxx"]
    // or this way
    manifestPlaceholders.google_map_key = "xxxxxxxxxx"
    manifestPlaceholders.app_label_name = "xxxxxxxxxx"
}

productFlavors {
    staging {
    }
    prod {
        // use some different value for prod
        manifestPlaceholders.google_map_key = "yyyyyyyyyy"
    }
}

Solution 3:[3]

I have mentioned for both build Types and flavors

buildTypes {
    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        resValue "string", "google_maps_key", "release google map key"
    }
    debug {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        resValue "string", "google_maps_key", "debug google map key"
    }
}
productFlavors {
    alpha {
        applicationId = "com.example.alpha"
        resValue 'string', 'app_name', 'alphaapp'
        resValue "string", "maps_api_key", "XXXXXXXXXXXXXXXXXXXXX"
    }
    beta {
        applicationId = "com.example.beta"
        resValue 'string', 'app_name', 'betaapp'
        resValue "string", "maps_api_key", "XXXXXXXXXXXXXXXXXXXXXX"
    }
}

Solution 4:[4]

I could not use the suggested approaches in other answers because gradle seems to have changed the type of manifestPlaceholders to val mutablemap in a recent release.

This was the only fix that worked for me:

manifestPlaceholders["key"] = "value0"
manifestPlaceholders["key"] = "value1"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Robyer
Solution 3 Sanyasirao Mopada
Solution 4 Taslim Oseni