-
-
Save medvedev/968119d7786966d9ed4442ae17aca279 to your computer and use it in GitHub Desktop.
... | |
/* 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) | |
} | |
... |
Just what I was looking for - thanks!
@smrahmadi probably to your build.gradle
@RKSharma89 I think this is out of scope of current gist's discussion and is more about general gradle usage
Where segment of build.gradle should we put this code in ?!
buildscript {
ext.kotlin_version = '1.3.70'
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.6.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'android.arch.navigation:navigation-safe-args-gradle-plugin:1.0.0'
}
}
allprojects {
repositories {
jcenter()
google()
}
}
tasks.create("depsize") {
listConfigurationDependencies(configurations.default)
}
tasks.create("depsize-all-configurations") {
configurations.each {
if (it.isCanBeResolved()) {
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)
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Get this error :
Build file '/Users/MYUSER/AndroidStudioProjects/jetpack-playground/build.gradle' line: 23
A problem occurred evaluating root project 'jetpack-playground'.
Could not create task ':depsize'.
Could not get unknown property 'default' for configuration container of type org.gradle.api.internal.artifacts.configurations.DefaultConfigurationContainer.
@smrahmadi what's your gradle version?
@smrahmadi what's your gradle version?
@medvedev
distributionUrl=https://services.gradle.org/distributions/gradle-6.2.2-all.zip
Hm.. maybe they again changed something in API in 6.2.2 :(
I don't have bandwidth to check this during next week and update the gist.
So if you'll find out a solution, please, don't hesitate to share it.
@smrahmadi I've just tested tasks with Gradle 6.3. Worked with no issues.
Gradle version : gradle-5.6.4-all.zip
Plugin : classpath 'com.android.tools.build:gradle:3.6.3'
Error with configurations.getByName("default")
Execution failed for task ':depsize'.
Configuration with name 'default' not found.
Error with configurations.default
Execution failed for task ':depsize'.
Could not get unknown property 'default' for configuration container of type org.gradle.api.internal.artifacts.configurations.DefaultConfigurationContainer.
@RohitSurwase
Maybe, if you don't have default
configuraiton in your project, you should use different configuration name.
I'm not an Android developer, so can't help you with debugging this issue.
However, googling question like gradle default configuration not found
brings up dozens of links.
@medvedev Yes, I did try with other available configuration names and and it get executed successfully just like 'depsize-all-configurations'.
But problem is nothing gets printed as a result.
Hi,bro. My gradle version is 6.1.1, and I edit project root build.gradle as below:
`buildscript {
ext{
minSdkVersion = 23
targetSdkVersion = 29
kotlinVersion = "1.3.72"
}
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.0.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
classpath 'androidx.navigation:navigation-safe-args-gradle-plugin:2.3.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
maven { url 'https://www.jitpack.io' }
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
subprojects { prj ->
prj.afterEvaluate {
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)
}`
I run "gradle depsize" and get result: No dependencies found
What wrong with my use? Please help me...
@UltramanTIGA did you try running gradle depsize-all-configurations
? What's the output?
@medvedev Oh, awesome! It output the list with many configuration name, some of them show "No dependencies found", and some show the dependencies list.
So you mean I should change the configuration name as what I want?
@UltramanTIGA
Yep, you may for example change "depsize" task to use a configuration other than "default".
@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
@medvedev
3.6.0-beta04