How to Reduce APK Size?

Imagine if you are trying to install a new app and receive a message that says 'Insufficient Storage Available'. What will you do?

You'll first try to figure out the least required app on your smartphone and remove it. If that doesn't free up space you'll move to the biggest app in your phone and so on. Now you don't want your app to be on that list when a user is trying to free up his smartphone's space.

Some would argue that the average storage capacity of smartphones has increased recently, but in developing countries like India, Brazil, etc. most of the smartphones have low storage space. Also if you are developing for a global audience then a large portion of the user base won't be able to afford a high-end device.

Along with user retention, a larger apk size also limits your user acquisition due to limited availability of 2G/3G network in developing countries.

How to reduce apk size?
1. Use Progaurd

Progaurd helps in minifying java source code, removing unused code and in obfuscating it. If you are using any third party libraries then make sure you add respective progaurd rules to your proguard-rules.pro. To enable progaurd add the following lines to the app level gradle file in the release section:

minifyEnabled true
Progaurd is great but it doesn't understand runtime changes. E.g. If you are using Retrofit then you need to tell progaurd to not process the model files, these files are used by Retrofit for de-serializing the API response.

2. Shrink Resources

Resource Shrinking: attribute will remove all the resources, those are not used anywhere in the project.
Add it to your app level gradle file as follows:

shrinkResources true

Resource shrinking is used to remove unused resources. For example, if you added libraries including their resources, then shrinking the code will remove all unused library code. It will also remove unreferenced resources.

3. Resource configs

“resConfigs” attribute will remove all the other localized resources while building the application.

//strip resources other than english resources
resConfigs "en"

This is useful in cases where your app supports only English(or any other) language but other libraries like google play services may contains more stuff related to many languages that your app doesn't needs.

4. Use vector images instead of PNG

Using a single vector image instead of multiple png's for supporting devices of various densities is a very useful technique to reduce apk size. Using vector images has become more convenient since the release of support library 23.2. Refer to my previous post to know how to support vector images on pre-lollipop devices.
If in case you need to use png instead of SVG then compress it with tools like OptiPng OR PngCrush to reduce their size without losing quality.

5. Carefully selecting dependencies

I have seen many developers who add a library to their project and use only a specific part of it. While progaurd will do its best to remove any unused code, but still adding a bigger library will increase your build time.

Another way to reduce apk size is using individual dependencies of libraries like google play services instead of adding the whole library to the project. For example, if you want to use only maps and GCM in your project you should include them individually as follows:

compile 'com.google.android.gms:play-services-gcm:8.3.0'
compile 'com.google.android.gms:play-services-maps:8.3.0'

Instead of adding the whole com.google.android.gms:play-services library which includes everything else like auth, fitness etc.

6. Remove Log Messages from production

"Every time you log in production, a puppy dies." - Jake Wharton

Logging in production mode increases apk size and also degrade the performance of the app. The easiest and effective way to do this is using the timber library (https://github.com/JakeWharton/timber) and planting the Tree only in debug as follows:

if (BuildConfig.DEBUG)
    Timber.plant(new Timber.DebugTree());

7. Build multiple APKs

The Apk Split mechanism allows us to create multiple apks for various densities of devices. This helps us in building multiple lean apks having smaller sizes. For more details refer to Link(http://tools.android.com/tech-docs/new-build-system/user-guide/apk-splits)

Hope you find these tips useful. If you have any queries, feel free to connect with me at https://twitter.com/askShantanu

Thumbnail Image Credits: CS-Unplugged