Proguard混淆注释 [英] Proguard obfuscating Annotations
问题描述
我需要使所有模型类保持一致,因此我在proguard规则中添加了以下行以保留所有模型类:
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?
推荐答案
Gson在使用字段时会使用存储在类文件中的泛型类型信息.Proguard默认会删除此类信息,因此请将其配置为保留所有信息.
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.
尝试添加
-keepattributes Signature
-keepattributes EnclosingMethod
-keepattributes InnerClasses
-keepattributes Annotation
用于使用GSON @Expose注释
For using GSON @Expose annotation
-keepattributes *Annotation*
针对Gson特定班级
-keep class sun.misc.Unsafe { *; }
防止proguard剥离TypeAdapterFactory,JsonSerializer,JsonDeserializer实例的接口信息(因此可以在@JsonAdapter中使用它们)
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
这篇关于Proguard混淆注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!