Last active
October 23, 2023 16:56
-
-
Save medvedev/968119d7786966d9ed4442ae17aca279 to your computer and use it in GitHub Desktop.
Gradle task that prints total dependencies size and (dependency+(size in kb)) list sorted by size desc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
/* Tested with Gradle 6.3 */ | |
tasks.register("depsize") { | |
description = 'Prints dependencies for "default" configuration' | |
doLast() { | |
listConfigurationDependencies(configurations.default) | |
} | |
} | |
tasks.register("depsize-all-configurations") { | |
description = 'Prints dependencies for all available configurations' | |
doLast() { | |
configurations | |
.findAll { it.isCanBeResolved() } | |
.each { listConfigurationDependencies(it) } | |
} | |
} | |
def listConfigurationDependencies(Configuration configuration) { | |
def formatStr = "%,10.2f" | |
def size = configuration.collect { it.length() / (1024 * 1024) }.sum() | |
def out = new StringBuffer() | |
out << "\nConfiguration name: \"${configuration.name}\"\n" | |
if (size) { | |
out << 'Total dependencies size:'.padRight(65) | |
out << "${String.format(formatStr, size)} Mb\n\n" | |
configuration.sort { -it.length() } | |
.each { | |
out << "${it.name}".padRight(65) | |
out << "${String.format(formatStr, (it.length() / 1024))} kb\n" | |
} | |
} else { | |
out << 'No dependencies found'; | |
} | |
println(out) | |
} | |
... |
@medvedev Ok.Thanks a lot!
In case somebody needs the same in .kts
tasks.register("depsize") {
description = "Prints dependencies for \"default\" configuration"
doLast {
listConfigurationDependencies(configurations["default"])
}
}
tasks.register("depsize-all-configurations") {
description = "Prints dependencies for all available configurations"
doLast {
configurations
.filter { it.isCanBeResolved }
.forEach { listConfigurationDependencies(it) }
}
}
fun listConfigurationDependencies(configuration: Configuration ) {
val formatStr = "%,10.2f"
val size = configuration.map { it.length() / (1024.0 * 1024.0) }.sum()
val out = StringBuffer()
out.append("\nConfiguration name: \"${configuration.name}\"\n")
if (size > 0) {
out.append("Total dependencies size:".padEnd(65))
out.append("${String.format(formatStr, size)} Mb\n\n")
configuration.sortedBy { -it.length() }
.forEach {
out.append(it.name.padEnd(65))
out.append("${String.format(formatStr, (it.length() / 1024.0))} kb\n")
}
} else {
out.append("No dependencies found")
}
println(out)
}
@phi1ipp thanks :)
Hi, I'm getting this error > Could not get unknown property 'default' for configuration container of type org.gradle.api.internal.artifacts.configurations.DefaultConfigurationContainer.
when I run ./gradlew depsize
.
I'm using gradle 7.2.2 by the way, any help will be much appreciated.
Thanks.
For anyone who is having issues with the configurations.default
, you can do the following.
- Print all your configurations to see what you've got.
- Replace
default
with a configuration that you would like to analyze. For example, you can usedebugImplementationDependenciesMetadata
instead ofdefault
. - Run
./gradlew depsize
cc: @ansyori28
configurations["default"].isCanBeResolved = true
on the top of build.gradle.kts
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@UltramanTIGA
Yep, you may for example change "depsize" task to use a configuration other than "default".