Reducing the size of Android .apk

After hundreds of hours of coding you are finally ready to ship your app to the Google play. You have created your .apk and started to upload it to the market and then... Your best friend Google smacks you in the face and tells you your .apk is too large? After some googling you discover that Google has limited the .apk size to 50MB. For a more detail description take a look at APK Expansion Files.

Even though 50mb is really not much for an application to weight these days, you unfortunately can't change Google's .apk policy, but you can try to shrink the size of your .apk file.

What if your current .apk is way above 50mb?

In those cases you probably can't optimize your .apk so much that it's in the magic 50mb area and you'll have to use APK Expension files. They work, but they are really tedious to implement.

What if your current .apk is a little over 50mb?

If so, you're lucky, because there is a chance that you can avoid using .apk extension files. Here we present several ways to reduce the size of your .apk, so lets start with the easiest one:

1. Remove any unused resource with LINT

If you are using Android Studio, which we strongly advise, use the built-in LINT code inspection. Under the main toolbar menu, select Analyze -> Inspect Code. After a while, you should see a screen similar to the screenshot below.

LINT in action

If you're using a lot of images, strings, layouts, assets etc., you will see a lot of similar entries, such as: „The resource R.drawable.test_products appears to be unused“. LINT will find any unused resources such as images, layout, raw files etc. Unfortunately, LINT can't automatically delete these files. You will have to do it manually or you can use Android Resource Remover which does this job for you. Caution: LINT is not perfect, as it sometimes detects resources as unused when they actually are used (in most cases in your code).

2. Optimize your images

In one of our projects, we had a ton of different images and other assets. The best practice in Android is it to use .png files, the main reason being the ability to use transparency. We got the best compression with the least amount of quality loss with PNGQuant. For multiple files, I recommend the desktop version and not the web version. In most cases, you can expect a size decrease of about 60%, which is a lot if you consider that we used more than a 100 different image assets.

3. Use Gradle and ProGuard

The last option is to optimize your resources using Gradle and ProGuard. To enable resource shrinking, update your build type as follows:

android{
    ...

    buildTypes{
        release{
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

For a more detailed description, take a look at resource shrinking.

Use the comments below and tell us if you have additional ways to reduce the size of your .apk files.