我在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}值为空字符串。如何正确填充此值?
build.gradle
不会构建您的应用程序,而是构建了一个对象模型,该对象模型--几秒钟、几分钟或几小时后--构建了您的应用程序。因此,您的函数在循环中使用相同的N个构建变体进行评估N次。 - CommonsWaremanifestPlaceholders
。在你的情况下,你不需要manifestPlaceholders
--将你的值放在构建变体特定目录的字符串资源中(例如,oneDebug/res/values/strings.xml
)。这让人想起了John O'Reilly的解决方案,只不过不是由Gradle生成的。 - CommonsWare