Fresco source parsing-use @DoNotSkip to prevent confusion

Source: Internet
Author: User

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 { }

@DoNotSkipthe 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.