DEV Community

Cover image for Android Tutorial: How to reduce Android app size?
Amit Makhija
Amit Makhija

Posted on • Edited on

Android Tutorial: How to reduce Android app size?

Do you know about 70% of people in emerging markets consider the size of an app before downloading it because they are concerned about data cost and phone storage space.? Are you facing trouble while uploading your apps because of their large size? Here’s a tutorial explaining how you can reduce android app size at the developer’s level as well as how Android App Bundle makes this task easy.

What you will learn?

How to reduce android app size at the developer’s level by,

  • Removing unused resources, code, and classes and reusing resources wherever possible.
  • Reducing the size of images
  • Reducing the size of native and Java codebase
  • A brief explanation of the role of Android app bundle in reducing app size

If the application is large, people would rather not download it all. Let’s imagine that this message pops up when a user installs your app.

Insufficient Storage

The user thinks they’re probably better off without the app. Who would go through all the trouble of finding and deleting unused files and apps for just one app? Even if they have enough storage but the internet speed is slow, they might get bored of the time it is taking to download and stop the process in between.

This is why it is extremely important to develop an app with high performance, quality design and seamless user experience that also doesn’t take up too much space.

The question is – how?

Android developers at Space-O Technologies receive a lot of queries about how they build effective small-sized apps, and this inspired us to write a practical Android tutorial on how to reduce Android app size. We will first get to the methods that a developer should take while developing an app and then go on to understand about Android App Bundle.

Before reducing app size, let’s analyze app size using APK analyzer and understand the APK structure.

Analyze app size

Directories

META-INF/ - Contains the CERT.SF and CERT.RSA signature files, as well as the MANIFEST.MF manifest file.
assets/ - All the app’s assets are contained by this. The app can retrieve them using an AssetManager object.
res/ - This contains resources that aren’t compiled into resources.arsc.
lib/ - This contains the compiled code that is specific to the software layer of a processor.

Files

resources.arsc - It contains compiled resources. All the language strings and styles, layout files and images are included in this. Basically, it contains the XML content from all configurations of the res/values/ folder.

classes.dex - It contains the classes compiled in the DEX file format understood by the Dalvik/ART virtual machine. You may have multiple .dex files if the number of methods is beyond 64K

AndroidManifest.xml: It has the core Android manifest file that lists the name, version, access rights, and referenced library files of the app. The file uses Android’s binary XML format.

How to reduce Android app size or APK size

1.Remove unused resources, code, and classes

The lint tool detects resources in the res/ folder that your code doesn’t reference. The lint tool only detects potentially unused resources and prints a message like this,

res/layout/preferences.xml: Warning: The resource R.layout.preferences appears
to be unused [UnusedResources]

Enable resource shrinking and shrink code using proguard. By setting minifyEnabled to true, proguard removes all the unused methods and slims down the classes.dex file.

Enable resource shrinking

You can also reuse resources. Instead of using different resources, utilize one resource in different manners. Here is an example from the Android Developers site,

<?xml version="1.0" encoding="utf-8"?>
android:drawable="@drawable/ic_thumb_up"
android:pivotX="50%"
android:pivotY="50%"
android:fromDegrees="180" />

If your application requires only one language, for example, English, then you should use resConfig. Enabling resConfig will only add English resources in your apk file. Here is how you can do it,

Enabling resConfig

2.Reduce the size of images

This can be done in many different ways, but we will discuss only a few main ones

a) You could use drawable shapes like this

use drawable shapes

b) Use .webp image format: Convert the .jpg and .png images to .webp format. You can see that there is no reduction in image quality but the size is considerably reduced.

Use webp image format

c) Compress PNG and JPG files

To compress PNG files, you may use tools like pngcrush, pngquant, or zopflipng.

To compress JPG files, tools like packJPG and guetzli are very effective.

d) Use vector graphics for simple images as they utilize space

Use vector graphics

e) Use 9-patch images to save a lot of space. The Draw 9-patch tool is used to create bitmap images that automatically resize to accommodate the contents of the view and the size of the screen.

Use Nine-Patch images

3.Reduce the size of native and Java codebase

This can be done in several ways,

  • Remove unnecessary code that is generated automatically.
  • Avoid enumerations: Use the @IntDef annotation and ProGuard to strip enumerations out and convert them to integers.
  • Reduce the size of native binaries: Two ways to do this

– Remove debug symbols: Use the arm-eabi-strip tool, provided in the Android NDK, to remove unnecessary debug symbols. Always publish your app in the release version.

– Avoid extracting native libraries: By setting android:extractNativeLibs=”false” prevents PackageManager from copying .so files from the APK to the filesystem during installation. This makes updates of your app smaller.

This is what a developer can do on his level, but there is a lot more in store for Android app developers. We’re talking about the Android app bundle format. What’s that?

With an increase in features, Android app sizes kept increasing which led to a decrease in the number of installs. This is why Android studio came up with Android App Bundles.

What is the Android App Bundle (.aab) format? Why use Android App Bundle?

What is the Android App Bundle format?

Initially, developers could publish their apps in the .apk format in two ways,

  • Either as a single file with all the code and resources for the different device configurations that your app supports.

  • Or as multiples files individually handling different device configurations.

Publishing an app with .aab format is the third option that is provided by the Android studio. Android app bundle is the new publishing format for Android apps. It is the best format if you want to reduce Android app size.

Why use Android App Bundle?

Android App Bundle serves only the code and resources a user needs to run your app on their specific device. Using this format you could have 35% size savings as compared to universal APK. You don’t need to use incomplete solutions like multi-APK thus saving you a lot of time and effort.

The app bundle uses the Dynamic Delivery serving model to reduce Android app size. App size is reduced in two main ways,

1.Using Configuration APK

Upload your app with the .aab format. Google Play will use this to generate 2 APKs: a single Base APK and multiple Configuration APKs. Base APK contains all the code and resources required to deliver your app’s base functionality. A user will first receive this APK and ever subsequent APK will depend on this. This is generated from your project’s app or base module.

After this, Google Play will scan the device configuration to generate Configuration APKs. Every time, the app is downloaded on a device, the configuration is scanned and Dynamic Delivery model comes into play. Only a customized configuration APK is downloaded on the device with respect to its individual configuration.

Thus only limited space is required for the app.

Dynamic Delivery model

2.Using on-demand features

If you wish to create a modular app, like a gaming app, then this one is for you. There are features in your application not required by many of your users, they may only download required features. You can distribute your features into dynamic feature modules which decreases the size of your APK, making it a dynamic Android app.

When a user requires a feature of one of these modules, he will install apks and Dynamic Delivery will serve a dynamic feature APK compiling the resources and code to run that specific feature.

Let’s Sum Up!

In this Android tutorial, we learned how to,

  • Remove unused resources, code, and classes. If possible, reuse resources wherever possible.
  • Reduce the size of images using drawable shapes, vector images, 9-patch images or convert to webp format or compress png and jpg images
  • Reduce the size of native and Java codebase
  • Use Android app bundle

We hope that this tutorial helps you to reduce your app size and you consider to develop small-sized feature-rich apps.

If you know any other simpler or faster way to reduce Android app size, please share it in the comment section.

Top comments (2)

Collapse
 
caviro18 profile image
caviro18

Thank you so much for the tutorial. I created an Android app for class room purpose but the size of it was high. I could follow these steps and somewhat reduced the size and the app is working great now but I cannot use it on Nox emulator

Collapse
 
surendersingh1 profile image
surendersingh1

Hi Amit,
This tutorial is very informative, currently i am working on helthcare project which is named as peryourhealth and i have successfully developed an android app which is gateway between customers and hospitals. By following your tutorial, i have reduced unnecessary codes, refactored code and i can see now the android app showing improvement, thanks lot