前提:我想本地化 App 名称(使用字符串资源)
同时从 gradle 文件添加构建类型后缀(“DEBUG”)。
我在这里尝试连接字符串资源“app_name”和 gradle 变量“AppNameSuffix”
Expected app name :
for product flavor "all" - "My application DEBUG"
for product flavor "china" - "My Chinese application DEBUG"
and subsequent build type suffixed for "RELEASE" and "CANARY"
build.gradle:
buildTypes {
debug {
manifestPlaceholders = [AppNameSuffix: "DEBUG"]
...
}
release{
manifestPlaceholders = [AppNameSuffix: "RELEASE"]
...
}
canary{
manifestPlaceholders = [AppNameSuffix: "CANARY"]
}
}
productFlavors {
china {
applicationId "com.myapplication.china"
}
all {
//default
applicationId "com.myapplication.all"
}
}
list :
<application
...
android:label="@{@string/app_name(${AppNameSuffix})}">
...
</application>
评估gradle变量时在“$”符号处出错
主/res/string.xml :
<string name="app_name">My application %s</string>
中国/res/string.xml :
<string name="app_name">My Chinese application %s</string>
References:
最佳答案
在您的 gradle 文件中,使用小写字母以便资源名称有效:
buildTypes {
debug {
manifestPlaceholders = [AppNameSuffix: "_debug"]
...
}
release{
manifestPlaceholders = [AppNameSuffix: "_release"]
...
}
canary{
manifestPlaceholders = [AppNameSuffix: "_canary"]
}
}
修改您的 list :有括号要删除,如果还没有的话,可能要添加 tools:replace:
<application
...
android:label="@string/app_name${AppNameSuffix}"
tools:replace="android:label"
...
</application>
然后你需要添加匹配你的后缀的字符串资源:
<string name="app_name">My application</string>
<string name="app_name_debug">My application DEBUG</string>
<string name="app_name_release">My application RELEASE</string>
<string name="app_name_canary">My application CANARY</string>
您还可以尝试各种风格,并将特定的 strings.xml 文件放入与您的风格相匹配的文件夹中。
关于android - 如何在android list 中连接字符串资源和构建变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54911292/