Quick note: App size is also an important aspect of the user experience, and reducing the size of an Android app package is not complicated at all.

For mobile applications, the smaller the package, the better. Especially in less developed regions, you don’t want users to uninstall your app because of “lack of storage space” on their phone.

Here are a few tips to reduce apK size effectively:

First, let’s use the APK Analyser tool provided by Android Studio to analyze our APK file:

apk


  • Classes.dex: a bytecode file containing Java code.
  • Res: Contains resource files, such as images, layout files, etc.
  • Resources. arsc: contains all the value resource files, such as strings, dimensions, styles, integers, and so on.

classes.dex

The size of the classes.dex file depends entirely on the number of methods in the program. In the example shown above, there are 4,392 classes and 29,897 methods. The result is proGuard free by default. We have two default ProGuard files to use:

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

TXT is a more radical version of proGuard, including the same proGuard rule as proGuard-android.txt. But there are other optimizations that perform analysis at the bytecode level (within and between methods) to further reduce the SIZE of APK and help speed it up.

It can be used like this:

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

Setting the minifyEnabled property to true tells ProGuard to remove all unused methods and directives, further reducing the size of the.dex file.

.dex


res

For the RES folder, images usually take up the most space. If your Android Studio is 2.3 and your project has a minimum version 18 or above, you should use WebP instead of PNG images. Webp images are smaller in size and there is no loss of image quality.

To convert the image to WebP format, right-click the Drawable and Mipmap folders and select Convert to WebP.

webp





res

We can also set shrinkResources to true in build.gradle, which removes unused resources at packaging time:

release{ //... / /... shrinkResourcestrue/ /... }Copy the code

In addition, if your application does not need to support internationalization, you can set resConfigs to “en”,”en”.

defaultConfig { //... / /... / /... resConfigs"zh"."en"
}Copy the code

The reason for this is that the official Support Library is internationalized by default, which means it contains resource files in many different languages, so you can use this setting to remove unwanted language resource files.

Through the above Settings, the size of the App installation package was reduced from 3.19MB to 1.89MB. There are plenty of other tips for reducing the size of APK, but the ones described here should be used as the default for your project.


Zhihu column: Aurora Daily

How can you decrease application size by 60% (In only 5 minutes)?

Aurora Daily, aurora developer’s Side Project, reads three foreign technical articles every day, welcome to follow.