The best practice of Proguard Annotations in Java Library
The best practice of Proguard Annotations in Java Library
When developing the Java class library, we often need to ensure that the code of the library is optimized and confused when it is released to protect intellectual property rights and reduce the size of the code.Proguard is a very powerful tool that helps us achieve these goals.However, in order to ensure that Proguard can correctly optimize and confuse the code of libraries, we need to use some special annotations to provide additional information.This article will introduce the best practice using Proguard Annotations in the Java library and provide some example code to help understand.
1. What is Proguard Anotations?
Proguard Annotions is a set of special annotations to provide additional optimization and confusion information to Proguard.These annotations are defined by the Proguard tool and used in the code of the library.Proguard will read these annotations in the process of optimization and confusion, and make corresponding optimization and confusion decisions based on the information provided.
Here are some common proguard annotations:
-@Keep: It is used to indicate that Proguard keeps a class, method or field without being optimized or confused.
-@KeepClassMembers: It is used to indicate that Proguard retains members of a class without being optimized or confused.
-@Keepname: It is used to indicate that Proguard retains a class, method or field name without being confused.
-@KeepPUBLICLASSMEMBERS: It is used to indicate that Proguard retains a class of public members without being optimized or confused.
-@KeepPUBLICGetters: It is used to indicate that Proguard retains a type of public getter method without being optimized or confused.
-@KeepPublicSetters: It is used to indicate that Proguard retains a type of public setter method without being optimized or confused.
Second, the best practice of using Proguard Annotations
1. Choose the right annotation
When using Proguard Annotations, first of all, you need to determine which annotations to be used.According to the characteristics and needs of the library, choose appropriate annotations to provide necessary optimization and confusion information.For example, if you have some important classes, methods, or fields, you can use @Keep annotations to ensure that they are not optimized or confused.
2. Add annotation to the code
Once the annotations to be used are determined, they need to be added to the code.Note can be used on class, methods, or fields to provide corresponding information.The following is an example:
@Keep
public class MyClass {
@Keep
private String myField;
@Keep
public void myMethod() {
// do something
}
}
In this example, the@Keep annotation has been added to the MyClass class, MyField field, and MyMethod method.This will tell Proguard to keep these elements without optimization or confusion.
3. Configure the PROGUARD rule
During the construction process, Proguard needs to read the annotations in the code and make corresponding optimization and confusion according to the information of the annotation.In order to make Proguard properly handle annotations, we need to add some rules to the Proguard configuration file.The following is an example:
-keep @com.example.annotations.Keep
-keepclassmembers class * {
@com.example.annotations.Keep <fields>;
@com.example.annotations.Keep <methods>;
}
In this example, we used @Keep annotations and told Proguard to retain the class and members using this annotation.
4. Construction and test
Once the Proguard rules are configured, we can build and test library code.Make sure that PROGUARD is enabled during the construction process and appropriate configuration files are used.Run the test case to ensure that the code is still working normally, and it has been optimized and confused by expected.
in conclusion
Using Proguard Annotations can help us achieve the goals of optimization and confusion when developing the Java class library.By selecting the appropriate annotation and adding it to the code, we can provide additional information and allow Proguard to optimize and confuse the processing based on this information.Keep in mind that when using the Proguard Annotations, you need to configure the appropriate Proguard rule and test to ensure that the code is still working properly.
I hope this article can help you understand the best practice of using Proguard Annotations in the Java class library.If you have any questions or need further help, please contact us at any time.
references:
-Loguard official document: https://www.guardsquare.com/manual/configuration/examples