Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jbruchanov/1d20e7a6f77ec6e20a7be244a5551dce to your computer and use it in GitHub Desktop.
Save jbruchanov/1d20e7a6f77ec6e20a7be244a5551dce to your computer and use it in GitHub Desktop.
Gradle code generation
task generateVClass << {
android.applicationVariants.all { variant ->
variant.outputs.each { output ->
def sep = System.getProperty("file.separator")
def rFileTemplate = "%s/generated/source/r/%s/%s/R.java".replace("/", sep)
def rFile = new File(String.format(rFileTemplate, project.buildDir, output.dirName.replace("/", sep), variant.applicationId.replace(".", sep)))
if (!rFile.exists()) {//clean, error or variant which is not currently building
return
}
println("Parsing:" + rFile.absolutePath)
//generate class content
def vClassFile = new File(String.format("%s/src/main/java/com/vodafone/auto/V.java", getProjectDir().absolutePath).replace("/", sep))
def vClassContent = /*tools.gradle*/generateStringHelpClass(project.projectDir.absolutePath, rFile)
//update it only with content change, git sees file attributes about update
if (!vClassFile.exists() || !(vClassContent.equals(vClassFile.text))) {
vClassFile.delete()
vClassFile.write(vClassContent)
}
}
}
}
afterEvaluate {
android.applicationVariants.all { variant ->
variant.javaCompiler.dependsOn(generateVClass)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment