In this blog, we are going to learn, how to reduce the APK size in Android? So, before proceeding let’s discuss, what is the need to learn about this topic and after that we see what is an APK.
To download any application (APK file) from the play store, if that application is too large then obviously, you will not download that application. In today’s world, everyone needs an application which is smaller so it is important to reduce the APK size.
What is an APK?
An APK is nothing but a type of file(zip) that contain all the information for your application including your dex file, manifest file and all the compiled resource(res) file. To make it more clear what resides inside an APK file go through the below mentioned steps:
Content of APK
Step1: Open your project in the Android Studio and click on Build–>Build APK.
Step2: Now again Build->Analyze APK. After clicking on this you will get the below pop-up window:
Step3: After clicking on OK you will see in your Android Studio, an apk-debug.apk file is generated and in this file, you can see all the files that comprise your app. The screenshots below might help you understand better.
Note: Before building your APK file make sure you have added the release build or debug build in the build.gradle(module app) file without activating the instant run as shown in below screenshots. You can also check for the same in the above APK file. there is no instant run file in it.
Example of release build:
Example of instance run:
Reducing APK Size
How fast the application is downloading and how much memory it is using is completely dependent on the size of the APK. And we can reduce the APK size to increase the performance by removing the unnecessary files (by reducing the size of the resources which our app is using).
1. Reducing Resource Size and Their Count
➔ Remove Unused Resources
You need to remove all the resources from your files that are unreferenced. The LINT tool helps you out here and it will show the unused resources in the message like below:
Note: Lint Tool only displays you the unused resource details but do not remove them by their own. If you want that the Gradle will automatically remove the unused resources then you need to enable the shrink resources in your app’s build.gradle file as:
After enabling the shrink Resources the pro-guard-removes all the unused code but leaves the unused resources. Gradle then removes the unused resources
To know more about the proguard you can refer to the below blog: Proguard
➔ Use Only Specific Libraries
Include only that part of the libraries that your app needs because Proguard can clean some unnecessary code associated with the library, but it cannot remove the internal dependencies of that library.
➔ Make Use of Specific Densities
There are many Android devices supporting diverse screen densities including ldpi, mdpi, tvdpi, hdpi, xhdpi, xxhdpi and xxxhdpi. So, you must identify your targeted users and accordingly use only that densities.
See in the above image.
➔ Reuse the Resources
If you are adding variation in an image in your app including tinted, shaded and rotated versions then we recommend you to use only the same resources and do the necessary changes on runtime. See the below code for reference
➔ Compress PNG and JPEG Files
You can reduce the size of a PNG file by tools like pngcrush, pngquant, zopflipng. Among them pngcrush is effective.
For JPEG files, you can use packJPG that compress JPEG files into a more compact format.
Reduce Java and Native Code
Methods to reduce the Java codes are:-
1. Remove Unnecessary Generated Code
2. Remove Enumeration
3. Reduce the size of Native Binaries:
If your app is using native code and the Android NDK, you can also reduce APK size by optimizing the code. To do so there are two techniques which might be helpful:-
- Using debug symbols
- Not extracting Native Libraries