Tips for building app Smaller

The size of your APK has an impact on how fast your app loads, how much memory it uses, and how much power it consumes. I think most of you know APK size is an important factor in your user engagement. Let’s see current app size in LINE MAN Driver

Current app size is 9MB

Quick recommendation to optimize your APK is to use APK Analyzer in Android Studio. You can open Android Studio and select “Profile or debug APK” then browse your APK file. This tool will breakdown your packages in your APK file e.g. layouts, images, classes, libraries and etc. So you’ll know which part is big or which part is small then you can prioritize which one you should optimize.

APK Analyzer

Tip 1: Remove unused resources

Most of you are developing in legacy projects and there are a lot of images, layouts, and strings that you never used but you don’t know and you don’t want to remove it yourself because you are afraid you will make your app crash right? So In Android Studio, it provides you a menu called Remove Unused Resources.

It is very helpful for us because it’ll find unused resources automatically then you can remove them in one click.

Tip 2: Add just Dependencies Needed

Some dependencies they include a bunch of library inside like play-services and Facebook SDK. If you don’t specify libraries you need you will get entire of them that make your app fat. For example, if you want to use Google Auth you should specify “com.google.android.gms:play-services-auth:16.x.x” instead of “com.google.android.gms:play-services:16.x.x”

You can break down dependencies in your project by a command line below

$ ./gradlew app:dependencies

You will see all of dependencies you use in your project then make sure you only use dependencies you need.

Tip 3: Build Multiple APKs for Screen Densities

By default Android Studio will generate a universal APK that include all of screen densities inside. In this tip you can specifically exclude or include screen densities you want to support in app/build.gradle and Android Studio will generate multiple APKs for you.

android {

splits {

density {

enable true // Specify a list of screen densities which Gradle won't create multiple APKs for

exclude 'ldpi', 'mdpi' // Specify a list of compatible screen size for the manifest

compatibleScreens 'small', 'normal', 'large', 'xlarge'

}

}

}

So you need to upload all of them to Google Play and finally your users will download only APK which match with their screen densities.

Tip 4: Build Multiple APKs for ABIs

This tip is similar to the previous tip but this tip is for supporting Application Binary Interfaces(ABIs). Now a day I think there are 7 CPU architectures in Android market and 3 of them are hard to find(mips, mips64, armeabi) so you can specify ABIs you want to support in app/build.gradle and Android Studio will generate multiple APKs for you.

android {

splits {

abi {

enable true reset() // Specify a list of ABIs that Gradle should create APKs for

include 'x86', 'x86_64', 'arm64-v8a', 'armeabi-v7a' // If you don’t want to generate a universal APK that includes all ABIs.

universalApk false

}

}

}

After that you need to upload all of them to Google Play and finally your users will download only APK which match with their CPU architectures.

Tip 5: Build an APK with Specific ABIs

This tip is different from multiple APKs. You can specify CPU architecture you want to support and Android Studio will generate only one APK.

android {

defaultConfig {

...

ndk {

abiFilters 'x86', 'x86_64', 'arm64-v8a', 'armeabi-v7a'

// armeabi, mips and mips64 has removed since NDK r17

}

}

}

In my experience I prefer this tip than build multiple APKs because I used to find some crashes in some devices from multiple APKs.

Tip 6: Remove Unused Alternative Resources

Sometimes you create a local app and you only want to target some specific languages. But there are some dependencies that include many languages around the world and you don’t need all of them. So you can use “resConfigs” property to specify languages you want to support and your app size will be smaller.

android {

defaultConfig {

resConfigs 'en', 'th'

...

}

}

Tip 7: Shrink Unused Code and Resources

By default minifyEnabled in Android Studio is false but I think many of you set it to “true” to minify and obfuscate your code for smaller and security reasons. I recommended you to add “shrinkResources” in app/build.gradle to remove unused resources after minify process. Because in minifying, gradle will remove unused code and that code might refer to some resources.

Lastly I think most of you may not know “-optimize” So you can add “-optimize” after “proguard-android” it will do more optimization for your build and your app would be smaller in significant number.

android {

buildTypes {

release {

minifyEnabled true



shrinkResources true proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'

}

}

}

Please be reminded this process will take more time so you should use “-optimize” in release block only.

Tip 8: Use Shape Drawable

Sometimes I see developers use gradient background or rounded corner image as a Bitmap image. Actually bitmap image is bigger than Shape Drawable that provided by Android Studio because in Shape Drawable you can draw rectangle oval, rounded corner and other in XML format.

Tip 9: Use WebP

WebP is smaller up to 30% with the same quality. The only thing you need to be careful is the OS requirements. If you are using opaque background, WebPs API level 15 and above will support them. But if you want to support transparency background in your WebP then you need to support API level 18 or above and Android Studio also provide you an easy way to convert your image to WebP format. You can do a right click on an image you want and select “Convert to WebP” then you will see the screen like this example.

So let’s see an example result the image size was reduced to 15% of the original size with 90% quality.

Tip 10: Use VectorDrawable

Since API level 21 you can use vector instead of bitmap image and for sure vector is smaller than bitmap image because VectorDrawable can reduce its size to different screen densities with the same quality of image.

If you are supporting minSdkVersion 20 and below. Don’t worry you can use it. Because the Android team provides you a support library. So you just use support library version 23.2 and above.

Cumulative Improvement

So here is a cumulative improvement after I apply these tips

One more thing

Starting from Android Studio 3.2 onwards Android App Bundle is the new app publishing format that makes your app smaller in the easier way. You don’t need to add any one line of code you just export it with a new way. So when you download your app, Google Play Dynamic Delivery only delivers code and resources a specific device needs.

In this way you don’t need to do Multiple APKs yourself anymore.

Please be noticed, file extension is gonna be .aab and you need to take more time to build but it’s worth.