How to reduce APK size in Android?

How to reduce APK size in Android?

No user would like to download a large APK as it might consume most of his Network/Wifi Bandwidth, also most importantly, space inside the mobile device.

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.

It's important to optimize the size of the app since mobiles are always memory and space constraint devices. So, what are the various ways in which we can improve our apk size in Android-Development? Let’s go ahead and learn in this article.

Welcome to our MindOrks blog on Reducing application size in Android.

Understanding Android App Bundles

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.

How to reduce APK size in Android?

Google Play uses your app bundle to generate and serve optimized APKs for each device configuration, so only the code and resources that are needed for a specific device are downloaded to run your app. You no longer have to build, sign, and manage multiple APKs to optimize support for different devices, and users get smaller, more optimized downloads.

App Bundles are Publishing formats

How to reduce APK size in Android?

Image Credit: Google Android Official Website.

Android App Bundles — File Targeting and Serving

  • How do these Android App bundles help in File targeting? It's simple, let's say we have hdpi, xhdpi, xxhdpi resources in our application. Based on the device on which the application is being downloaded, if it's a hdpi device (for example), only the resources from hdpi will be installed on the device.
  • If the application is targeting multiple languages (English, Spanish, French, etc.), only the specific string resources will be downloaded onto the device.
  • This helps in saving the space on the device's memory.

Building the Android App Bundle

  • Building the Android app bundle is straight forward. Just select the Build option from the Android Studio menu and select Build Bundles
How to reduce APK size in Android?

Android Size Analyser

In order to understand which files are actually taking up more space in the application, use the Android Size Analyser plugin inside Android Studio. For installing the plugin

  1. Select File > Settings (or on Mac, Android Studio > Preferences .)
  2. Select the Plugins section in the left panel.
  3. Click the Marketplace tab.
  4. Search for the “Android Size Analyzer” plugin.
  5. Click the Install button for the analyzer plugin.
How to reduce APK size in Android?

Restart the IDE after installing the plugin. Now, to analyze the application, go to Analyze > Analyze App Size from the menu bar. We will get a window something similar to this:

How to reduce APK size in Android?

The recommendations can help us in reducing the app size in a much better way.

Remove Unused Resources

As we have already discussed the size of the apk has an impact on how fast the app loads, how much memory it uses, and how much memory power it consumes. Hence, one of the main things that can be implemented to reduce apk size is to remove unused resources in the application.

Also, it is advised to use scalable drawable objects(importing vector assets) instead of other image formats like PNG, JPEG, etc.

Using Vector Drawable is one of the best ways to reduce the size significantly.

Using Lint

Lint actually helps in generating warnings or unused code inside the application. So this can actually help in removing the same thereby helping in reducing the size of the application.

Reduce libraries size

Check if you can reduce the size when it comes to the usage of libraries. For example, use only specific libraries of Google Play Services. Compile only which is required.

Reuse Code

Object-Oriented Programming has solved a lot of problems in the programming world. Try reusing the code as much as possible instead of repetitive code. Repetitive code also leads to increased file size thereby effecting the Apk size.

Compress PNG and JPEG files

If using PNG and JPEG files is something mandatory in your project, you can compress them using image quality tools like TinyPNG.

In most of the applications, Images are used to convey messages or improve the UX. But the biggest drawback here might be using a lot of images that can bloat up the size of the app. Ensure that the Image Compression techniques are understood and implemented to reduce the size of the apk before releasing the app to the play store.

To learn more about Image Compression in Android, you can refer our MindOrks article on the same.

Use WebP file format

As we have seen in the image shared for the Android Analyser plugin above, one of the recommendations was to change the PNG file to a WebP file format.

Use Proguard

Every time we build a new project, we see the following piece of code in the app-level build.gradle file

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
}

ProGuard makes the following impact on our project,

  • It reduces the size of the application.
  • It removes the unused classes and methods that contribute to the 64K method counts limit of an Android application.
  • It makes the application difficult to reverse engineer by obfuscating the code.

To learn more about Proguard, you can refer our MindOrks article on the same.

Create Multiple APKs

If we are not using App Bundles, we can go with the traditional way. We can create multiple APKs similar to App Bundle. Multiple apk is mainly used to generate specific APKs for different screen densities and different CPU architecture.

To learn more about it, you can refer to our MindOrks article on the same.

ShrinkResources

Reduce resources where ever possible. Using shrinkResources attribute in the Gradle will remove all the resources which are not being used anywhere in the project. Enable this in your app-level build.gradle file by adding below line:

buildTypes {
    release {
        ........
        shrinkResources true
        ........
    }
}

ResConfigs

Remove the localized resources which are not needed by using resConfigs . All the support libraries may have localized folders for the other languages which we don’t need.

The Gradle resource shrinker removes only resources that are not referenced by your app code, which means it will not remove alternative resources(device/location-specific) 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.

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

android {
    defaultConfig {
        ...
        resConfigs "en", "fr"
    }
}

As discussed, having unused language resources only swells the apk size. Hence it is important to remove unused files and resources.

DebugImplementation

Remove any debug library you have in the app. It can be done by using debugImplementation while building testing debug apk.

Like we at MindOrks use the library Android Debug Database for debugging database.

debugImplementation 'com.amitshekhar.android:debug-db:1.0.1'

Use R8 to reduce APK size

R8 shrinking is a process in which we reduce the amount of code of our application and by doing so, the APK size automatically gets reduced. R8 does most of the work as Proguard. Why do we need to prefer it? The reason is it works with Proguard rules and shrinks the code faster while improving the output size.

To learn more about using R8 to optimize apk size, you can refer our MindOrks article on the same.

Whats better finally than understanding and implementing these steps in our applications to deliver an optimized apk to our customers!

That’s all about this article.

We hope that this article has been of some help in understanding different methods to reduce app size in Android.

Thank you so much for your time!

Keep Learning, Keep Sharing, Keep Supporting!

Team MindOrks!

Also, Let’s connect on Twitter , Linkedin , and Github