Sitemap
Dipien

Productivity tools & ideas for Android, Kotlin & Gradle developers.

10 ideas to reduce your APK size [Part I]

Less is More: A Comprehensive Guide to Reducing APK Size and Optimizing Your Android App’s User Experience

6 min readMar 7, 2023

--

Press enter or click to view image in full size

When it comes to Android apps, the download size and install size are two different things.

The download size is the amount of data you need to download from the internet to install the app on your device. This includes the app’s installation file (APK) with all the files required to run the app on that particular device.

On the other hand, the install size is the amount of space the app takes up on your device once it’s been installed. This includes the APK file, as well as any additional files or data that the app generates or downloads during use.

It’s worth noting that the download size is usually smaller than the install size, as some files may be compressed during the download process to reduce the amount of data you need to download. Additionally, some apps may download additional files or data after installation, which can increase the app’s overall size.

Knowing the difference between download size and install size can be useful when managing the storage space on your device. If you’re running low on storage space, you may want to consider deleting apps with large install sizes, even if their download sizes were small.

Both are important to reduce. A download size reduction can reduce download times, which is particularly helpful when your internet connection is not good. For example, in emerging markets where devices connect to spotty 2G and 3G networks or have pay-by-the-byte plans, users tend to avoid downloading apps that appear too large. An install size reduction can help users with devices without too much storage available.

This series of 3 articles will explore some ideas to minimize your app’s download and install size.

You can find 20 more ideas in Part II & Part III.

1. Upload your app with Android App Bundles

To reduce the size of your app when publishing to Google Play, the most straightforward approach is to upload it as an Android App Bundle. This upload format includes all the compiled code and resources for your app but delays the APK generation and signing process to Google Play.

With this app-serving model, Google Play generates and delivers optimized APKs based on the user’s device configuration. As a result, users only download the code and resources necessary to run your app. You no longer need to create, sign, and manage multiple APKs to support various devices, and users can benefit from smaller, more optimized downloads.

2. Use Multiple APK files

If you can’t create bundles for your application, for example, because you need to upload your app to a store that doesn't support app bundles, then you can opt for the option of creating multiple APK files.

Using multiple APK files can be a great way to reduce the size of your Android app. When you create multiple APK files, each one can target a specific device configuration or feature set. This allows you to include only the resources and code that are necessary for each specific configuration, reducing the overall size of your app.

By creating multiple APK files, you can also optimize your app for different screen sizes, processor architectures, and other device configurations. This can help ensure that your app runs smoothly and efficiently on a wide range of devices, which can improve the user experience and increase user engagement.

3. Keep your build tools up to date

Using the latest version of the build tools (like Gradle, Android Gradle Plugin, Android Studio, bundle tool, etc) can be a great way to reduce the size of Android APKs. Newer versions often include improvements in performance and optimizations that can significantly reduce the size of the final APK.

One of the key benefits of using the latest tools is that they often include more efficient algorithms and methods for compressing and optimizing code and resources. For example, newer versions of ProGuard and R8, which are used for code shrinking and obfuscation, can significantly reduce the size of the final APK.

4. Keep your libraries up to date

Using the latest library versions can be a great way to reduce the size of your Android APK. The latest versions often have better performance optimizations, bug fixes, and feature improvements that can make your app run more efficiently and smoothly.

Using the latest library versions can also help you reduce the overall size of your app by eliminating the need for additional code or resources. Many libraries have been optimized to reduce their size and minimize their impact on your app’s APK size. By using the latest versions of these libraries, you can take advantage of these optimizations and reduce the size of your app.

5. Enable R8

The Android Gradle Plugin works with the R8 compiler (instead of Proguard since v3.4.0) to handle the following compile-time tasks:

  • Code shrinking (or tree-shaking): detects and safely removes unused classes, fields, methods, and attributes
  • Resource shrinking: removes unused resources
  • Obfuscation: shortens the name of classes and members
  • Optimization: inspects and rewrites your code to further reduce the size of your app’s DEX files

To minimize the size of your app, it is recommended to activate the shrinking feature in your release build. This will eliminate any unused code and resources in your app, leading to a smaller app size. Additionally, enabling shrinking provides the benefits of obfuscation, which shortens the names of your app’s classes and members, and optimization, which utilizes more advanced techniques to further decrease the size of your app.

To activate shrinking, obfuscation, and optimization, you should include the relevant code in your project-level build.gradle.kts file.

android {
buildTypes {
getByName("release") {
// Enables code shrinking, obfuscation, and optimization
isMinifyEnabled = true

// Enables resource shrinking
isShrinkResources = true

// Includes the default ProGuard rules files that are
// packaged with the Android Gradle plugin.
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
}

6. Minimize usage of third-party libraries

When creating an Android application, it is common to utilize external libraries in order to enhance the app’s functionality and flexibility.

In the event that a library was originally designed for use on a server or desktop, it may contain a plethora of objects and methods that are not necessary for the app. In such cases, sometimes is possible to modify the library’s files and only include the relevant portions of the app. Alternatively, a mobile-friendly library can be employed to achieve specific functionality within the app.

It’s also important to try to reduce to the minimum the usage of third-party libraries. A typical issue is having multiple tracking/analytics libraries that sometimes have similar features.

7. Analyze transitive library dependencies

Minimizing the usage of third-party libraries is not enough. You also need to inspect which transitive dependencies are being loaded by each dependency you include in your project.

You can run the following Gradle task to get a tree with all the transitive dependencies of your app, so you can inspect them:

./gradlew app:dependencies

8. Remove unused dependencies

Having unused dependencies declared on your grade configuration files increases your build times and APK size. You can use the gradle-lint-plugin to detect those unused dependencies. Just add the following configuration to your root build.gradle file:

buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.netflix.nebula:gradle-lint-plugin:{VERSION}'
}
}
allprojects {
apply plugin: 'nebula.lint'
gradleLint.rules = ['all-dependency']
}

And then run the lintGradle task. You will see a report with all the unused dependencies, as well as other useful Gradle warnings. Running this report automatically using your CI tool could be a good idea.

9. Remove your unused code

Removing your unused code is a good way to reduce your APK size. It also has other advantages, like avoiding each developer wasting time understanding and compiling it.

On Android Studio you can use the Code -> Code Cleanup tool to find unused code.

Removing your unused code should be one of your main technical priorities.

10. Avoid enumerations

An enum in your app can increase the size of its classes.dex file by approximately 1.0 to 1.4 KB. This size increase can become significant for larger or more complex systems, as well as shared libraries. To mitigate this impact, you may want to consider using the @IntDef annotation and code shrinking techniques to remove enums and replace them with integers. By doing so, you can maintain the type safety benefits of enums while reducing the overall size of your app.

10 more ideas on Part II

You can follow our DipienMedium Publication for more productivity tools & ideas for Android, Kotlin & Gradle developers.

Support Us

There are different ways to support our work:

  • With Bitcoin Lightning using Alby:
  • With PayPal or a credit card using Ko-fi.

--

--

Dipien
Dipien

Published in Dipien

Productivity tools & ideas for Android, Kotlin & Gradle developers.

Maxi Rosson
Maxi Rosson

Written by Maxi Rosson

I write about Productivity tools & ideas for Android, Kotlin & Gradle developers. Founder of smarthomecompared.com where I compare the best Smart Home device.

No responses yet