Skip to content

Instantly share code, notes, and snippets.

@Nilzor
Last active August 5, 2025 07:18
Show Gist options
  • Save Nilzor/73d2456ee059395ffad4c2a404f35e5e to your computer and use it in GitHub Desktop.
Save Nilzor/73d2456ee059395ffad4c2a404f35e5e to your computer and use it in GitHub Desktop.
Gradle task to generate a PUML dependency graph of android gradle modules.
tasks.register("exportFullDependencyGraphAsPlantUml") {
group = "reporting"
description = "Generates a full transitive dependency graph as PlantUML"
doLast {
val configName = "${variant}RuntimeClasspath"
val configList = configurations.toList()
val config = configurations.findByName(configName)
?: configurations.findByName("qaDebugRuntimeClasspath") // <- Modify this per project
?: throw GradleException("Configuration '$configName' not found in project ${project.name}")
val output = File("$buildDir/reports/local-project-dependencies-$variant.puml")
output.parentFile.mkdirs()
val visitedEdges = mutableSetOf<Pair<String, String>>()
fun isProjectComponent(component: ResolvedComponentResult): Boolean {
return component.id.displayName.startsWith("project ")
}
fun formatProject(component: ResolvedComponentResult): String {
// Output something like ":modules:RuterSaleAndroidLibrary"
return component.id.displayName.removePrefix("project ")
}
fun walk(dep: ResolvedDependencyResult) {
val fromComp = dep.from as? ResolvedComponentResult ?: return
val toComp = dep.selected
if (!isProjectComponent(fromComp) || !isProjectComponent(toComp)) return
val from = formatProject(fromComp)
val to = formatProject(toComp)
val edge = from to to
if (!visitedEdges.add(edge)) return
toComp.dependencies
.filterIsInstance<ResolvedDependencyResult>()
.forEach { walk(it) }
}
val resolution = config.incoming.resolutionResult
resolution.root.dependencies
.filterIsInstance<ResolvedDependencyResult>()
.forEach { walk(it) }
output.printWriter().use { out ->
out.println("@startuml")
visitedEdges.forEach { (from, to) ->
out.println("\"$from\" --> \"$to\"")
}
out.println("@enduml")
}
println("Local-only PlantUML dependency graph written to: ${output.absolutePath}")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment