9

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.

3
  • 2
    Well, once they are in your app, all of the libraries get merged in with your APK, and therefore it is difficult to split them apart. You might look at the size of the JAR or AAR files, plus those from transitive dependencies, to get a rough estimate. For example, Glide appears to be ~465KB, while support-v4:23.2.1 is ~1.2MB (plus another ~19KB for its support-annotations dependency). Apr 27, 2016 at 17:58
  • You could add a dependency one at a time to a completely bare Android project, generate an APK, and calculate the difference for each. Apr 27, 2016 at 19:01
  • @fractalwrench Thats fine. But again it would be a tedious process, you have to build the project again and again.
    – Chandru
    Apr 28, 2016 at 9:35

2 Answers 2

14

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

5
  • 1
    Hi @medvedev. Thanks for you answer. I was exactly looking for the same. I inserted this piece of code in the build.gradle(app), but it is not printing anything in the log. Also tried copying these in build.gradle(project).
    – Chandru
    Jul 9, 2016 at 7:03
  • 1
    That's strange. I've copied this piece of code from my build.gradle. To get the output, I execute "gradle depsize" Jul 9, 2016 at 14:38
  • However, it worked for my non-android build. Didn't try it with android. Sorry for that. Jul 9, 2016 at 14:39
  • @MedvedevV. - I also tried this but just getting "0 Mb". Like the user above it's in the app's build.gradle at the end.. any ideas? :app:depsize Total dependencies size: 0 Mb
    – jpage4500
    Feb 20, 2017 at 21:00
  • I guess you have to change "default" in the code to something else. fx, to "_debugApk", as mentioned in answer above. Cannot tell for sure since I'm not android guy, just gradle fan. Feb 21, 2017 at 6:19
3
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}" }
2
  • Thanks for the reply. Can you please let me know where to add this. Once if it is done I will accept the answer, so that it would be very helpful to others who are all searching for this requirement.
    – Chandru
    Aug 23, 2016 at 10:48
  • I have updated my answer ... you can put it at the end of your app module gradle.build
    – David
    Aug 23, 2016 at 12:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.