android - 如何在android list 中连接字符串资源和构建变量

标签 android android-gradle-plugin concatenation build.gradle android-manifest

前提:我想本地化 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/

相关文章:

android - 应用程序的多个版本:带有和不带有AD

java - Android gradle从多个目录复制到一个.jar

java - 性能 (JAVA) ~ 循环中的字符串连接,带有前置和附加

python - 沿任意轴连接未知维度的numpy数组

android 480x800 hdpi 和 480x800 mdpi 之间的区别

java - Android 2.3 ProgressDialog不会显示

android - gradle build 在 lint 任务上失败

php - MySQL concat() 无法正常工作

android - AAR 缺少我的依赖项

android - ListView 中的复选框 - 当我选择第一个时,它会检查最后一个,反之亦然