Learn Proguard in Android

Tram Ho

1. What is Proguard?

Proguard is a free java tool in android. Specifically it helps our application:

  • Reduce the size of the application significantly
  • Optimize code by removing unused classes and methods, creating inlining functions from existing, passable functions.
  • Contribute to our code decompilation by changing class names, methods, variables, function names, etc …

2. Proguard benefits

Proguard is a very useful tool for creating an optimal application. It helps our application to reduce an amount of redundant code (unusing code), the application is greatly optimized so it will make the application run smoother. Proguard is created by default when creating an Android application from Android Studio, and we can customize some configurations to suit the project. They offer many benefits, such as:

  • It makes decompiling the code in our application extremely difficult. By renaming, briefly renaming classes, … For example: CategoriesViewModel can rename to CV.
  • Helping our application to have a significantly reduced size by ignoring unused resources in the project like pictures in drawable. That will make our application lighter and faster.

3. How to use proguard in application?

In the app’s build grandle:

  • minifyEnabled true : When we set the value to true , our code will help us at this time when we build the release version, it will be optimized, decompiled, etc.Otherwise it will activate the rules in the proguard file. -rules.pro this.

This is also where we will add the desired rules and the necessary rules for the library we are using in the project. To avoid on the release, proguard will remove a large amount of code and break the activity flow of the application. The most common case is that in the libraries we use in the application, there are functions we don’t use, but when removed it produces an error so we have to add some rules that have been issued by the publisher. the proposed library operator or on the community using that library. The most effective way to avoid unexpected errors.

Now, let’s learn some useful custom rules in proguard.

3.1 Keep class files

Suppose we have a data class like below:

And we don’t want proguard to tamper with it. We can do the following:

Keep will keep this class ignored when proguard is minified. Whether this class or its method is not used is not removed from the build after the build.

We can also use -keep in the proguard-rules.pro file to manage no different from Keep above. One thing that using -keep is better than Keep is that we have better control, which methods to keep and which methods to keep.

In addition to the two ways above, the only thing we want to do is to keep a single property of the class, we can use an anotation of the Gson library as follows:

3.2 Keep member for a class

In this case we want to keep our class members from getting messed up and narrowed down. In the proguard-rule.pro file add:

Assuming that with the User class above, we want to keep all public members of its class, then we do the following:

3.3 Keeping names of the class and members

Let’s say we want to keep the class name and its methods if they are called, by adding the following line to the proguard rules file:

Let’s take a look at a specific example as below:

If the methods in GlideModule are not used they are discarded. As for the rest of the methods, they do not need to be tampered with or renamed.

3.4 Using any Library in Android

When using any library we need to add some proguard statements required of that library to avoid some error cases. For example, for Glide library, they will provide us some rules like this:

For some libraries that regularly display warnings or logcat, we can fix it by adding some custom rules:

We can write any other rules for the library as we would for our class.

3.5 Only Obfuscate your project

In some small cases we just want to scramble our code against decompilation. Also don’t want proguard to do shrink and optimize it. We can completely solve the problem by adding the proguard rule file to the script as follows:

3.6 Maintaining annotations

While building the app, the annotations will be all removed and our app is still running fine. However, in some cases you want to keep these annoutations, there is still a way:

3.7 Optimization

After writing some commands in Proguard, we may need to provide an extra layer of optimization for our app. First, need to update the build.gradle file as below:

Usually, we don’t need this option. However in some cases we have to add it to increase the number of cycles during optimization. For example: We want to check if the optimization is working properly, otherwise it will re-optimize a certain number of times.

With the above code, the optimization will happen 5 times to make it more optimal.

With the above code, the final classes will be optimized up to 5 times and it can end sooner if the optimization is done. Similarly, if you want to optimize private fields, we could do the following:

  • The optimization process will for the most part be done first.

The exception, if we don’t want to run the optimize in our app:

The optimization of the Proguard file will make our application safer and lighter.

4. Important Notes

  • We should not use MainFragment :: class.java.simpleName to set the TAG for a fragment. Because in the process of proguard messing up the code, 2 fragments of the same name in two different packages can be renamed to A.class, for example. This will result in these 2 fragments carrying the same TAG. So may lead to errors.
  • Always keep the mapping file of Proguard to keep track of the old code. You may have to upload it in different places like the PlayStore Console for monitoring.

Refer:

Share the news now

Source : Viblo