Proguard Annotions framework use skills and precautions
Proguard Annotions framework use skills and precautions
Proguard Annotations is a framework for adding confusion rules to the Java code.It can help developers by adding annotations to the code to inform them of which classes, methods, and fields should be kept when the code is confused to ensure that the function of the code is running normally.This article will introduce the skills and precautions of Proguard Annotations and provide some Java code examples.
1. The basic knowledge of Proguard Annotations
1. Introduce dependencies
First, we need to add proguard annotations to the project construction file.In the Maven project, you can add the following configuration to the pom.xml file:
<dependency>
<groupId>net.sf.proguard</groupId>
<artifactId>proguard-annotations</artifactId>
<Version> Replace the version number to the latest version </version>
</dependency>
2. Add annotations
The Proguard Annotions framework provides a set of annotations to inform the Proguard not to confuse the code.Here are some commonly used annotations:
-`@Keep`: Used to retain the class, methods, or fields of the annotation.This annotation can be added before the class, method or field.
-`@Keepname`: The name of the scripted class is retained, but the members of the commentary class are confused.
-`@KeepPublicClassMembernames`: The names for retaining the annotated public class members are allowed to be confused.
-`@KeepClasseswithmembers`: Used to retain the names of the annotated classes and the names of its members.
-`@Keepimplementations`: The name of the implementation class of the interface of the commentary interface is used.
You can add corresponding annotations as needed.
2. PROGUARD Annotations use skills
1. Keep a specific class or method
If you want to keep a specific class or method from being confused, you can add `@Keep` in front of them.For example:
@Keep
public class MyClass {
// Reserve this method is not confused
@Keep
public void myMethod() {
// ...
}
}
2. Keep a specific class name
Sometimes, we want to retain members who are not confused.This can be achieved by adding `@Keepname`.For example:
@KeepName
public class MyImportantClass {
// ...
}
3. Reserve the name of the class membership
If you want to retain the names of the class and confuse the category, you can use the annotation of `@KeepPUBLICLASSMEMBERNAMES`.For example:
@KeepPublicClassMemberNames
public class MyClass {
// ...
}
4. Keep the entire class and its members
Sometimes, we need to keep the entire class and its members being not confused, we can use the annotation of `@KeepClasSeswithmembers`.For example:
@KeepClassesWithMembers
public class MyClass {
// ...
}
5. Keep the interface implementation class name
If you want to retain the implementation of the interface, the name of the interface is not confused, you can add the annotation of `@Keepimplementations` in front of the interface.For example:
@KeepImplementations
public interface MyInterface {
// ...
}
Third, the precautions of Proguard Annotations
1. Scope of annotation
When using Proguard Annotations, you need to select the appropriate annotation as needed and add it to the corresponding position.For example, when the entire class needs to be retained, the annotation should be added before the definition of the class; when a method in the class needs to be retained is not confused, the annotation should be added before the definition of the method.
2. Confusion rules conflict
If a certain class or member confusion rules are configured in the proguard configuration file, and the class or members will be added with the program annotations, the annotation will cover the rules in the configuration file.Therefore, when using Proguard Annotations, it is necessary to ensure that the rules of the annotation and the rules of configuration files will not cause conflicts.
3. Dependence
Before using the Proguard Annotations, you need to ensure that the project has been properly introduced.If the dependencies are incorrect or lacking, the annotation of the annotation may not be effective or the compilation error occurs.
Summarize:
Proguard Annotations framework can help developers retain specific classes, methods and fields when code confusion.Through reasonable use of related injection, the confusion effect of the code can be more flexibly controlled, thereby ensuring the normal operation of the program.
The above is a brief introduction to the techniques and precautions of the Proguard Annotations framework, and provides some examples of commonly used annotations.I hope you can help you use the Proguard Annotations.