How to Reduce APK Size in Android?
APK size is one of the most important factors while building any app for any organization or any business. No user would like to install a very large APK and consume his data on downloading that APK. The APK size will make an impact on your app performance about How fast it loads, How much memory it consumes, and How much memory it uses. It is very important to take a look at your APK size while developing. In this article, we will take a look over the tips to reduce your APK size in Android Studio.
1. Remove unused sources
The size of APK depends on very small factors rather it may be code, images, and assets used in your app. To reduce the size of your APK remove unused sources which will help to reduce APK size up to a certain extent. Removing unused sources of APK such as unused pngs, jpegs, and many other assets. Small size images are also preferable for reducing APK size. It is advised to use vector drawables instead of other image formats like JPEG, PNG, and others. Vector drawables are small in size and the main advantage of vector drawables is they don’t lose their quality even after increasing or decreasing size.
2. Use of Vector Drawables
Avoid using jpegs and pngs images because they consume very high memory in comparison with normal vector drawables. Vector drawables are easily scalable and their quality does not degrade in the change of size.
3. Reuse your code
Reuse your code as much as possible instead of repeating the code. Object-Oriented Programming will help you a lot to solve this problem and this will also help to maintain your size of APK. Repetitive code will increase the size of that particular file and it will lead to an increase in APK size.
4. Compress PNG and JPEG files
In most cases, images are the main purpose to degrade the performance in-app as well as on websites. So it is preferable to use compressed images to reduce its size and increase app performance. The size of the image will also affect the APK size so it is preferable to use compressed images in your app. You can use so many online platforms to compress your images for free.
5. Use of Lint
Lint is one of the important tools which will help us to get the unused and repeated code inside your application. So this tool will help in removing repeated and unused code.
6. Use images in WebP file format
WebP is another one of the famous image formats which are developed by Google. This image format generally focuses on image quality and optimization. Rather than using images in PNG and JPEG format WebP image format is highly preferable because of its quality.
7. Use of proguard
Proguard also plays an important role in adjusting the size of the Android APK. The main functions of using Proguard are given below:
- It makes the application difficult to reverse engineer.
- It helps to reduce the size of the application by removing the unused classes and methods.
Proguard in Android App can be found in Gradle Scripts > build.gradle file.
proguardFiles getDefaultProguardFile(‘proguard-android-optimize.txt’), ‘proguard-rules.pro’
8. Use of ShrinkResources
ShrinkResources method will be displayed in build.gradle file. This method will remove the resources which are not being used in the project. You have to enable it by specifying it to true. You can find this method in build.gradle file > buildTypes > release > shrinkResources. Enable it to true.
9. Use of R8 to reduce the size of APK
R8 works similar to that of proguard. R8 shrinking is the process in which we reduce the amount of code which helps to reduce APK size automatically. R8 works with proguard rules and shrinks code faster improvising the output size.
10. Limit the usage of external libraries
While adding many external features inside our app we prefer to use some external libraries. These external libraries will install the classes provided by them some of the classes are not required and of no use, they can consume storage and will lead to an increase in APK size. So it is preferable to limit the usage of external libraries to reduce APK size.
11. Use the Android Size Analyzer tool
In Android Studio there is a plugin called Android Size Analyzer this tool will help to find the amount of memory consumed by different files of our APK. Along with this Size, the Analyzer tool will also provide us some tips which will be helpful for reducing the size of our APK. To analyze your APK size you just have to click on the build > Analyze APK option and then select your APK. You will get to see the actual size of files with distribution along with downloadable size. With the help of this tool, you can also compare the size of your previous APK with the new one.
12. Generate App Bundles instead of APK
Android App Bundle is a publishing format which is provided by Google. It consists of your app’s code and resources which is different from APK generation and signing to Google Play. Google Play will handle your app’s bundle, it will generate optimized APK for a specific device according to device configuration. When you are using app bundles you don’t have to generate multiple APK files for different devices. To generate app bundles for your app you just have to click on Build>Build Bundle(s)/APK(s) and then click on Build Bundle(s). Your apps bundle will be generated.
13. Use of Multiple APK files
If you don’t want to create bundles for your application then you can opt for the option for creating multiple APK files. Multiple APK files can be used to support different device types and different CPU architectures.
14. Reduce library size
If you are using the libraries for adding some custom views inside your project, then you can add the official libraries which are provided by Google. Generally adding libraries makes it easier to add new functionality easily inside our app but at the same time, it also affects the size of our app. It is preferable to reduce the usage of libraries while building any application.
15. Use of Resconfigs
While building an Android application it is having some default resources present inside them. All of these support libraries which we are using in our device are having local folders for other languages which we actually don’t inside our app. These folders also take some memory location inside our app. We can remove these resources which are not required inside our APK using resConfigs and this will helps in reducing the app size by a certain amount. If you are building a specific app for targeting an only specific audience and this audience only uses a specific language like Hindi, then we will only keep resource for Hindi language and we will remove all other languages which are not required.
16. Remove libraries that are only required for debugging
Many developers used some specific libraries for debugging their APK files. These debugging libraries will also take some storage inside our APK file. We can use the method of debugImplementation() inside our app to use these debugging libraries only while debugging. So these libraries will be used only while debugging and will not be added inside your release APK files.