How to use R8 to Reduce APK Size in Android?

23 Feb, 2021
If you are building any apps and you want to target this app to a large audience then there are so many factors which you should consider while building your apps. The most important factor while building any application is its size. The size of the app matters a lot. If your app size is very high then users don’t like to download such big apps that require such a huge storage space and data charges. So it is very important to maintain your app size for your users while building any Android application. In this article, we will take a look at reducing app size using R8. 
What is R8?
How to use R8 shrinking in your app?
How the shrinking in R8 works?
For enabling R8 in your application you have to use the Android Studio version like 3.4 or higher.
The Gradle version inside your project should have a version of around 3.4.0
R8 is an app shrinking tool that is used to reduce the size of your application. This tool present in Android Studio works with the rules of Proguard. R8 will convert your app’s code into optimized Dalvik code. During this process of conversion R8 will remove the classes and methods which are unused inside our application. Along with this android applications are having so many unused files that we don’t require inside our application so R8 will remove that files and helps in the reduction of app size. The benefit of using R8 is that it will make your app more secure and difficult to reverse engineer. So this will helps us to increase the security of our application. 
Optimization of your code: In this process, R8 will actually optimize the code to reduce app size. So in the process of optimization, it will remove the unused classes, methods, and dead code present inside our application.
Identifier Renaming: In this process, R8 will actually rename our class names and variable names. While writing code we use some identifier pattern so that we can understand our code properly. But in this process, R8 will rename all the classes into smaller variable names that will also help to reduce APK size. for example, you have generated a new class named “Constant” for storing some constants which are being used in different parts of our application. R8 will rename that class in any smaller name let’s say “a” or in any other format.
Shrinking: While writing code we create so many methods inside our application. While creating this method some of the methods are not being used inside our application, those methods are called unreachable methods which are not being used anywhere inside our application. So R8 will remove such unreachable methods to reduce the size of APK.
Optimization of 3rd party library: For the implementation of many external features inside our application developers generally prefer to use external libraries inside their application. But while using these libraries this it will install so many files that we don’t require for our app. So R8 will optimize the code for that library and keep only that code that is required in our application and will remove the files which are unused.
Now we will move towards the practical implementation of R8 inside our app. 
Implementation of R8 shrinking is a single-step process. First of all, create a New Project with an empty activity inside Android Studio. For creating a new project in Android Studio. Take a look over this link on How to create a new project in Android Studio. After creating this new project. Navigate to the Gradle scripts > build.gradle(:app) and you will get to see the section of buildTypes. Inside this change minifyEnabled from false to true to enable R8. Below is the section where we have to make a change.
buildTypes {
       release {
           // make change here
           minifyEnabled true
           proguardFiles getDefaultProguardFile(‘proguard-android-optimize.txt’), ‘proguard-rules.pro’
           }
   }
After changing it true sync your project and R8 is being added inside your application You can now test the APK size by building the APK. You can also test the APK size by adding some external libraries. Test the APK size using R8 and without using R8 you will get to see the difference between the size of APK. 
The algorithms which are used by R8 for the reduction of app size will trace for the unused methods and unreachable code in your application and remove those methods. So R8 will start tracing your code from an entry point which we have to declare for any class and from that entry point R8 will starts tracing the code for the unused code. For example, we will take a look at a class which is created below: 
Java
/*package whatever //do not write package name here */  import java.io.*;  // entry point for R8 class GFG {  // Method one is used   // inside our main method.   private void methodone(){            System.out.println("Method 1 ");  }  // Method two is not being  // used in our main method.   private void methodtwo(){            System.out.println("Method 2 ");  }              // Our main method     public static void main (String[] args) {      methodone();    }}
Inside the proguard we will ad the below rule to it as: 
-keep class GFG {
   public static void main (String[] args) {
       }
}
In the above example, the proguard will start scanning your application from the main method and inside this method, the methodone() will be called first and after printing method one the tracing will be stopped. Then R8 will look for the unused method inside our application the unused method is methodtwo() which we are not using inside our main method. So R8 will remove that methodtwo() inside our application. and the methodone() will be renamed with some smaller names and align the code properly. After aligning the code will look like the below example. 
Java
/*package whatever //do not write package name here */  import java.io.*;  class GFG {    public static void main (String[] args) {            System.out.println("Method 1 ");    }}
So inside our application in Android Studio, there are so many files that are to be traced by R8. So for tracing these files and deciding the entry point for each file aapt2 tool is used. Add the below line below to that of the line where you have enabled your proguard.
minifyEnabled true
proguardFiles getDefaultProguardFile(‘proguard-android-optimize.txt’)
But while using this there are some problems with gson files When your code contains some reflection files So it will remove that code but we have to keep that code inside our project. For implementing this add the below line under the minifyEnabled true. 
proguardFiles getDefaultProguardFile(‘proguard-android-optimize.txt’), ‘proguard-gson.pro’, ‘proguard-rules.pro’
So to overcome the issues while Gson in Android adds the line which is shown above in your build.gradle file. So in this way we have implemented the R8 app shrinking inside our application and this will helps to reduce the size of our app. 
Android-Misc
Technical Scripter 2020
Android
Technical Scripter
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Add Views Dynamically and Store Data in Arraylist in Android?
Android SDK and it's Components
Flutter - Custom Bottom Navigation Bar
How to Communicate Between Fragments in Android?
Retrofit with Kotlin Coroutine in Android
How to Post Data to API using Retrofit in Android?
Flutter - Stack Widget
Introduction to Android Development
Activity Lifecycle in Android with Demo App
Fragment Lifecycle in Android