Mobile App Development

How you can reduce android application size almost one third?

Sometimes, Smaller is better

As a developer, we always think about the performance of the application, design and user experience. But, we forget one thing that is Application Size. This is one of the key factors to consider if you want to target the next billion users with your application.

1.Shrink your code and resources

To make your APK file as small as possible, you should enable shrinking to remove unused code and resources in your release build.

Code shrinking is available with ProGuard, which detects and removes unused classes, fields, methods, and attributes from your packaged app, including those from included code libraries. ProGuard also optimizes the bytecode, removes unused code instructions, and obfuscates the remaining classes, fields, and methods with short names.

To enable code shrinking with ProGuard, add minifyEnabled true to the appropriate build type in your build.gradlefile.

By setting minifyEnabled to true, you are telling ProGuard to remove all the unused methods, instructions and slim down the classes.dex file.

“With the right MINDSET, you can Survive, Thrive & Grow… Even in the Midst of turbulence and change” ― Tony Dovale

2.Reduce Res

Resource shrinking works only in conjunction with code shrinking. After the code shrinker removes all unused code, the resource shrinker can identify which resources the app still uses. This is especially true when you add code libraries that include resources—you must remove unused library code so the library resources become unreferenced and, thus, removable by the resource shrinker.

To enable resource shrinking, set the shrinkResources property to true in your build.gradle file (alongsideminifyEnabled for code shrinking)

Remove unused alternative resources:

The Gradle resource shrinker removes only resources that are not referenced by your app code, which means it will not remove alternative resources for different device configurations. If necessary, you can use the Android Gradle plugin’s resConfigs property to remove alternative resource files that your app does not need.

For example, if you are using a library that includes language resources (such as AppCompat or Google Play Services), then your APK includes all translated language strings for the messages in those libraries whether the rest of your app is translated to the same language or not. If you’d like to keep only the languages that your app officially supports, you can specify those languages using the resConfig property. Any resources for languages not specified are removed.

The following snippet shows how to limit your language resources to just English and French

3.Convert Your PNGs, JPEGs, and BMPs Into WebP

If your project contains a number of drawables, then compressing them can significantly reduce the size of your APK.

If your project’s minSdkVersion is 18 or higher, then you can compress your PNGs, JPEGs, and BMPs by converting them to WebP format.

First, control-click the drawable that you want to convert and select Convert to WebP… Choose between lossy, or lossless encoding. Select Skip images when the encoded result is larger than the original and click OK to convert the image.

4.Use vector graphics:

You can use vector graphics to create resolution-independent icons and other scalable media. Using these graphics can greatly reduce your APK footprint. Vector images are represented in Android as VectorDrawable objects. With a VectorDrawable object, a 100-byte file can generate a sharp image the size of the screen.

However, it takes a significant amount of time for the system to render each VectorDrawable object, and larger images take even longer to appear on the screen. Therefore, consider using these vector graphics only when displaying small images.

“End-users, not technologies, shape the market. Consequently, marketers need to stay abreast not only of technological developments but also of the way people respond to them.”-Matt Haig

5.Upload your app with Android App Bundles

An Android App Bundle is a new upload format that includes all your app’s compiled code and resources, but defers APK generation and signing to Google Play, you should be able to reduce the size of your APK by an average of 35%.

Google Play’s new app serving model, called Dynamic Delivery, then uses your app bundle to generate and serve optimized APKs for each user’s device configuration, so they download only the code and resources they need to run your app. You no longer have to build, sign, and manage multiple APKs to support different devices, and users get smaller, more optimized downloads.

Conclusion:

By applying the above simple tricks the application size decreases to almost one third. You should always apply above simple tricks to your Android applications to reduce the application size as much as you can.

written by
Sirshendu Sar
Sirshendu Sar Sr. Android Developer