In an Android gradle build, change contents of strings.xml during build -
i able load value of user-visible strings res/values/strings.xml our cms (or db), during gradle build.
e.g.
<string name="button_label">ok, it</string>
could changed
<string name="button_label">ok, now!</string>
... or whatever.
the idea being new value read @ build time our cms, , baked apk file. ( motivation automate changes app strings, text app read existing cms).
what's best way achieve this? possible generate/modify resource files (e.g. strings.xml ) during build, before used android build system?
adding more requirements:
- the solution needs support "alternative resources", aka overriding strings other languages. (http://developer.android.com/guide/topics/resources/providing-resources.html#alternativeresources)
- the solution should able override values in original (default) strings.xml file.
here's solution strings modified via external properties file. not sure fits requirement exactly, should off start.
i similar in builds - have custom gradle tasks type of replacement called inside of dofirst()
. regex here use polishing, , input format may change per requirement, in few local tests seems work me.
ext.updatestrings = { println("changing strings!") properties props = new properties() props.load(new fileinputstream(file('cms.properties'))) def stringsfile = file("src/main/res/values/strings.xml") def stringstext = stringsfile.gettext() (p in props) { def pattern = pattern.compile(string.format("%s\">.*<", p.key)); def match = pattern.matcher(stringstext); match.find(); println("found key " + p.key + ", replacing string " + p.value) stringstext = match.replaceall(string.format("%s\">%s<", p.key, p.value)) stringsfile.write(stringstext) } }
i keep external properties/functions in separate file (for instance common.gradle). in build.gradle, add custom task so:
apply from: "common.gradle" task updatestringsxml() { dofirst { updatestrings() } }
then, gradle command might gradle updatestringsxml assemblerelease
.
Comments
Post a Comment