使用现有字符串定义定义“resValue”

问题描述 投票:0回答:4

我有兴趣在 strings.xml 文件而不是 build.gradle 中定义我的应用程序的多种风格。鉴于多种风格,我想要一个简化的发布/调试变体:

  buildTypes {
    release {
        signingConfig signingConfigs.release
        resValue "string", "app_name", "@string/prod_name"
    }
    debug {
        applicationIdSuffix ".beta"
        resValue "string", "app_name", "@string/beta_name"
    }

然后在我的每个构建风格的自定义 res/values/strings.xml 文件中,我将定义每个自己的“prod_name”和“beta_name”。我也想使用这个类似的框架来定义提供商的权限等......

目前可以通过 gradle 命令行构建良好的版本,但无法被 Android Studio 识别。

Android Studio 错误:

我在“ generated.xml”中找到了这个

<!-- Values from build type: debug -->
<string name="app_name">@string/beta_name</string>

这是一个字符串引用另一个字符串的典型方式。但是这次 Android Studio 给了我这个错误:

Error:(7, 29) No resource found that matches the given name 
(at 'app_name' with value '@string/beta_name').

我正在使用 Android Studio 2.1 Preview 5

android gradle android-gradle-plugin build.gradle android-resources
4个回答
18
投票

这实际上是可能的。您所要做的就是在 defaultConfig 中将这些字符串声明为空,如下所示:

defaultConfig {
    resValue "string", "prod_name", ""
    resValue "string", "beta_name", ""
}

14
投票

根据我的经验,您无法解析

@string/my_string
DSL 中的
resValue
。 Gradle 将值作为简单字符串放入资源文件中。

在这种情况下,您可以使用不同的文件夹来实现它:

只需使用:

src/release/res/values/strings.xml
src/debug/res/values/strings.xml

如果您想为每个构建变体(构建类型+风格)使用不同的资源,您可以使用:

src/flavor1Release/res/values/strings.xml
src/flavor1Debug/res/values/strings.xml
src/flavor2Release/res/values/strings.xml
src/flavor2Debug/res/values/strings.xml

3
投票

您可以在 gradle 中添加 res 值以适应您的构建风格

productFlavors {

   prod {
        dimension "default"
        resValue "string", "app_name", "your prod app name"    
    }


    staging {
       dimension "default"
        resValue "string", "app_name", "your staging app name"    

    }
}

0
投票

我正在使用另一种方法。

替换 AndroidManifest 中的 android:label:

 android:label="${appName}"

将其粘贴到您的构建中,gradle - buildTypes - 您的环境

示例:

buildTypes {
  create("staging") {
     ,,,
     manifestPlaceholders["appName"] = "*Your App Name* Staging"
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.