Skip to content

Instantly share code, notes, and snippets.

@BruceWind
Forked from medvedev/build.gradle
Last active December 22, 2020 09:56
Show Gist options
  • Save BruceWind/0bf231688c77fafef0b7e4e42f940084 to your computer and use it in GitHub Desktop.
Save BruceWind/0bf231688c77fafef0b7e4e42f940084 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
/* Tested with Gradle gradle-6.6.1-all.zip and gradle plugin 4.0.2 */


allprojects {
    repositories {
        afterEvaluate {
    
        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 configurationWhichSaveToMarkdown = "_classStructurekaptDebugKotlin"
    def formatStr = "%,10.2f"

    def size = configuration.collect { it.length() / (1024 * 1024) }.sum()

    def Date now = new Date()

    def out = new StringBuffer()
    out << "\n  Date :${now.getDateString()}  \n   Configuration name: \"${configuration.name}\"\n"
    if (size) {
        out << '\nTotal dependencies size:'.padRight(65)
        out << "${String.format(formatStr, size)} Mb\n\n"

        out << '| file   |      size      |\n'
        out << '|----------|------:|\n'
        configuration.sort { -it.length() }
                .each {
                    String name = it.name
                    if(name == "classes.jar"){//this name is not clear, I have to print its parent dir name.
                        File file = it.getAbsoluteFile()
                        while (file.parentFile!=null){
                            if (file.name=="build"){
                                file = file.parentFile
                                name = file.getName() +"/build/.../" + name
                                break
                            }
                            file = file.parentFile
                        }
                    }
                    out << "| ${name} ".padRight(65)
                    out << "| ${String.format(formatStr, (it.length() / 1024))} kb |\n"
                }
    } else {
        out << 'No dependencies found';
    }
    println(out)
    if (configuration.name == configurationWhichSaveToMarkdown) {
        new File(projectDir, "all_libs.md").text = out
    }
}



@BruceWind
Copy link
Author

BruceWind commented Oct 13, 2020

How to use?

  1. copy that code into project/build.gralde ;
  2. run ./gradlew :app:depsize-all-configurations on terminal;
  3. then you will have got a new file all_libs.md in you main module directory. Pls open it. Other things you will have seen on terminal.

If you get a error as you run it, pls set gradle plugin version to 4.0.2.

@BruceWind
Copy link
Author

BruceWind commented Oct 15, 2020

I modified the original author's code, and some information will be saved in all_libs.md as markdown form for analyzing easily.
Hope you enjoy it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment