在build.gradle中定义清单占位符

Len*_*Bru 3 android android-gradle-plugin

我在我的几个清单的占位符AndroidManifest.xml文件

build.gradle我要动态地填充这取决于我的风味和构建类型的值。

我怎样才能做到这一点 ?

我写了一个执行以下操作的函数

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
}
Run Code Online (Sandbox Code Playgroud)

我在android.defaultConfig中有这个

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

这是我的AndroidManifest.xml相关部分包含的内容

<meta-data
            android:name="key"
            android:value="${key}"/>
Run Code Online (Sandbox Code Playgroud)

问题是当我在生成的AndroidManifest.xml文件中查看时,$ {key}的值是一个空字符串。

如何正确填充此值?

Com*_*are 5

您可以根据产品口味(和AFAIK构建类型)定义清单占位符,方法是将它们添加到适当的闭合中。

此示例应用程序中,我使用产品风格对Android 7.0的网络安全配置使用不同的规则:

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
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,我可以正常地在清单中引用占位符:

<application
  android:icon="@drawable/ic_launcher"
  android:label="@string/app_name"
  android:networkSecurityConfig="@xml/${networkSecurityConfig}">
Run Code Online (Sandbox Code Playgroud)