如何让 flavor 定义的 strings.xml 优先于库定义的 strings.xml?

How to give priority to strings.xml defined by flavor over strings.xml defined by a library?

我有一个具有多种风格的 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

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

这是将 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>

希望这个回答对您有所帮助。编码愉快。