4

Someone asked me in interview how to reduce APK file size, and my answer was to manage the resources and also to manage the libraries that I am using and remove any unused libraries, but he told me that there are other ways to reduce APK file size

Can anyone tell me what are these ways ?

Amira Elsayed Ismail
  • 9,216
  • 30
  • 92
  • 175

6 Answers6

6

You can reduce APk size using following way.

  1. Remove unused resources
  2. Minimize resource use from libraries
  3. Support only specific densities
  4. Use drawable objects
  5. Reuse resources
  6. Render from code
  7. Crunch PNG files
  8. Compress PNG and JPEG files
  9. Use WebP file format
  10. Use vector graphics
  11. Use vector graphics for animated images
  12. Remove unnecessary generated code
  13. Reduce the size of native binaries
  14. Maintain multiple lean APKs
  15. Obfuscation of the code
Rahul Giradkar
  • 1,818
  • 1
  • 17
  • 28
5

Follow this steps to reduce your APK file size.

1st step :

make sure that you have enabled minify in your release buildType in build.gradle file :

buildTypes {

    release {
        minifyEnabled true
    }

}

by doing this you are also enabling proguard, so you need to add proguard rules for using libraries in your project. for example this are the proguard rules for retrofit2 :

-dontwarn okio.**
-dontwarn javax.annotation.**

you can add this two rules in your proguard-rules.pro file.

2nd step :

make sure that you have enabled resource shrinking in your release buildType in build.gradle file :

buildTypes {

    release {
        shrinkResources true
    }

}

so your final release scope in build.gradle file should look like this :

buildTypes {
    release {
        minifyEnabled true
        shrinkResources true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

3rd step :

some libraries that you may have used in your project have some additional resources for different countries and languages. for example a library could have two string.xml files, one for english language and one for japanese language. but you are only supporting english language in your application, so you don't need those japanese strings.

to do this open your build.gradle file and add this line :

resConfigs "en"

to your defaultConfig scope under android scope :

android {

    ...

    defaultConfig {

        ...
        resConfigs "en"

    }

}

4th step :

  • use vectorDrawables instead of png files
  • use webp images instead of png files
Mahdi Nouri
  • 1,391
  • 14
  • 29
3

Please follow the below steps,

1. Use .svg format icon set

Its time to say bye to PNGs and welcome vector drawables. There are a couple of benefits of using them. We don’t have to worry about different device DPI’s and it also helps in reducing apk size. With Support Library 23.2, we can now use the app:srcCompat property of ImageView instead of android:src to make it backward compatible. When you are downloading system app icon set from Google’s Material Design Icon Library, download .svg format instead of .pngs. This helped us reduce our app size by 1 Mb.

enter image description here

2. Compress PNGs

We are using PNGs for our walkthrough screens for all screen densities. The PNGs were of very high quality and size which just bloated up our app size! In Weather Stream v1.2 when we started optimizing our app size, we compressed our walkthrough screens and boom! The walkthrough screens were ~1/10th size it was before! (Yes! 1/10th). That’s crazy! We quickly created a build and checked the image quality in various screen densities, looked just the same. A quick Google search will give you a lot of tools that will help you compress your PNGs. We also got rid of the ldpi resources after seeing the Device Metrics shared by the Google Design team.

3. Use only specific libraries of Google Play Services.

Prior to Google Play Services 6.5 we had to compile the entire package. But now we can selectively compile it into our app. We are now just using Google Cloud Messaging, Google Maps and Google Location APIs.

4. Use Proguard

Proguard is used for code obfuscation and it also removes unused Java code from the dependencies. The result after using Proguard is a smaller apk file size which is difficult to reverse engineer. To enable proguard:

build.gradle

android {
    ...
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

5. Shrink Resources

The Android Gradle plugin supports the ability to automatically exclude unused resources during the build process using shrinkResources gradle property. This alone reduced the apk size by ~0.5 Mb. To take advantage of this in your release builds, just add shrinkResources true to your release configuration.

build.gradle

android {
    ...
    buildTypes {
        release {
            shrinkResources true
            minifyEnabled true
            ...
           //Other parameters
           debuggable false
           jniDebuggable false
           renderscriptDebuggable false
        }
    }
}

6. res Confing

“resConfigs” attribute will remove all the other localized resources while building the application. In our case, “Anti-Theft Screen Lock” only supports the English language. While all the support libraries may have localized folders for other languages. Which we don’t need. So, add following line to allow only English resources to be added in APK file.

defaultConfig {
    //...
    //...
    //...

    //strip other than english resources
    resConfigs "en"
}
Fenil Patel
  • 1,528
  • 11
  • 28
3

I only have these methods! I hope it can help you

  1. lint your code or res
  2. png image to webp
  3. minfyEnabled true
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Martin Yu
  • 271
  • 2
  • 3
  • 11
1

There are 3 more ways i can think of :

  1. Obfuscation of the code shrinks your apk size as well, by defining minifyEnabled true
  2. Downloading libraries at runtime - not recommended, and quite a hack, but works.
  3. Removing unused code by using lint : Analyze -> inspect code
Dus
  • 4,052
  • 5
  • 30
  • 49
0

Add this in your gradle file

buildTypes {
    release {
        minifyEnabled true
        shrinkResources true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
Aqua 4
  • 771
  • 2
  • 9
  • 26