Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 327
Chandru Avatar asked Apr 27 '16 17:04

Chandru


People also ask

What are library dependencies?

By definition, every shared library system provides a way for executables to depend on libraries, so that symbol resolution is deferred until runtime. An inter-library dependency is where a library depends on other libraries.

How do you add a dependency to a library?

To add a dependency to your project, specify a dependency configuration such as implementation in the dependencies block of your module's build. gradle file. This declares a dependency on an Android library module named "mylibrary" (this name must match the library name defined with an include: in your settings.

What are dependencies in Android?

In Android Studio, dependencies allows us to include external library or local jar files or other library modules in our Android project. For example: Suppose I want to show some images in ImageView. But I'm using Glide Library to enhance the smoothness of application.

Where are the dependencies in Android Studio?

Go to File > Project structure in Android Studio. Select the app module in the Modules list on the left. Select the Dependencies tab.


1 Answers

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

like image 158
Slava Medvediev Avatar answered Jan 04 '23 19:01

Slava Medvediev