How to Use Proguard to Reduce APK Size in Android?

23 Feb, 2021
App size matters a lot when building any application. If the app size is larger most of the users will not download your app due to its huge size. The large app size will cost your user huge data and he will also require disk space to install your application. So to reduce the size of APK proguard is one of the important java tools which is used to reduce the size of your APK. In this article we will take a look at the topics mentioned below: 
What is Proguard?
What are the uses of Proguard?
How to use proguard inside your app?
What are the disadvantages of using Proguard?
Custom rules in Proguard.
Proguard is a java tool that is present in Android and it is used to reduce your app size. It is a free tool that will help you to reduce your app size by 8.5 %. Proguard will convert your app’s code into optimized Dalvik bytecode. The process for conversion is as follows: 
Proguard will obfuscate your code and rename your code. In this process, it will rename your files of different java classes. This will make your app more secure and difficult to reverse engineer.
It will remove unused code snippets inside your code and optimize your code.
Using proguard will reduce your app size by 8.5 %.
Proguard will shrink the resources inside your app which is not being used in your app.
The usage of proguard will reduce your app size and make your code inline. This will make your app faster and less in size.
To enable proguard inside your app, navigate to the app > gradle Scripts > Open build.gradle file. Inside that gradle file you will get to see below lines of code in your file inside the release section. Change minifyEnabled from false to true. This will activate your proguard with the proguard file. 
buildTypes {
       release {
           // make change here
           minifyEnabled true
           proguardFiles getDefaultProguardFile(‘proguard-android-optimize.txt’), ‘proguard-rules.pro’
         }
   }
You will find this set of code inside your release block. This means that Proguard will be activated for your release app only.  
When we use proguard inside our application it will automatically remove some modal classes which are required inside our app. So to avoid removing these files we have to add some custom rules inside our application. 
While using libraries for networking or while implementing a RecyclerView or ListView. We have to create a Data class for storing all our Data. So while using the Proguard it will remove the variables of the class which is of no usage. So to avoid the removal of variables of this class we have to add @Keep annotation to that class. For example, we are creating a data class for storing studentName and StudentId inside our class and we don’t want proguard to obfuscate our class. So in this case we will add @Keep annotation to it. So in the below code snippet @Keep will prevent this class from obfuscating. Along with “@keep” we can also use -keep to prevent our class obfuscating. If we have to preserve any variable of our class then we will annotate that variable with “@SerializableName”. This type of annotation is mostly used when we have to parse data from a JSON file or from a server.  
Java
/*package whatever //do not write package name here */  import java.io.*;@Keepclass GFG {   string StudentName;     int studentID;}
Suppose if we have to keep the name of our class and its member variables then we will use annotation as ‘-keepnames’. This annotation will keep the name of that class as well as its member variables. Although proguard will shrink this class it will not obfuscate your class. Below code snippet will tell us that How we have to use “-keepnames” to keep our class members. 
Java
/*package whatever //do not write package name here */  import java.io.*;  -keepnames class GFG {   string studentName;  int studentId;}
If we want to keep only the members of the class and not the class from obfuscating by Proguard. Then we will annotate that class with “-keepclassmembers”. This annotation will prevent the obfuscating of our class members. Below is the code snippet in which we will see the implementation of this method.
Java
/*package whatever //do not write package name here */  import java.io.*;  -keepclassmembers  class GFG {   string studentName;  int studentId;}
When using Proguard it will remove all the annotations which we add in our class provided by libraries. In any case, this code works fine without any issue. But it may cause issues if removed. If you are using a Retrofit library for fetching data from web servers in your app then you use so many annotations such as “@GET”, “@POST”,”@PUSH” and many more. If these annotations will get removed Retrofit may cause issues to get the data from the server. So to avoid the removal of these annotations we will use “-keepattributes ” keyword. Below is the code snippet of its usage. 
Java
/*package whatever //do not write package name here */  import java.io.*;  -keepattributes @GETclass GFG {   string studentName;  int studentID;}
When we are using some external libraries. They provide specific rules which we can add to our proguard rules. But if the library is not having any rules present in it. Then in that case we can get to see warnings in our logs. To avoid the warnings of this library we have to add the library with “-dontwarn” annotation in our proguard. 
-dontwarn “your library”
Sometimes while using Proguard it will remove a huge set of code that is required and this might cause crashes in your application. So to avoid removing this code from our application code. We have to add some custom rules inside our Proguard to keep that specific file so that removal of that files will be avoided and our app will not crash even after using proguard.
So Proguard is very helpful in reducing the size of our app and it will also make our app more secure and difficult to reverse engineer. So we should use this for optimization of our app’s size. 
Android-Misc
Technical Scripter 2020
Android
Java
Technical Scripter
Java
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
Arrays in Java
Arrays.sort() in Java with examples
Reverse a string in Java
Split() String method in Java with examples
Object Oriented Programming (OOPs) Concept in Java