Ankur Khandelwal
Ankur Khandelwal

Reputation: 1070

How to reduce Apk size when 80% size is due to the third party library?

Currently my app apk size is 22 MB. How will I reduce the app size?

I have compress the size of all the images. I also use the proguard file.

I also use the analyzer apk option in the android studio which shows approx 80% size is due to third party library. I checked through the method count plugin and find that method count is also less.

Than i use the dexcount plugin to check which part of my apk is taking how much method count. Thats the finding:

This is the file of total method count for my apk:

Total method count: 40909

android.support: 22652 com.google.android: 5428

These are the two method count library. Is there way I can reduce the method count. I think reducing the method count will leads to the reducing apk size.

Progaurd File:

#keep json classes                
 -keepclassmembernames class * extends com.applozic.mobicommons.json.JsonMarker {
    !static !transient <fields>;
 }

 -keepclassmembernames class * extends com.applozic.mobicommons.json.JsonParcelableMarker {
    !static !transient <fields>;
 }
 #GSON Config          
-keepattributes Signature
-keep class sun.misc.Unsafe { *; }           
-keep class com.google.gson.examples.android.model.** { *; }            
-keep class org.eclipse.paho.client.mqttv3.logging.JSR47Logger { *; } 
-keep class android.support.** { *; }
-keep interface android.support.** { *; }

-dontwarn sun.misc.Unsafe.**
-dontwarn com.google.gson.examples.android.model.**
-dontwarn org.eclipse.paho.client.mqttv3.logging.JSR47Logger.**
-dontwarn android.support.**
-dontwarn com.google.android.gms.**          
-dontwarn com.roughike.bottombar.**


-keep class com.sun.pdfview.**{ *; } 
-dontwarn com.sun.pdfview.** 

-keep class test.**{ *; }
-dontwarn test.**

app gradle file

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.0"
    useLibrary 'org.apache.http.legacy'

    defaultConfig {
        applicationId "user.com.hlthee"
        minSdkVersion 17
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            debuggable false
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
            debuggable true
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    sourceSets {
        main {
            assets.srcDirs = ['src/main/assets', 'src/main/assets/']
            res.srcDirs = ['src/main/res', 'src/main/assets/fonts']
        }
    }

    dexOptions {
        jumboMode true
    }

    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/ECLIPSE_.SF'
        exclude 'META-INF/ECLIPSE_.RSA'
    }
}


dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    //for bottom bar
    compile 'com.roughike:bottom-bar:2.0.2'
    compile 'com.android.support:cardview-v7:25.1.0'
    compile 'com.android.support:recyclerview-v7:25.1.0'
    compile 'com.android.support:appcompat-v7:25.1.0'
    compile 'com.android.support:design:25.1.0'
    //for volley
    compile 'com.android.volley:volley:1.0.0'
    //graph library
    compile 'com.github.lecho:hellocharts-android:v1.5.8'
    //for file implementation
    compile files('libs/commons-io-2.5.jar')
    //To show the thumbnail of pdf
    compile 'com.github.barteksc:pdfium-android:1.4.0'
    //for applozic chat api
    compile project(':mobicomkitui')
    //for junit
    //universal image loader to load the image.
    compile 'com.github.bumptech.glide:glide:3.7.0'
    //for circlular image
    compile 'de.hdodenhof:circleimageview:1.3.0'
    //for adherence progress circular bar
    compile 'com.github.jakob-grabner:Circle-Progress-View:v1.2.9.1'
    //for the sectioned recycler view
    compile 'com.github.IntruderShanky:Sectioned-RecyclerView:2.1.1'
    //for the multipart things encounter while dealing with image send to server
    compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5.1'
        compile('org.apache.httpcomponents:httpmime:4.3') {
            exclude module: "httpclient"

         }



}

apply plugin: 'com.google.gms.google-services'
apply plugin: 'com.getkeepsafe.dexcount'

Upvotes: 4

Views: 4412

Answers (4)

Shriki
Shriki

Reputation: 119

Use

proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'

Instead of

proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

Upvotes: 3

varun bhardwaj
varun bhardwaj

Reputation: 1522

Write this resConfigs "en" in your build.gradle and add the type of language resources your app support and it will exclude other language resources from all third parties your app is having.. My application supports only English so, I added "en". It reduced my apk size by 800kb and my total app size came down to 4.2mb from 5mb.

Upvotes: 0

Avinash Verma
Avinash Verma

Reputation: 2762

Size of com.github.barteksc:pdfium-android:1.4.0 library is 16 MB so ur app size is going this much. Reduce Methods Counts and Dex Size.

Check the Library size at methodscount.com

http://www.methodscount.com/?lib=com.github.barteksc%3Apdfium-android%3A1.4.0

Upvotes: 0

Michael Dodd
Michael Dodd

Reputation: 10280

You're right that reducing the number of third party libraries you use will reduce the dex count, but unfortunately this won't be a quick fix. This will involve looking through your app and trying to figure out which libraries you might not need, or can roll your own solution for. For example (though this doesn't apply to you), Google's Guava library contains a lot of handy utility functions, but is a very heavy library as it's so broad in scope.

Have a look at the libraries you're using in more detail, and see if there's stripped-down versions available containing only what you need. Also take a look at what your mobicomkitui project is using, as all the dependencies of that library project will also be compiled into your app.

Upvotes: 1

Related Questions