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?
ProGuard can't obfuscate strings. xml. You can use other software to obfuscate your file like DexGuard. You can get more info here.
By default, ProGuard obfuscates the code: it assigns new short random names to classes and class members. It removes internal attributes that are only useful for debugging, such as source files names, variable names, and line numbers.
In the obfuscation step, ProGuard renames classes and class members that are not entry points. In this entire process, keeping the entry points ensures that they can still be accessed by their original names. The preverification step is the only step that doesn't have to know the entry points.
Try this:
-keepattributes *Annotation*
-keepattributes Signature
-dontnote sun.misc.**
-keep class * implements com.google.gson.TypeAdapterFactory
-keep class * implements com.google.gson.JsonSerializer
-keep class * implements com.google.gson.JsonDeserializer
Actually, there is a proguard config in official repo on github https://github.com/google/gson/blob/master/examples/android-proguard-example/proguard.cfg
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
-keep @package.annotationclassname public class *
The rule
-keep class com.google.gson.annotations.*
will keep all annotations in the package com.google.gson.annotations including the SerializedName and Expose ones that you have used.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With