DEV Community

Cover image for Reducing APK size in Android made easy with R8
Nwokocha wisdom maduabuchi
Nwokocha wisdom maduabuchi

Posted on

Reducing APK size in Android made easy with R8

Alt Text
Android application size matters a lot to the users and also to the developer because most users tend to download apps with a smaller size than apps with a bigger size. size of an android application determines the number of users that will download your app. It's now an important task for every android developer to reduce the apk size.

Below are the topics that will be covered in this article.

1: What is R8 Shrinking?
2: How to enable R8 Shrinking in your app?
3: Shrink your code
4: Shrink your resources

What is R8 Shrinking?

R8 shrinking is a process in which we reduce the amount of code/resources of our application, which makes the APK size automatically gets reduced. So, reduced APK sized applications are more likely to be kept in people's phones.
To make your app as small as possible, you should enable shrinking in your release build to remove unused code and resources. When enabling shrinking, you also benefit from obfuscation, which shortens the names of your app’s classes and members, and optimization, which applies more aggressive strategies to further reduce the size of your app.
When you build your project using Android Gradle plugin 3.4.0 or higher, the plugin no longer uses ProGuard to perform compile-time code optimization. Instead, the plugin works with the R8 compiler to handle the following compile-time tasks:

Code shrinking (or tree-shaking):

detects and safely removes unused classes, fields, methods, and attributes from your app and its library dependencies (making it a valuable tool for working around the 64k reference limit). For example, if you use only a few APIs of a library dependency, shrinking can identify library code that your app is not using and remove only that code from your app.

Resource shrinking:

removes unused resources from your packaged app, including unused resources in your app’s library dependencies. It works in conjunction with code shrinking such that once unused code has been removed, any resources no longer referenced can be safely removed as well.

Obfuscation:

shortens the name of classes and members, which results in reduced DEX file sizes.

Optimization:

inspects and rewrites your code to further reduce the size of your app’s DEX files.

How to enable R8 Shrinking in your app?

When you use Android Studio 3.4 or Android Gradle plugin 3.4.0 and higher, R8 is the default compiler that converts your project’s Java bytecode into the DEX format that runs on the Android platform. However, when you create a new project using Android Studio, shrinking, obfuscation, and code optimization is not enabled by default. That’s because these compile-time optimizations increase the build time of your project and might introduce bugs if you do not sufficiently customize which code to keep.

So, it’s best to enable these compile-time tasks when building the final version of your app that you test prior to publishing. To enable shrinking, obfuscation, and optimization, including the following in your project-level build.gradle file.



android {
    buildTypes {
        release {
            // Enables code shrinking, obfuscation, and optimization for only
            // your project's release build type.
            minifyEnabled true

            // Enables resource shrinking, which is performed by the
            // Android Gradle plugin.
            shrinkResources true

            // Includes the default ProGuard rules files that are packaged with
            // the Android Gradle plugin. To learn more, go to the section about
            // R8 configuration files.
            proguardFiles getDefaultProguardFile(
                    'proguard-android-optimize.txt'),
                    'proguard-rules.pro'
        }
    }
    ...
}



Shrink your code

Code shrinking with R8 is enabled by default when you set the minifyEnabled property to true.

Code shrinking (also known as tree shaking), is the process of removing code that R8 determines is not required at runtime. This process can greatly reduce your app's size if, for example, your app includes many library dependencies but utilizes only a small part of their functionality.

Shrink your resources

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 shrink resources property to true in your build.gradle file (alongside minifyEnabled for code shrinking). For example:



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




Source

Alt Text
Congratulations you now know how to reduce your Apk size

Top comments (1)

Collapse
 
android_blaze profile image
Uchenna Chukwuwa

Wow thanks for this information