Read More

Android: Shrinking the apk file

Code shrinking is available with minify property or you can say as pro-guard property in android. Android gives you a tool to shrink your code by removing extra class, resources, methods etc. For enabling shrinking you have to enable minify. Pro-guard detects and removes the unused code from your apk that is all the extra libraries, classes, attributes that you for got to remove or that are not required. It obfuscates the your code hence making it hard to reverse engineer and thats a plus point of minify.

minify

For enabling minify you have to update your app level gradle file with this code.

or you can do it by going to module setting then build settings.

Screenshot_1

But enabling pro-guard makes the build process slow, so try to use it only with release variant.

The above code takes a pro-guard files. It defines the pro-guard rules, it by default contains some rules to minify but you can add your own rules as well. It is by default located at the root folder.

Enabling minify can sometimes remove the used files so you should atleast once test your application with enabling minify so that you can catch such errors. To keep a class or a method that is removed by pro-guard you can use @Keep  annotation with that class or method or you can define it in your pro-guard rule like

and it the pro-guard will not eliminate this file. You can also add different pro-guard rules for different flavors, just mention your pro-guard file in product flavor like

and create flavourB-rules.pro file in root folder.

Shrinking resources

Resource shrinking only works when minifyEnabled is true. So for resource shrinking you have to add the following code to app’s build.gradle file

By enabling shrinkResources you can make your apk file even more small. It removes all the extra resources that are never called in you app. The resources you forgot to remove, the extra resources that comes with the libraries you added. So all these resources ate unused and just increase the size of your apk.

Alternative Resources used: There are some resouces that are alternative to each other but are never used the android shrink doesn’t remove them. Such files are the alternative layouts for different screen sizes, or the strings of different languages. These files are not removed by shrink property. But if you still want to remove these files you can do this by gradle plugin resConfigs like

In resConfigs you have to mention only the language files you want your app to support. It you want to do this with layout files for different sizes use APK splits. You can get the further information over here.

. . .

Comment

Add Your Comment

Be the first to comment.

css.php
Hire Us!
Brief us about your requirements and we'll get back to you.
Woo! Hooy!
We have just recieved your project brief and our expert will contact you shortly.
Send Again
Close