Replace word in strings.xml with gradle for a buildType
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}"
}
}
}
}