如何在字符串资源文件中使用resValue

How to user resValue in String resource file

我在 ~/.gradle/gradle.properties 中定义了一个 属性,我想在字符串资源文件中访问它。

定义在~/.gradle/gradle.properties
ServerIP="XXX.XXX.XX.XX"

app/build.gradle

中的声明
android {  
  buildTypes.each {  
    it.resValue "string", "ServerIP", "SERVER_IP"
  }
}

string/web_services.xml
中的用法 <string name="serverIP">@string/SERVER_IP</string>

构建时出错
Error:(4, 5) No resource found that matches the given name (at 'serverIP' with value '@string/SERVER_IP').

有什么办法可以实现还是做不到?

这一行有问题:

it.resValue "string", "ServerIP", "SERVER_IP"

第一个参数是type,第二个是name,第三个是value。在您的情况下,名称应为 "SERVER_IP",值应为 ServerIP,不带引号。 build.gradle 中的最后一块:

android {  
  buildTypes.each {  
    it.resValue "string", "SERVER_IP", ServerIP
  }
}