简体   繁体   中英

How to get dependencies size with gradle task in Android Studio?

I need to create a task that displays each dependency size when executed.

I've searched for a way to do it and the closest answer is this however, this solution seems to work for non Android projects only.

I'm currently using the tools.build:gradle:3.4.2 and the gradle plugin for android 5.1.1

I would like something to list each dependency and size like:

- Retrofit 2.5.0 ------ 654 KB.

- Butterknife 10.1.0 -- 150 KB.

I tried to implement the gist I linked before but it always says:

ERROR: Cannot change strategy of configuration ':app:androidApis' after it has been resolved.

After removing a piece of the code for multiple configurations:

ERROR: Cannot change strategy of configuration ':app:default' after it has been resolved.

At this point I don't know what to put at configuration...

You need to put the script mentioned here inside project.afterEvaluate{} , eg

project.afterEvaluate {
    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)
}

Then run below command you will get the size printed.

$ ./gradlew depsize

For example, my output is as following:

Configuration name: "minApi16ReleaseRuntimeClasspath"
Total dependencies size:                                               2.90 Mb

appcompat-v7-26.1.0.aar                                              980.85 kb
support-compat-26.1.0.aar                                            621.42 kb
recyclerview-v7-26.1.0.aar                                           335.80 kb
support-media-compat-26.1.0.aar                                      304.53 kb
support-core-ui-26.1.0.aar                                           227.63 kb
support-fragment-26.1.0.aar                                          160.75 kb
constraint-layout-solver-1.0.2.jar                                    93.32 kb
support-core-utils-26.1.0.aar                                         85.11 kb
constraint-layout-1.0.2.aar                                           37.28 kb
animated-vector-drawable-26.1.0.aar                                   34.33 kb
support-vector-drawable-26.1.0.aar                                    30.74 kb
support-annotations-26.1.0.jar                                        24.13 kb
common-1.0.0.jar                                                      12.08 kb
common-1.0.0.jar                                                      10.89 kb
runtime-1.0.0.aar                                                      9.38 kb
support-v4-26.1.0.aar                                                  3.01 kb

See: https://gist.github.com/medvedev/968119d7786966d9ed4442ae17aca279#gistcomment-3003945

I have launched a new website to just do this. I am hoping to build an index for all libraries. The first time the lookup is going to be slow but subsequent lookups will be faster. Please check out the website at droidanalyser.tools

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM