Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reduce APK file size

Tags:

android

apk

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 ?

like image 821
Amira Elsayed Ismail Avatar asked Dec 03 '22 12:12

Amira Elsayed Ismail


2 Answers

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
like image 108
Rahul Giradkar Avatar answered Dec 16 '22 05:12

Rahul Giradkar


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
like image 20
Mahdi Nouri Avatar answered Dec 16 '22 03:12

Mahdi Nouri