I have a project which has multiple localizations.
Now I have to replace a certain word in all strings.xml
files for different buildTypes. For example let's say I have this:
<string>My name is Bill</string>
<string>Bill is on duty today</string>
And in another buildType I need to have
<string>My name is Will</string>
<string>Will is on duty today</string>
How can I do it (probably through Gradle)?
You can do something like following in build.gradle but would only apply to that particular string resource.
resValue "string", "<string_name>", string_value
Ok, found a correct solution, which is not a workaround:
For the required buildType
add the following
applicationVariants.all { variant ->
variant.mergeResources.doLast {
def dir = new File("${buildDir}/intermediates/res/merged/${variant.dirName}") //iterating through resources, prepared for including to APK (merged resources)
println("Resources dir " + dir)
dir.eachFileRecurse { file ->
if(file.name.endsWith(".xml")) { //processing only files, which names and with .xml
String content = file.getText('UTF-8')
if(content != null && content.contains("Bill")) {
println("Replacing name in " + file)
content = content.replace("Bill", "Will") //replacing all Bill words with Will word in files
file.write(content, 'UTF-8')
}
}
}
}
The answers here no longer work with latest android build tools, but here is a new version that is working for me.
android.applicationVariants.all { variant ->
variant.mergeResources.doFirst {
variant.sourceSets.each { sourceSet ->
sourceSet.res.srcDirs = sourceSet.res.srcDirs.collect { dir ->
def relDir = relativePath(dir)
copy {
from(dir)
include '**/*.xml'
filteringCharset = 'UTF-8'
filter {
line -> line
.replace('Bill', 'Will')
}
into("${buildDir}/tmp/${variant.dirName}/${relDir}")
}
copy {
from(dir)
exclude '**/*.xml'
into("${buildDir}/tmp/${variant.dirName}/${relDir}")
}
return "${buildDir}/tmp/${variant.dirName}/${relDir}"
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With