How to Use Proguard in Android Application Properly?

19 Sep, 2021
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 library provides. Also, you may have developed some code that will be obsolete in the future and failed to delete it. These factors are to blame for the increased size of your application’s APK. Android has Proguard capability to minimize the size of the APK. Using Proguard aids in fulfilling the three functions listed below:
Minify the codeObfuscate the codeOptimize the code
Minify the code
Obfuscate the code
Optimize the code
As a result, Proguard will assist you in shrinking the size of your APK, removing unnecessary classes and methods, and making your program tough to reverse engineer. So, while using Proguard, you need to keep a few things in mind. In this article, we will look at some of the aspects that must be considered while utilizing Proguard in our Android application. So let us begin with the first point.
Almost every Android application contains a Data or Model class that is used to retrieve data from a remote database. So, if you use Proguard, the Progurde will modify the class, variable, and method names. 
For example, if you have a class named User and it has variables such as firstName and lastName, the Proguard will rename your User class to A or something like that. Simultaneously, your variable names may be altered to some x, y, or z. As a result, if you try to put the data of the same variable in the database, you will obtain a run time error since no such field exists in the database.
To circumvent this, instruct Proguard to preserve the variables and methods of the User class and not obfuscate them. To accomplish this, add the following line to your proguard-rules.pro file:
-keep class full.class.name.of.your.project.here.** { *; }
By adding the preceding line, the Proguard will do no obfuscation or optimization of the code in the chosen class.
The majority of Proguard’s issues are caused by its obfuscation characteristic. The same as with Fragments.
We define some String TAG as FragmentName.class whenever we create a Fragment. use getSimpleName() to get the TAG and utilize it in our Fragment. However, if you are using Proguard, this may cause a problem in your application. Assume we have two fragments in the firstfragment package named Fragement1 and Fragment2 in the secondfragment package. As a result, the Proguard will change the firstfragment package to “a” and the secondfragment package to “b,” but the Proguard can also change the names of Fragment1 to “a” (because the package and Fragments are different, so names can be the same) and Fragment2 to “a” as well, because Fragment2 is a different package.
However, if you are currently using FragmentName.class.getSimpleName(), this will cause confusion since Fragment1.class.getSimpleName() has been changed to a.class.getSimpleName() and Fragment2.class.getSimpleName() has been changed to a.class.getSimpleName() (). Both TAG values will get perplexed as to which fragment is being called. Using FragmentName.class.getSimpleName() should thus be avoided.
When your classes and methods are accessible dynamically, such as through reflection, you must ensure that your class or function is obfuscated. For example, if you reference your code from an XML file (which normally uses reflection) and use Proguard at the same time, Proguard will change your class name to “a” or something similar in the XML code, no changes will be made, and this will result in a class not found or method not found exception.
As a result, always include the class utilizing reflection in your Proguard rules.
When you construct your own view in an Android application, ensure sure the view class you’re creating is included in the Proguard rule, or you’ll get a class not found error.
For example, if you create a simple view named com.geeksforgeeks.android and use it in your XML file, the Proguard will alter your view class name to “a” or something similar, and your code in the XML is using com.geeksforgeeks.android, but there is no such class since it has been changed to “a.”
We use a lot of open-source libraries in our Android application. However, if you are using Progurad, you must also provide the Proguard rules of the libraries you are using, if any. 
You may locate the Proguard rules of a library by reading the README.md file of the library you used and adding the provided Proguard rules to your application. An example of the Retrofit library’s Proguard rules may be found here.
You must be mindful of the effects of Proguard’s obfuscation when utilizing native code in your Android application, i.e. when using c++ code in your Android application. The Proguard can only inspect the Java class in general. So, if you call a Java method from C++ code and that function has been obfuscated by Proguard, the JNI will throw a method not found error.’
-keepclasseswithmembernames your.class.name.here * { native <methods>; }
Geek Tip: So, if you call any methods from native code, you must include such methods in the Proguard rules by including the following line in the proguard-rules.pro file:
In Java, we can load resources from a JAR file, and certain libraries can also load resources from an APK file. This is not an issue, but if we use Proguard, the Proguard will alter the names of these resources files, and these files will not be discovered in the required package. As a result, the names of classes that load resources from the APK should be kept in your Proguard rules.
When you’re finished optimizing your code, place all of the files in the root package to eliminate the need for fully qualified names like com.geeksforgeeks.android.example. To enable this feature, include the following line in your proguard-rules.pro file:
-repackageclasses
In this article, we learned about the precautions that should be followed when utilizing Proguard in our Android app. We discovered how to maintain specific classes in the Proguard. We also learned how to add Proguard rules from third-party libraries.
Picked
Android
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference Between Implicit Intent and Explicit Intent in Android
Retrofit with Kotlin Coroutine in Android
Flutter - BoxShadow Widget
Bundle in Android with Example
Animation in Android with Example
ExoPlayer in Android with Example
Bottom Navigation Bar in Android Using Kotlin
How to Post Data to API using Retrofit in Android?
Android | How to Create/Start a New Project in Android Studio?
Data Binding in Android with Example