As we all know, if the obfuscation switch is turned on, the code release phase is confused according to the Proguard rule, but some entity classes (such as the model for JSON strings) need to be serialized and deserialized, while the serialization tool (for example, Gson, Fastjson) is a member variable that uses reflection to one by one the key and entity classes corresponding to the JSON string.
For example, we define a User entity class of type POJO:
publicclass User { public String firstName; public String lastName; publicint age;}
Then the corresponding JSON string content is as follows:
{ "firstName":"fei", "lastName":"liangfei", "age":28}
If the User class is confused (the possibility of a member variable being deleted or the name is removed because it is not used), the serialization tool is powerless because it cannot find the corresponding relationship between the JSON key and the member variable (for example, User.firstname = "FirstName").
The general practice is to keep member variables in the Proguard file User
:
-keepclassmembernames User.**
But when multiple teams work together and the code is scattered across project, adding an entity class each time requires modifying the Proguard file, and if more than one person modifies it at the same time, it is likely to conflict, so we have to think of a way to do it once and for all.
There must be a lot of methods, Fresco use Annotation- @DoNotSkip
The practice is still more ingenious.
The definition is defined first @DoNotSkip
(the function is explained in detail in the note):
/** * Add this annotation to a class, method, or field to instruct Proguard to not strip it out. * * This is useful for methods called via reflection that could appear as unused to Proguard. */@Target({ ElementType.TYPE, ElementType.FIELD, ElementType.METHOD, ElementType.CONSTRUCTOR })@Retention(CLASS)public @interface DoNotStrip {}
Then add the following rule in the Proguard file:
# Keep our interfaces so they can is used by other Pr Oguard rules.# See Http://sourceforge.net/p/proguard/bugs/466/-keep, Allowobfuscation @interface com.facebook.common.internal.donotstrip# do not Strip any method/class that is annotated With @donotstrip -keep @com . Facebook.common.internal.DoNotStrip class * - Keepclassmembers class * { @com . Facebook.common.internal.DoNotStrip *;}
In this way, we User
@DoNotSkip
will not be confused as long as we add.
@DoNotSkippublicclass User { }
@DoNotSkip
the definition of this can be known by its target:
@Target({ ElementType.TYPE, ElementType.FIELD, ElementType.METHOD, ElementType.CONSTRUCTOR })
So we can also @DoNotSkip
use the member variable (FIELD), method, and construction method (Constctuctor) In addition to the class (type).
@Keep
In fact, we can not define this annotation, because the Android support-annotations package provides a @DoNotSkip
annotation with similar- @Keep
but at this stage we still need to add in Proguard at once Rules that may not be needed in the future.
We ' ve also added @Keep to the support annotations. Note however that this annotation hasn ' t been hooked up to the Gradle plugin yet (though it's in progress.) When finished this would let you annotate methods and classes that should is retained when minimizing the app.
Fresco source parsing-use @DoNotSkip to prevent confusion