首页 > 解决方案 > 如何优先于由库定义的strings.xml 由flavor 定义的strings.xml?

问题描述

我有一个具有多种风格的 Android 应用程序,每种风格都在其自己的 strings.xml 文件中定义 app_name 字符串。最近我合并了一个新库,它在自己的 strings.xml 文件中定义了自己的 app_name 值。问题是合并资源中 app_name 的最终值是库定义的 app_name 值。如何优先考虑风味 app_name 值?

目前,我在每个风味的单个 strings.xml 文件中为 app_name 定义特定值。

我希望 app_name 值是风味值而不是库值。

更新: 我的口味定义如下:

    productFlavors {
        doctoronline {
            dimension "client"
            applicationId 'com.doctoronline.doctoronline'
            resValue 'string', 'filebrowser_provider', 'com.doctoronline.doctoronline.aditya.fileprovider'
            buildConfigField 'String', 'CORPORATE_DOMAIN', '"e4c4aa0f523941d2a332d15101f12e9e"'
            buildConfigField 'String', 'SHORT_NAME', '"DRONLINE"'
            buildConfigField 'String', 'WAITING_MESSAGE', '"DRONLINE_WAITING_MESSAGE_PATIENT_DEFAULT"'
        }
        mapfre {
            dimension "client"
            applicationId 'com.doctoronline.mapfre'
            resValue 'string', 'filebrowser_provider', 'com.doctoronline.mapfre.aditya.fileprovider'
            buildConfigField 'String', 'CORPORATE_DOMAIN', '"3282a15f144e288bac4c07b1598e9234"'
            buildConfigField 'String', 'SHORT_NAME', '"mapfre"'
            buildConfigField 'String', 'WAITING_MESSAGE', '"MAPFRE_WAITING_MESSAGE_PATIENT_DEFAULT"'
        }
...
//4 or 5 extra flavors
}

我要添加的库是this

标签: androidandroid-resources

解决方案


如果您想根据您构建的产品风格更改您的应用程序名称,您有两种选择:
1. 使用resValue。这可以帮助您将一些资源添加到您的应用程序中,例如字符串资源、颜色资源。
2. 使用manifestPlaceHolder。这可以帮助您从 build.gradle 文件向 android manifest 文件中添加变量,并通过构建类型或产品风格更改这些变量。

这是如何将 resValue 添加到您的产品风味中

productFlavors {
        flavor1 {
            dimension = 'test'
            resValue("string", "app_name", "MY APP NAME")
        }
        flavor2 {
            dimension = 'test'
            resValue("string", "app_name", "MY APP NAME")
        }
    }

如果您的 strings.xml 具有名称为app_name的字符串,则必须将其删除以避免重复 res 值消息。
检查应用程序名称是否通过不同的产品风格更改了您的产品风格并构建项目,然后检查清单文件的应用程序标记中出现的值。

第二种方法是使用 manifestPlaceHolder。在您的产品风味中添加 manifestPlaceHolder,然后在清单文件中的应用程序标记内使用此变量

productFlavors {
        flavor1 {
            dimension = 'test'
            manifestPlaceholders=[appName: 'MY APP NAME']
        }
        flavor2 {
            dimension = 'test'
            manifestPlaceholders=[appName: 'MY APP NAME']
        }
    }

然后导航到您的 android 清单文件

<application
        android:name=".DemoApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="${appName}"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
...
</application>

希望这个答案是有帮助的。快乐编码。


推荐阅读