【问题标题】:String resource from debug variant does not work来自调试变体的字符串资源不起作用
【发布时间】:2017-08-25 18:34:48
【问题描述】:

我曾经在gradle中定义了一个应用名作为资源:

android {
 compileSdkVersion 26
 buildToolsVersion '26.0.1'
 defaultConfig {
    applicationId "lelisoft.com.lelimath"
    resValue 'string', 'app_name', 'LeliMath'
 }
 buildTypes {
    debug {
        applicationIdSuffix ".debug"
        resValue 'string', 'app_name', 'LeliMath DEV'
    }
 }

然后我发现需要本地化,所以我从gradle中删除了所有resValue并在strings.xml中定义:

app\src\debug\res\values\strings.xml
<string name="app_name">LeliMath DEV</string>

app\src\main\res\values\strings.xml
<string name="app_name">Leli Math</string>

app\src\main\res\values-cs\strings.xml
<string name="app_name">Leli Matematika</string>

当我在 Studio 中编译我的应用程序时,它会显示 Leli Matematika。我期待 LeliMath DEV。我受到https://stackoverflow.com/a/37579475/1639556这个答案的启发。

【问题讨论】:

  • 您为自定义位置创建了一个值目录。所以你必须在调试中创建相同的目录才能使其工作。看我的回答。

标签: android android-build android-build-flavors


【解决方案1】:

你可以通过将字符串放在这里得到它。

app\src\debug\res\values-cs\strings.xml
<string name="app_name">LeliMath DEV</string>

希望对你有帮助:)

【讨论】:

  • 是的,它可以工作,但为什么呢?为什么在调试构建变体中没有回退到值目录?
  • 如果您有多个变体,例如调试、发布或风味,构建工具首先检查特定变体项目。如果资源不适用于特定变体,那么它将从 main.xml 获取。在您的情况下,您使用的是特定于区域的值目录(values-cs),因此构建工具首先检查文件的调试目录。如果它不可用,那么它将从主目录中获取。
  • 我希望如果未定义调试中的 cs,它将在调试中查找默认字符串。如果没有找到,那么它将使用 main.
  • 不幸的是构建系统不是这样设计的。它总是会寻找主要资源。没有办法告诉构建系统这样做。
猜你喜欢
  • 2021-07-13
  • 1970-01-01
  • 2019-04-22
  • 1970-01-01
  • 2014-04-08
  • 1970-01-01
  • 1970-01-01
  • 2016-08-22
  • 1970-01-01
相关资源
最近更新 更多