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

android– 在build.gradle中定义清单占位符,第1张

概述我的AndroidManifest.xml文件中有一些显式占位符在build.gradle中,我想根据我的风格和构建类型动态填充这些值.我怎样才能做到这一点?我编写了一个执行以下 *** 作的函数defgetKey(){defKeyToReturn=""android.applicationVariants.all{variant->prin

我的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}

我在androID.defaultConfig中有这个

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

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

<Meta-data            androID:name="key"            androID:value="${key}"/>

问题是当我查看构建的AndroIDManifest.xml文件时
${key}值是一个空字符串.@H_419_32@

如何正确填充此值?

解决方法:

您可以通过将它们添加到正确的闭包来基于产品风格(和AFAIK构建类型)定义清单占位符.

在this sample app中,我使用产品风格为AndroID 7.0的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    }}

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

<application  androID:icon="@drawable/ic_launcher"  androID:label="@string/app_name"  androID:networkSecurityConfig="@xml/${networkSecurityConfig}">
总结

以上是内存溢出为你收集整理的android – 在build.gradle中定义清单占位符全部内容,希望文章能够帮你解决android – 在build.gradle中定义清单占位符所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1108557.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存