Android App Optimization

As an android developer, many of us have faced one common hiccup i.e. increasing app size as new features/modules are being added. In this article, I will share my experience of reducing app size.

Apart from proguard and code shrinking there are many other things we need to take care of to optimize the app.

Before proceeding make sure to do an app health checkup using APK Analyzer Tool

Below are the various steps we followed to reduce the size of one of our apps.

1. Compressing Images:

  • In most android apps, we use png files for background and icons. While adding image files we add nearly 4 kinds of variations for each mdpi, xhdpi, xxhdpi and xxxhdpi format which in turn increases the app size significantly.

2. Removing unused fonts and replacing the existing ones with google fonts

  • We were using the Lato font family with various styles. Each style was taking around 500 to 600kB of space.

Note: Google fonts don’t have all font styles present in a font family.

3. Removing unused library

  • As the code base grows over the years we need to keep an eye on unused libraries that can add to the app size.

4. Android App Bundle

  • An Android App Bundle is a publishing format that includes all your app’s compiled code and resources, and defers APK generation and signing to Google Play.

In the second half of 2021, new apps will be required to publish with the Android App Bundle on Google Play. New apps larger than 150 MB must use either Play Feature Delivery or Play Asset Delivery.

  • Google Play uses your Android App Bundle to build and serve APKs that are optimized for each device configuration.

Generating and testing App Bundle

  • One can easily create a bundle using the GUI option available under
Build -> Build Bundle(s)/APK(s) (debug build)
Build -> Generate Signed Bundle/APK (signed build)

Note: In order to install any app on an android device, we need the file having .apk extension.

In order to test the app bundle, we need to first convert it to an apk file, and to do that we have two options.

Using PlayStore

  • We can upload the bundle using google internal app sharing and share the generated URL for testers to test the app.

Note: One needs to have permission to access the play store account to upload for internal app sharing. Tester also needs to be added in the play console to test the app using the generated link.

Using bundletool

  • It is the underlying tool that Gradle, Android Studio, and Google Play use to build an Android App Bundle or convert an app bundle into the various APKs that are deployed to devices.
To generate debug apks
bundletool build-apks --bundle=/MyApp/my_app.aab --output=/MyApp/my_app.apks
To generate signed apks
bundletool build-apks --bundle=/MyApp/my_app.aab --output=/MyApp/my_app.apks
--ks=/MyApp/keystore.jks
--ks-pass=file:/MyApp/keystore.pwd
--ks-key-alias=MyKeyAlias
--key-pass=file:/MyApp/key.pwd
  • We can install the app to any connected device from the generated apk set from the below command.
bundletool install-apks --apks=/MyApp/my_app.apks

Note: Find the various options we can use while generating the apks using bundletool here

Report card of our app after performing the above-mentioned steps.
For release builds in Moto G5 Plus device-OS 8.0
Previous build size: 21.6MB
New build size: 9.7MB

Please find the below blog which describes the importance of the app bundle and how to generate and test it in detail.

Important links

https://developer.android.com/studio/build/shrink-code

https://developer.android.com/platform/technology/app-bundle

https://developer.android.com/guide/app-bundle

Android Developer