这是对我在this post上帮助我的答案的延续
我们可以从build.gradle中添加字符串资源,如下所示
productFlavors { resValue { resValue "string“、"app_name”、"InTouch Messenger“} googlePlay{ resValue "string”、"app_name“、"InTouch Messenger: GPE Edition”}}
它的工作原理就像一种魅力,它的目的是让不同的应用程序根据不同的口味命名。(将原始app_name字符串资源从strings.xml文件中删除。
但是,如何为从build.gradle添加的字符串资源添加本地化字符串?
是否有额外的参数可以通过指定区域设置?或可以使用gradle任务来完成它?
注意:--我不能使用strings.xml来完成这个任务(因为我的项目有几种结构方式,所以不可行)
发布于 2016-03-29 10:37:39
不过,关于生成的资源,我的另一个回答可能对您的用例来说太过了。根据我目前对您的项目的了解,我认为这个更适合:(并不是说您仍然可以将其与生成的资源结合起来)。
src/味1/res/value/src s.xml
<string name="app_name_base">InTouch Messenger"</string>
<string name="app_name_gpe">InTouch Messenger: GPE Edition"</string>src/astvor1/res/values hu/src s.xml
<string name="app_name_base">InTouch Üzenetküldő"</string>
<string name="app_name_gpe">InTouch Üzenetküldő: GPE Változat"</string>src/astvor2/res/value/src s.xml
<string name="app_name_base">Whatever Messenger"</string>
<string name="app_name_gpe">Whatever Messenger: GPE Edition"</string>src/香料2/res/values hu/src s.xml`
<string name="app_name_base">Whatever Üzenetküldő"</string>
<string name="app_name_gpe">Whatever Üzenetküldő: GPE Változat"</string>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"
}
}
}这意味着flavor1/2将拥有额外的未使用的app_name_gpe字符串资源,但这将由aapt负责:
android {
buildTypes {
release {
shrinkResources true // http://tools.android.com/tech-docs/new-build-system/resource-shrinking
}发布于 2016-03-29 04:36:30
如果不需要对这些字符串进行操作,最好的选择是移动到strings.xml,但这将使您在两种版本之间共享所有的res文件夹。如果您基于build.gradle上的某些属性生成这些字符串,那么不幸的是,我认为您运气不佳。
编辑:澄清了上述操作的含义,并添加了一些选项:
通过在这些字符串上操作,我指的是在构建过程中与构建参数、命令行或环境变量的某种连接(例如,获得提交SHA1,以便以后更容易跟踪bug)。如果不需要操作,则可以选择strings.xml。但是,当您为风味覆盖一个res文件夹时,所有这些都会被覆盖,如果除了有限数量的字符串之外,多个版本共享相同的res,则可能会造成问题。
如果每个APK都有自己的区域设置,那么它只是一个resValue或buildConfigField。您可以定义变量,以便更容易地重用值。有点像
def myVar = "var"
...
flavor1 {
resValue "string", "my_res_string", "${myVar}"
}
flavor2 {
resValue "string", "my_res_string", "${myVar}"
}但是,如果同一APK中需要多个区域设置,并且Android将在运行时选择它,则字符串必须位于正确的values-<locale>文件夹中。
发布于 2016-03-29 10:14:56
您在这里的不同级别上操作,BuildConfig是代码,因此没有本地化,这就是为什么我们对硬编码字符串有Lint警告。安卓的本地化是通过<string资源完成的,如果你想让系统在运行时根据用户的设置选择语言,那就没有办法了。拥有资源的方法有很多种:values文件夹、resValue in build.gradle和生成的资源。
您应该查看 project in Gradle,例如,我使用它从src/main/values/stuff.xml生成SQL。下面是一些代码。
buildSrc/build.gradle
// To enable developing buildSrc in IDEA import buildSrc/build.gradle as a separate project
// Create a settings.gradle in buildSrc as well to prevent importing as subproject
apply plugin: 'groovy'
repositories { jcenter() }
dependencies {
compile localGroovy()
compile gradleApi()
testCompile 'junit:junit:4.12'
}buildSrc/src/main/groovy/Plugin.groovy
import org.gradle.api.*
/**
* Use it as
* <code>
* apply plugin: MyPlugin
* myEntities {
* categories {
* input = file(path to Android res xml with Strings)
* output = file(path to asset SQL file)
* conversion = "structure|SQL"
* }
* }
* </code>
*/
class MyPlugin implements Plugin<Project> {
void apply(Project project) {
def entities = project.container(MyEntity)
// this gives the name for the block in build.gradle
project.extensions.myEntities = entities
def allTasks = project.task('generateYourStuff')
def allTasksClean = project.task('cleanGenerateYourStuff')
project.afterEvaluate {
entities.all { entity ->
//println "Creating task for ${entity.name} (${entity.input} --${entity.conversion}--> ${entity.output})"
def task = project.task(type: GenerateTask, "generateYourStuff${entity.name.capitalize()}") {
input = entity.input
output = entity.output
conversion = entity.conversion
}
allTasks.dependsOn task
// clean task is automagically generated for every task that has output
allTasksClean.dependsOn "clean${task.name.capitalize()}"
}
}
}
}
class MyEntity {
def input
def output
String conversion
final String name
MyEntity(String name) {
this.name = name
}
}buildSrc/src/main/groovy/GenerateTask.groovy
import net.twisterrob.inventory.database.*
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.*
class GenerateTask extends DefaultTask {
@InputFile File input
@OutputFile File output
@Optional @Input String conversion
@TaskAction void generate() {
input.withReader { reader ->
// you may need to treat output as a folder
output.parentFile.mkdirs()
output.withWriter { writer ->
// custom transformation here read from reader, write to writer
}
}
}
}这只是一个框架,您可以在这里疯狂地做任何事情:例如,通过网络检索一个CSV并将内容传播到生成的variant*/res/values-*/gen.xml文件中。
您可以在需要时手动运行它,或者在构建生命周期的正确点运行它(在build.gradle中)。
android.applicationVariants.all { com.android.build.gradle.api.ApplicationVariant variant ->
variant.mergeAssets.dependsOn tasks.generateYourStuff
}https://stackoverflow.com/questions/36105494
复制相似问题