Reducing apk size : A quick hack!
There are already many good articles on “How to reduce size of an apk”.
My focus in this article will be on “Reuse of resources”. Resources contribute a major chunk in the size of apk.
The techniques that I’ll be mentioning here only takes few lines of changes and will save a great deal of space.
# Use RotateDrawable resources
Many a times, resources are nothing but just a rotated version of some other resource. For e.g. collapse arrow and expand arrow
arrow_up.xml can be easily drawn using arrow_down.xml. Create a file named arrow_up.xml in res/drawable folder like this:
# Use setColorFilter for resources with different colour versions
In this example, three different versions of same icon are used. Instead of using three different icon sets, we can easily use setColorFilter to produce other two icons using the first one.
To achieve this, we can create a custom view that extends ImageView and apply color filter in its constructor by passing the desired color in its xml attributes.
- CustomColorIconView.java — Class that extends ImageView
- attrs.xml — styleable for color attribute
That’s it. Now to use this in your xml, create a CustomColorIconView and set dciv_color to whatever color you need. In this case, we changed the dark location icon to white.
That’s all for this post! Will keep posting interesting snippets about Android and other things. Follow me to get updates :)
Thanks for reading this article. Be sure to clap/recommend as much as you can and also share with your friends. It means a lot to me.