Java class libraries advanced programming guidance based on Proguard Annotations
Java Class Libraares advanced programming guidance based
Overview:
PROGUARD is a tool for code optimization and compression during the construction of the Java application.It can significantly reduce the size of the application and improve the execution efficiency by removing unused classes, fields, and methods, and can significantly reduce the volume of the application.Proguard Annotations is a set of annotations that help developers to control the behavior of Proguard during code compression and confusion.This article will introduce how to use Proguard Annotations to optimize and protect Java Class Libraries.
1. Import in Proguard Anotations dependencies
First of all, you need to add the Proguard Annotations library to the construction path of the project.You can add dependencies through Maven or Gradle.The following is the example of Gradle:
dependencies {
implementation 'org.guardsquare:proguard-annotations:7.0.1'
}
2. Use PROGUARD Annotations
1. Protect the code to avoid the confusion of PROGUARD
PROGUARD will confuse all code by default, but some categories and methods need to be retained for other modules.You can use the `@Keep` annotation to mark the classes or methods that need to be retained.For example:
@Keep
public class MyClass {
@Keep
public void myMethod() {
// Reserve method logic
}
}
2. Specify classes and members who do not confuse
Sometimes, some classes or members cannot be confused, and they can use the `@Keepname` annotation to maintain their original name.For example:
@KeepName
public class MyClass {
@KeepName
public String myField;
}
3. Prevent certain classes from being deleted
Sometimes, some classes may not be directly cited by other modules, but they are accessed by reflection or other ways at runtime.You can use the note to prevent these classes from being deleted.For example:
@KeepClassMembers
public class MyClass {
// The reserved class members
}
4. Prevent certain methods from being deleted
Similarly, you can use ``@Keepname` to prevent some methods from being deleted.For example:
public class MyClass {
@KeepName
public void myMethod() {
// Reserve method logic
}
}
3. Configure Proguard
It is very simple to configure Proguard to use Proguard Annotations.Add the following rules to the proguard configuration file (usually proguard-rules.pro):
-keepclassmembers class * {
@org.guardsquare.**Annotations* <methods>;
}
The above rules will ensure that Proguard recognition and properly handle the Proguard Annotations.
in conclusion:
Using Proguard Annotations to use the Proguard tool more efficiently to optimize and protect Java Class Libraries.By using@Keep`, `@Keepname` and`@KeepClassMembers`, you can retain the required code and classes without being deleted or confused by mistake.This is very useful for the development of Java libraries with code reuse and modular characteristics.
Reference materials:
- [Proguard Annotations official document] (https://www.guardsquare.com/manual/configuration/examples))
- [Proguard Annotations GitHub Repo](https://github.com/guardsquare/proguard-annotations)