Search code examples
androidproguardobfuscation

Proguard obfuscating Annotations


I need to keep all model classes to be unobfuscated, so I added this line in proguard rules to keep all model classes:

-keep class my_package_name.model.** { *; }

All model classes are getting kept by this command but still, it is obfuscating the annotations inside the Model classes. I tried adding the following line:

-keepattributes *Annotation*
-keepattributes EnclosingMethod

But still, results are same. My model classes contain these two annotations:

@SerializedName("message")
@Expose
private String message;

How can I keep the two annotations unobfuscated?


Solution

  • Gson uses generic type information stored in a class file when working with fields. Proguard removes such information by default, so configure it to keep all of it.

    Trying adding

    -keepattributes Signature
    -keepattributes EnclosingMethod
    -keepattributes InnerClasses
    -keepattributes Annotation
    

    For using GSON @Expose annotation

    -keepattributes *Annotation*
    

    For Gson specific classes

    -keep class sun.misc.Unsafe { *; }
    

    Prevent proguard from stripping interface information from TypeAdapterFactory,JsonSerializer, JsonDeserializer instances (so they can be used in @JsonAdapter)

    -keep class * implements com.google.gson.TypeAdapterFactory
    -keep class * implements com.google.gson.JsonSerializer
    -keep class * implements com.google.gson.JsonDeserializer