• How can you decrease application size by 60% (In only 5 minutes)?
  • Author: This article is authorized by Keval Patel
  • The Nuggets translation Project
  • Translator: jifaxu
  • Proofreader: Gaozp, ZiXYu

How did I reduce the size of my app by 60% in 5 minutes

Mobile devices have limited resources. Limited battery, limited storage, limited processing power, limited memory, limited network bandwidth… This is true whether you’re dealing with Android or iOS.

In the last few months, I’ve been working on an Android app called Anti-Theft Screen Lock. When someone tries to unlock the device with the wrong password, the app takes a picture with the front-facing camera and plays an alert sound. If you’d like to learn more, here’s the Play Store page:

Anti-Theft Screen Lock – Android Apps on Google Play

Here I’ll teach you some tricks I use to reduce the size of my apps. These tips are simple and easy to use and will help you now or in the future.


the smaller the better

As developers we are always more concerned with the performance, design and user experience of our applications. However, there is one thing most developers forget (or underestimate) : app size. If you want your app to appeal to a large audience, this is very central.

There are about 11,000 Android models on the market, and most of them are low-end, have limited storage (1GB to 8GB), and even run on 2G or 3G networks. There are a lot of markets for these devices in Developing countries in Africa like India and Pakistan, where you can get a lot of users.

It’s especially important to keep your app size optimal. The smaller your app, the more space your users have to store their videos and photos. To be honest, you don’t want users deleting your app because of a “low storage space” message.

If users run out of storage space, they will uninstall your application. These users in developing countries still use limited 2G/3G networks. So, if your app is too big, it will take longer to download (and more likely, users won’t download at all). Similarly, most users have limited data, and every byte they download costs money.

So, the obvious truth in the application world is:

the smaller the better


Use APK Analyser to break down your APK

Android Studio provides a useful tool: APK Analyser. APK Analyser will break down your application and let you know which part of the.apk file takes up a lot of space. Let’s take a look at a screenshot of Anti-Theft before it’s optimized.

From the output of Apk Analyser, the original size of the application was 3.1MB. Compressed by the Play store, it’s roughly 2.5MB.

As you can see from the screenshot, there are three main folders that take up most of the application space.

  • Classes.dex — This is the dex file that contains all the bytecode files that will run in your DVM or ART.
  • Res – This folder contains all files in the RES folder. In most cases it contains all images, ICONS and source files, menu files and layout.

  • Resources.arsc – This file contains all value resources. This file contains all the data in your value directory. This includes strings, dimensions, styles, Intergers, IDS, and so on.


So, now you know how APK is made up. Let’s see how we can optimize it piece by piece to reduce the size of the application.

Reduce classes. Dex

Classes.dex contains all the Java code. When you compile your application, Gradle will convert the.class files in all your modules into.dex files and combine them into a classes.dex file.

If you’re curious about how The compilation process works, check out my other blog post: The Jack and Jill: Should You Use in Your Next Android Application?

A single classes.dex file can hold about 64K methods. If you reach this limit, you must enable multidexing in your project. This will create another classes1.dex file to store the rest of the methods. So the number of classes.dex files depends on the number of methods you have.

You can see that the current “Anti-Theft Screen Lock” contains 4,392 classes and 29,897 methods. There is no confusion about the result. You have two default obfuscation files.

  • proguard-android-optimize.txt
  • proguard-android.txt

As the file name says, “proguard-Android-optimize.txt” is the more aggressive obfuscation option. We will use this as the default obfuscation configuration. You can add custom obturation in proGuard-rules. pro under /app.

 release {
    //Enable the proguard
    minifyEnabled true
    proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), "proguard-rules.pro"

    //Other parameters
    debuggable false
    jniDebuggable false
    renderscriptDebuggable false
    signingConfig playStoreConfig //Add your own signing config
    pseudoLocalesEnabled false
    zipAlignEnabled true
}Copy the code

By setting minifyEnabled to true, obfuscation will remove all unused methods and directives to reduce the classes.dex file.

This is APK with Minify enabled.

You can see that our classes.dex size decreased by almost 50% after obliquity was enabled for each module. At the same time you can see that the number of methods dropped from 29,897 to 15,168 (almost 50%). Congratulations on… 🎊 🎉

The size was reduced from 3.1MB to 1.98MB. (Reduced by about 50%)


Reduce the res:

The next big chunk is the RES folder, which contains all the images, raw files, and XML. You cannot add/delete/modify your XML because they contain your layout. But we can make the image file smaller.

  • The “shrinkResources” property will remove all resources that are not used in the project. Enable it in build.gradle as follows:
release{
  / /...
  / /...
  shrinkResources true
  / /...
}Copy the code
  • The “resConfigs” property will remove all localized resources during build. The app “Anti-Theft Screen Lock” only needs to support English. Many support libraries may have localized folders in other languages. These are the ones I don’t need. So, add the following code to make the app English-only.
defaultConfig {
    / /...
    / /...
    / /...

    //strip other than english resources
    resConfigs "en"
}Copy the code
  • If you are using Android Studio 2.3 and your app has a minimum supported version of 18, you can use Webp instead of PNG. Webp images are smaller than PNG images but of the same quality. And Android supports WebP. So you can load webp images in ImageView just like any other raster image. There is no need to change your layout.

You can select the Drawable and Mipmap folders in the project, right click and select Convert to Webp. This will open a configuration popup like the one below.

Clicking OK will convert all PNG images to WebP. If the Webp image is larger than PNG, Android Studio will automatically skip the file.

Let’s take a look at the end result:

Oh!!! The RES folder was reduced from 710KB to 597KB.

Our volume has decreased by 105KB. (Down 16 percent)

You can also convert images into vector images. But then you need to do something about its backward compatibility. If you want to learn more about Vector, check out this blog by Chris Banes.


TL; DR:

  • Enable obfuscation by adding the following code to your release Build type.
  • Enable shrinkResources.
  • Remove all unneeded localized resources by adding required resource names to “resConfigs”.
  • Convert all images to WebP or vector images.

Conclusion:

I reduced the size of my app from 3.19MB to 1.89MB using these simple techniques.

These are just the simplest ways, but there are many ways to reduce the size of your application. However, you should always use these simple methods to ensure that you have reduced the size of your application as much as possible.

You can learn more skills here.

Remember: Smaller is better. 😉


The Nuggets Translation Project is a community that translates quality Internet technical articles from English sharing articles on nuggets. Android, iOS, React, front end, back end, product, design, etc. Keep an eye on the Nuggets Translation project for more quality translations.