Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

define manifest place holders in build.gradle

I have a few manifest placeholders in my AndroidManifest.xml file

In build.gradle I want to dynamically populate those values depending on my flavor and build type.

How can I do that ?

I wrote a function that does the following

def getKey() {
    def KeyToReturn = ""
    android.applicationVariants.all { variant ->
        printout("getKey: ${variant.name}")
        def flavor = "default";
        if (variant.productFlavors.size() > 0)
            flavor = variant.productFlavors.get(0);


        def buildType = variant.buildType.name

        if (buildType == "debug" || buildType == "staging") {
            if (flavor.name == "one") {
                KeyToReturn = test_key_1
            }
            if (flavor.name == "two") {
                KeyToReturn = test_key_2
            }

        }
        if (buildType == "release") {
            if (flavor.name == "one") {
                KeyToReturn = live_key_1
            }
            if (flavor.name == "two") {
                KeyToReturn = live_key_2
            }
        }
    }
    printout("KeyToReturn:" + KeyToReturn)
    return KeyToReturn
}

I have this in my android.defaultConfig

defaultConfig {
    minSdkVersion 15
    targetSdkVersion 23
    versionCode getVersionCode1()
    versionName getVersionName1() + ""
    manifestPlaceholders = [key: getKey()]
}

This is what my AndroidManifest.xml relevant part contains

<meta-data
            android:name="key"
            android:value="${key}"/>

The problem is when I look in the built AndroidManifest.xml file the ${key} value is an empty string.

How do I populate this value correctly ?

like image 302
Lena Bru Avatar asked Oct 24 '25 16:10

Lena Bru


1 Answers

You define manifest placeholders based on product flavors (and AFAIK build type) by adding them to the proper closure.

In this sample app, I use product flavors to use different rules for Android 7.0's network security configuration:

productFlavors {
    thawte {
        resValue "string", "app_name", "CA Validation Demo"
        applicationId "com.commonsware.android.downloader.ca.thawte"
        manifestPlaceholders=
                [networkSecurityConfig: 'network_thawte']
        buildConfigField "String", "URL", WARES
    }
    verisign {
        resValue "string", "app_name", "Invalid CA Validation Demo"
        applicationId "com.commonsware.android.downloader.ca.verisign"
        manifestPlaceholders=
                [networkSecurityConfig: 'network_verisign']
        buildConfigField "String", "URL", WARES
    }
    system {
        resValue "string", "app_name", "System CA Validation Demo"
        applicationId "com.commonsware.android.downloader.ca.system"
        manifestPlaceholders=
                [networkSecurityConfig: 'network_verisign_system']
        buildConfigField "String", "URL", WARES
    }
    pin {
        resValue "string", "app_name", "Cert Pin Demo"
        applicationId "com.commonsware.android.downloader.ca.pin"
        manifestPlaceholders=
                [networkSecurityConfig: 'network_pin']
        buildConfigField "String", "URL", WARES
    }
    invalidPin {
        resValue "string", "app_name", "Cert Pin Demo"
        applicationId "com.commonsware.android.downloader.ca.invalidpin"
        manifestPlaceholders=
                [networkSecurityConfig: 'network_invalid_pin']
        buildConfigField "String", "URL", WARES
    }
    selfSigned {
        resValue "string", "app_name", "Self-Signed Demo"
        applicationId "com.commonsware.android.downloader.ca.ss"
        manifestPlaceholders=
                [networkSecurityConfig: 'network_selfsigned']
        buildConfigField "String", "URL", SELFSIGNED
    }
    override {
        resValue "string", "app_name", "Debug Override Demo"
        applicationId "com.commonsware.android.downloader.ca.debug"
        manifestPlaceholders=
                [networkSecurityConfig: 'network_override']
        buildConfigField "String", "URL", SELFSIGNED
    }
}

I can then reference the placeholder in the manifest normally:

<application
  android:icon="@drawable/ic_launcher"
  android:label="@string/app_name"
  android:networkSecurityConfig="@xml/${networkSecurityConfig}">
like image 188
CommonsWare Avatar answered Oct 26 '25 04:10

CommonsWare



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!