Skip to content

Instantly share code, notes, and snippets.

@koral--
Last active March 2, 2020 01:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save koral--/3d845374aec61c9c54f8942e27f09e58 to your computer and use it in GitHub Desktop.
Save koral--/3d845374aec61c9c54f8942e27f09e58 to your computer and use it in GitHub Desktop.
GenerateNavArgsProguardRulesTask action
private const val APP_NAMESPACE = "http://schemas.android.com/apk/res-auto"
abstract class GenerateNavArgsProguardRulesTask : DefaultTask() {
@get:InputFiles
@get:SkipWhenEmpty
val navigationGraphFiles: FileTree = project.fileTree("src/main/res/navigation") {
include("*.xml")
}
@get:OutputFile
val rulesFile = File(project.buildDir, NAVARGS_PROGUARD_RULES_PATH)
@TaskAction
fun generateRules() {
rulesFile.printWriter().use { writer ->
val documentBuilder = DocumentBuilderFactory.newInstance().apply { isNamespaceAware = true }.newDocumentBuilder()
navigationGraphFiles.forEach { inputFile ->
val xmlDoc = documentBuilder.parse(inputFile)
val arguments = xmlDoc.getElementsByTagName("argument")
(0 until arguments.length)
.mapNotNull { arguments.item(it).attributes.getNamedItemNS(APP_NAMESPACE, "argType")?.nodeValue }
.filter { it.contains('.') }
.toSet()
.map { "-keepnames class $it" }
.forEach(writer::println)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment