I am making a book app with pdf files in the assets folder. All files are 10-20mb after compressing. How can I keep the size of the app small? It's 500+mb currently.
Android studio provides a handful tool: APK Analyser APK Analyser will tear down your application and let you know which component in your .apk file is taking the larger amount of space? Let's have a look in Anti-Theft screen lock apk file without any optimization applied.
From the apk analyser output, you can see that the application raw size is about 3.1MB. After applying play store compressions, the application size is roughly 2.5 MB. As you can see from the screenshot, there are main 3 folders that are consuming most of the application size.
So, now you know what an APK is made of. Let's see, how can we decrease the application size by optimising one by one component.
As we discussed, this contains all the java code. While you build your application, gradle will combine all of your .class files from all the modules and convert those .class files to .dex files and combine them in single classes.dex file. Single classes.dex file can accommodate, approximately 64K methods. If you exceed this limit, you have to enable multidexing in your project. Which will create another classes1.dex file to include all remaining methods. Thus the number of classes.dex file depends on the number of methods you have.
As you can see currently “Anti-Theft Screen Lock” contains 4392 classes and 29897 methods. This result is without applying proguard. You have two default proguard file you can apply.
- proguard-android-optimize.txt
- proguard-android.txt
As the name suggests “proguard-android-optimize.txt” is the more aggressive progurard configuration. We used this as the default proguard configuration. You can add you custom proguard configurations in proguard-rules.pro file in your /app directory .
By setting minifyEnabled to true (In the Compression Gist 1), you are telling proguard to remove all the unused methods, instructions and slim down the classes.dex file.
Here is the result from minify enabled APK,
As you can see by enabling the proguard in every module of our project we can we are able to reduce the classes.dex file size almost by 50%. Also, you can see method count decreased from 29897 to 15168 (almost 50%).
Size decreased from 3.1 MB to 1.98MB. (~50% decrease)
The second largest component in your apk size is your res folder, which contains all the images, raw files, and XML. You cannot add/remove/modify your XML, as they are containing your layouts. But we can decrease the image files.
You can select drawable and mipmap folders from you project, right click and select convert to webp . This will open below configuration dialog.
Press ok and it will convert all the png images to webp format one-by-one. If the webp image is having the larger size than png, Android Studio will automatically skip that file.
Let's see the final result:
By applying the above simple tricks the application size decreases from 3.19 MB to 1.89 MB.
Happy Coding 😊
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.