Skip to content

Instantly share code, notes, and snippets.

@navneet-kumar
Created June 25, 2019 08:29
Show Gist options
  • Select an option

  • Save navneet-kumar/a003071c435dd04d6464670c4ee78899 to your computer and use it in GitHub Desktop.

Select an option

Save navneet-kumar/a003071c435dd04d6464670c4ee78899 to your computer and use it in GitHub Desktop.
Gradle task to generate kotlin data class on the fly from xml file
task generateStrings{
String dir = "build/sources"
delete dir
mkdir(dir)
def f = file(dir + "/StringsEn.kt")
File file = file("src/main/resources/strings_en.xml")
f.append("package myCompany.framework.data\n")
f.append("data class StringsEn constructor (\n")
def strings = []
def xml = new XmlParser().parse(file)
xml.children().each {string ->
String val = string.text()
val = val.replaceAll("\"","\\\\\"").replace('$','\\$')
strings.add( "public val " + string.@name + " : String = \"" + val + "\"")
}
f.append(strings.join(",\n") + ")")
sourceSets.main.java.srcDir(dir)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment