Minimizing APK Size: Techniques for Shrinking Android App Size

Diego Marcher
4 min readOct 16, 2023

In the world of mobile app development, optimizing your Android app’s APK (Android Package) size is a crucial task. Smaller APK sizes not only reduce the download and installation times for your users but also save valuable storage space on their devices. In this article, we’ll explore various techniques for minimizing your Android app’s size, ensuring a smooth user experience without compromising functionality.

Proguard and R8 Code Shrinkers

Proguard and R8 are code shrinkers that can significantly reduce the size of your APK by removing unused code, renaming classes, and making your code more compact. They are integrated into Android Studio and are easy to use.

To enable Proguard in your Android project, add the following lines to your build.gradle file:

android {
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt')
}
}
}

Proguard configuration files can be customized to exclude specific classes, methods, or resources that should not be obfuscated. This is especially important for libraries and frameworks. For example, to keep specific classes from being obfuscated, add the following to your Proguard configuration:

-keep class com.example.myapp.model.** { *; }

R8 is the next-generation code shrinker from Google, and it’s the default option in newer Android projects. It provides similar code shrinking capabilities and is enabled by default in your build.gradle:

android {
buildTypes {
release {
minifyEnabled true
useProguard false
}
}
}

Resource Optimization

Vector Drawables

Replacing bitmap images with vector drawables can reduce the size of your app significantly. Vector drawables are resolution-independent and can adapt to various screen sizes and densities. To use vector drawables, you should convert your SVG or XML vector graphics to Android Vector Drawable format.

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_vector_image" />

Image Compression

Optimize image assets by reducing their size without sacrificing quality. Tools like ImageMagick, TinyPNG, or Android Studio’s Image Asset Studio can help in this process.

Modularization

Splitting your app into modules can help you deliver only the necessary code and resources to the user. Android App Bundles provide a way to modularize your app, and the Play Store can serve users with tailored APKs based on their device configuration, reducing the overall size of the download.

In your build.gradle, enable App Bundles:

android {
// ...
bundle {
language {
enableSplit = true
}
density {
enableSplit = true
}
}
}

Code Optimization

Lint Warnings

Addressing lint warnings in your code can lead to substantial size reductions. Lint often detects redundant code, which, when fixed, can significantly reduce your app’s size. For example, consider the following lint warning:

// Unnecessary object creation
String result = String("Hello, World!")

Optimize the code by removing the unnecessary object creation:

val result = "Hello, World!"

Removing Unused Resources

Unused resources, such as layouts, strings, or drawables, contribute to APK bloat. Android Studio has a built-in Lint check for unused resources, which you can run to identify and remove them. For example, if you have an unused layout file, Lint will flag it:

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Unused Button" />

You can safely delete this unused layout to reduce the APK size.

Proguard Rules

Writing custom Proguard rules to keep only necessary code can significantly reduce APK size. For instance, if your app uses the popular Gson library, you can add Proguard rules to preserve only the parts you need:

-keep class com.google.gson.** { *; }

This rule ensures that Gson-related classes and methods are not removed by Proguard.

Code Splitting

Lazy loading of certain features or modules can be implemented to load code on-demand rather than including it in the main APK. The Android App Bundles approach mentioned earlier supports dynamic delivery of modules.

Native Libraries

If your app includes native libraries, consider optimizing them for size. Use the strip and objcopy tools to remove unnecessary symbols and reduce the size of your native libraries.

Use AAPT2

AAPT2 (Android Asset Packaging Tool 2) is the improved version of the original AAPT. It can reduce the size of resources by eliminating redundancy. Make sure you are using AAPT2 by default in your project by adding this line to your gradle.properties file:

android.enableAapt2=true

Reduce Dependencies

Every library you include in your project adds to the APK size. Carefully evaluate and remove unnecessary dependencies, or consider alternative, smaller libraries with similar functionality.

Conclusion

Minimizing your Android app’s APK size is a critical task in mobile app development. By using a combination of code shrinking, resource optimization, modularization, and other techniques, you can create leaner apps that load faster, consume less storage, and provide a better user experience.

Remember that optimizing APK size should be an ongoing process, and regular audits of your app’s size can help you identify new opportunities for reduction. Stay up-to-date with the latest tools and techniques, and keep an eye on the evolving Android ecosystem for more ways to minimize your app’s size.

--

--