Android 如何从build.gradle的ProductFlavors中替换string.xml值

Android 如何从build.gradle的ProductFlavors中替换string.xml值,android,xml,Android,Xml,在strings.xml中 <resources> <string>name="app_name">App_Name</string> <string>name="app_port">https://example.com</string> <string>name="app_key">App_Key</string> </resources> 我需要一个代码来操作build.gr

在strings.xml中

<resources>
<string>name="app_name">App_Name</string>
<string>name="app_port">https://example.com</string>
<string>name="app_key">App_Key</string>
</resources>

我需要一个代码来操作build.gradle文件的ProductFlavors中的strings.xml,而不是为暂存和生产创建单独的文件夹

我找到了答案。使用resValue如下所示

productFlavors {
production {
    applicationId = "com.example.production"
    resValue 'string', 'APP_NAME', 'app_name'
}
staging {
    applicationId = "com.example.staging"
    resValue 'string', 'APP_NAME', 'app_name_stage'
}
它将使用内置目录创建生成的.xml文件。 要获取代码中的值,请像所有其他资源一样使用生成的资源,如下所示:

getResources().getString(R.string.APP_NAME)

我找到了答案。使用resValue如下所示

productFlavors {
production {
    applicationId = "com.example.production"
    resValue 'string', 'APP_NAME', 'app_name'
}
staging {
    applicationId = "com.example.staging"
    resValue 'string', 'APP_NAME', 'app_name_stage'
}
它将使用内置目录创建生成的.xml文件。 要获取代码中的值,请像所有其他资源一样使用生成的资源,如下所示:

getResources().getString(R.string.APP_NAME)

它们有不同的值还是键本身不同?我想下面的链接可能会帮助您[在此处输入链接描述][1][1]:@mixel我在strings.xml文件中指定的内容对于暂存是不同的。@Archana为product flavor创建单独的文件夹是正确的解决方案。我不明白为什么它不适合你。您能否提供有关您的具体案例的更多细节的问题?@mixel我正在尝试更改strings.xml值,而无需为暂存和生产创建单独的文件夹。它们是否具有不同的值或密钥本身不同?我认为以下链接可能会帮助您[在此处输入链接说明][1][1]:@mixel我在strings.xml文件中指定的内容对于暂存是不同的。@Archana为product flavor创建单独的文件夹是正确的解决方案。我不明白为什么它不适合你。您能提供关于您的具体案例的更多细节吗?@mixel我正在尝试更改strings.xml值,而无需为暂存和生产创建单独的文件夹。