How to Use Proguard to Reduce APK Size in Android?
Last Updated :
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.
What is 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:

What are the Uses of Proguard?
- 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.
How to use Proguard inside your app?
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.
Custom Rules in Proguard
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.
1. Keeping our class
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
import java.io.*;
@Keep
class GFG {
string StudentName;
int studentID;
}
|
2. Keeping names of our Class and its members
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
import java.io.*;
-keepnames class GFG {
string studentName;
int studentId;
}
|
3. Keeping the members of our class
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
import java.io.*;
-keepclassmembers class GFG {
string studentName;
int studentId;
}
|
4. Keeping annotations
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
import java.io.*;
-keepattributes @GET
class GFG {
string studentName;
int studentID;
}
|
5. Using external libraries
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”
Disadvantages of Using Proguard
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.
Conclusion
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.
Similar Reads
How to use R8 to Reduce APK Size in Android?
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 t
6 min read
How to Reduce APK Size in Android?
APK size is one of the most important factors while building any app for any organization or any business. No user would like to install a very large APK and consume his data on downloading that APK. The APK size will make an impact on your app performance about How fast it loads, How much memory it
7 min read
How to Use Proguard in Android Application Properly?
While writing the code for your Android app, there may be some lines of code that are unnecessary and may increase the size of your app's APK. Aside from useless code, there are numerous libraries that you may have incorporated in your program but did not use all of the functionalities that each lib
6 min read
How to Use APK Signature Scheme v3 in Android?
APK Signature Scheme v3.1, an upgrade to the previous APK Signature Scheme v3, is now supported by Android 13. Some of the rotation-related known problems with APK Signature Technique v3 are addressed by this scheme. Applications can accept both original and rotated signers in a single APK thanks to
4 min read
How to Use PRDownloader Library in Android App?
PRDownloader library is a file downloader library for android. It comes with pause and resumes support while downloading a file. This library is capable of downloading large files from the internet and can download any type of file like image, video, pdf, apk and etc. It provides many features that
11 min read
How to Resize a Bitmap in Android?
ImageViews are used within the android application to display different types of images. Many times images are displayed within the ImageView using bitmap instead of drawable files. In this article, we will take a look at How to Resize Bitmap in the android application to resize our image to be disp
3 min read
How to Reduce and Compress Image Size in Android Studio?
Android Studio is the official IDE (Integrated Development Environment) for Android app development and it is based on JetBrainsâ IntelliJ IDEA software. Android Studio provides many excellent features that enhance productivity when building Android apps. Here, we are going to reduce and compress an
2 min read
How to Generate Unsigned (Shareable) Apk in Android Studio?
Unsigned Apk, as the name suggests it means it is not signed by any Keystore. A Keystore is basically a binary file that contains a set of private keys. Every App that we install using the Google Play App Store needs to be signed with a Keystore. The signed apk is simply the unsigned apk that has be
2 min read
How to Download Free Apps on Android?
Downloading free apps on your Android device is a simple process that can greatly enhance your smartphone experience. With millions of apps available on the Google Play Store, you can find tools, games, productivity apps, and much more without spending a dime. In this guide, we will walk you through
4 min read
How to Utilize the Resource of Small-Scale Apps on Android OS?
Understanding the resource utilization of small-scale applications on the Android operating system is crucial for developers aiming to optimize performance and ensure a smooth user experience. Efficient resource management in Android apps involves monitoring and optimizing memory usage, CPU consumpt
5 min read