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.
- One of the solutions for this is to use vector files but again in our app we were facing some UI glitch using the vector files so we had to stick to the png files in our app.
- One way to reduce the app size was to reduce the size of the png files we have used in our app without decreasing the quality of the file.
- One of the tools we used to reduce the file size is TinyPNG, which helped us to reduce the size of our app. Though it was not a very significant change in app size still every KB counts.
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.
- The first thing we did was removing the unused font files which were added during the design phase.
- The next thing we did was to replace the font styles with Google fonts.
- The google fonts will provide the same fonts which are more optimized.
Eg: Before we had the font with 600 kB. In google fonts, we can get the same font with 60 kB.
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.
- Remove the unused libraries/dependencies and update the app.
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.
- Though the app bundle publishing format came a couple of years ago most of us still using APK publishing format while releasing the build.
- An important thing to be noted is bundle format will be made compulsory for new apps in the second half of 2021 as stated below in android documentation.
- Google Play uses your Android App Bundle to build and serve APKs that are optimized for each device configuration.
- App Bundle helps users to enjoy a smaller app download without unused code and resources needed for other devices.
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.
- We can upload the bundle using google internal app sharing and share the generated URL for testers to test the app.
- Testers can then use the generated URL to install 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.
- 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.
- When bundletool generates APKs from your app bundle, it includes them in a container called an APK set archive, which uses the .apks file extension.
- Below bundletool commands are used to generate the apk’s from the bundle.
To generate debug apks
bundletool build-apks --bundle=/MyApp/my_app.aab --output=/MyApp/my_app.apksTo generate signed apks
bundletool build-apks --bundle=/MyApp/my_app.aab --output=/MyApp/my_app.apks
- 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.
Exploring the Android App Bundle
There were a ton of exciting things announced at I/O this year — one of the things that caught my eyes the most was the…