Chandru
Chandru

Reputation: 5962

How to calculate the size of libraries that is added as a dependency in the Android project

I am working in a project where I am using multiple libaries like google play service, retrofit, gson, glide, twitter and facebook sdk. So what I want to know the exact size of each library occupies in my application. Kindly please help me whether is there any possible ways to analyse the size in Android studio. Any tool suggestions or tips for my requirement would be very helpful to me. I am posting the dependencies that I am using in my build.gradle as follows.

compile('com.twitter.sdk.android:twitter:1.9.0@aar') {
        transitive = true;
    }
compile 'com.android.support:appcompat-v7:23.2.1'
compile 'org.twitter4j:twitter4j-core:4.0.2'
compile 'com.github.bumptech.glide:glide:3.6.1'
compile 'com.google.android.gms:play-services-gcm:8.4.0'
compile 'com.android.support:support-v4:23.2.0'

Note: I am analysing this report in order to reduce the apk size of my application by removing the libraries which occupies much memory.

I am completely stuck with this solution for the past couple of days. I even searched a lot and I couldn't find the optimized approach to calculate the exact size usages of libraries in my project.

Please help. Thanks in advance.

Upvotes: 9

Views: 8260

Answers (2)

Slavkó Medvediev
Slavkó Medvediev

Reputation: 1591

Not sure it it's something you're looking for, but this might help:

task depsize  {
    doLast {
        final formatStr = "%,10.2f"
        final conf = configurations.default
        final size = conf.collect { it.length() / (1024 * 1024) }.sum()
        final out = new StringBuffer()
        out << 'Total dependencies size:'.padRight(45)
        out << "${String.format(formatStr, size)} Mb\n\n"
        conf.sort { -it.length() }
            .each {
                out << "${it.name}".padRight(45)
                out << "${String.format(formatStr, (it.length() / 1024))} kb\n"
            }
        println(out)
    }
}

The task prints out sum of all dependencies and prints them out with size in kb, sorted by size desc.

Updated version, compatible with Gradle 5+

tasks.register("depsize") {
    def formatStr = "%,10.2f"
    def size = configurations.default.collect { it.length() / (1024 * 1024) }.sum()

    def out = new StringBuffer()
    out << 'Total dependencies size:'.padRight(45)
    out << "${String.format(formatStr, size)} Mb\n\n"

    configurations
            .default
            .sort { -it.length() }
            .each {
                out << "${it.name}".padRight(45)
                out << "${String.format(formatStr, (it.length() / 1024))} kb\n"
            }
    println(out)
}

Update #2: Latest version of task code can be found on github gist

Upvotes: 15

David
David

Reputation: 1520

task (depsize) << {
def size = 0;
configurations._debugApk.collect { it.length() / (1024 * 1024) }.each { size += it }
println "Total dependencies size: ${Math.round(size * 100) / 100} Mb"

configurations
        ._debugApk
        .sort { -it.length() }
        .each { println "${it.name} : ${Math.round(it.length() / (1024) * 100) / 100} kb" }
}

Try put this in your app module gradle.build and then you could run in directly by gradle. If you want to see all posissible configuratuions add:

configurations
        .findAll()
        .each { println "${it.name}" }

Upvotes: 3

Related Questions