使用"resValue"本地化通过build.gradle添加的字符串资源

And*_*tel 13 android android-resources android-studio android-gradle-plugin android-productflavors

这是对这篇文章帮助我的答案的延续

我们可以添加字符串资源,如下所示build.gradle:

productFlavors {
    main{
        resValue "string", "app_name", "InTouch Messenger"
    }

    googlePlay{
        resValue "string", "app_name", "InTouch Messenger: GPE Edition"
    }
}
Run Code Online (Sandbox Code Playgroud)

它的作用就像一个魅力,并且每个风格都有不同的app名称.(app_name从strings.xml文件中删除原始字符串资源.

但是,我们如何为这个添加的字符串资源添加本地化字符串build.gradle?

我们可以通过指定区域设置的附加参数吗? 或者 可以使用gradle任务来完成它?

注意:我不能这样做strings.xml(因为我的项目结构的几种方式不可行)

TWi*_*Rob 8

关于生成的资源我的另一个答案可能对你的用例来说太过分了.基于我目前对您的项目的了解,我认为这个更合适:(不是说您仍然可以将其与生成的资源相结合)

SRC/flavor1/RES /值/ strings.xml中

<string name="app_name_base">InTouch Messenger"</string>
<string name="app_name_gpe">InTouch Messenger: GPE Edition"</string>
Run Code Online (Sandbox Code Playgroud)

SRC/flavor1/RES /值-HU/strings.xml中

<string name="app_name_base">InTouch Üzenetküld?"</string>
<string name="app_name_gpe">InTouch Üzenetküld?: GPE Változat"</string>
Run Code Online (Sandbox Code Playgroud)

SRC/flavor2/RES /值/ strings.xml中

<string name="app_name_base">Whatever Messenger"</string>
<string name="app_name_gpe">Whatever Messenger: GPE Edition"</string>
Run Code Online (Sandbox Code Playgroud)

SRC/flavor2/RES /值-HU/strings.xml`

<string name="app_name_base">Whatever Üzenetküld?"</string>
<string name="app_name_gpe">Whatever Üzenetküld?: GPE Változat"</string>
Run Code Online (Sandbox Code Playgroud)

的build.gradle

android {
    sourceSets {
        [flavor1, flavor3].each {
            it.res.srcDirs = ['src/flavor1/res']
        }
        [flavor2, flavor4].each {
            it.res.srcDirs = ['src/flavor2/res']
        }
    }
    productFlavors { // notice the different numbers than sourceSets
        [flavor1, flavor2].each {
            it.resValue "string", "app_name", "@string/app_name_base"
        }
        [flavor3, flavor4].each {
            it.resValue "string", "app_name", "@string/app_name_gpe"
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这意味着flavor1/2将有一个额外的未使用的app_name_gpe字符串资源,但这将由aapt处理:

android {
    buildTypes {
        release {
            shrinkResources true // http://tools.android.com/tech-docs/new-build-system/resource-shrinking
        }
Run Code Online (Sandbox Code Playgroud)


Dou*_*ond 5

如果您不必对这些字符串进行操作,最好的选择是移动到strings.xml,但这将使您res在风格之间共享所有文件夹。如果您根据 上的某些属性生成这些字符串build.gradle,那么不幸的是,我认为您运气不好。

编辑: 澄清我上面的操作的意思并添加一些选项:

通过对这些字符串进行操作,我的意思是与构建参数进行某种串联,在构建过程中从命令行或环境变量进行读取(例如,获取提交 SHA1,以便以后更容易跟踪错误)。如果不需要任何操作,strings.xml可能是一种选择。但是,当您覆盖风味文件夹时,所有文件夹都会被覆盖,如果除有限数量的字符串外,res多个风味共享相同的内容,则可能会出现问题。res

如果每个 APK 都有自己的区域设置,那么它只是一个resValue或buildConfigField一个风格。您可以定义变量以便更轻松地重用值。就像是

def myVar = "var"

...

flavor1 {
    resValue "string", "my_res_string", "${myVar}"
}

flavor2 {
    resValue "string", "my_res_string", "${myVar}"
}
Run Code Online (Sandbox Code Playgroud)

但是,如果同一个 APK 中需要多个语言环境,并且 Android 将在运行时选择它,则该字符串必须位于正确的values-<locale>文件夹中。