Proguard混淆注释

Jin*_*cis 3 obfuscation android android-proguard

我需要使所有模型类保持一致,我在proguard规则中添加了这一行以保留所有模型类。

-keep class my_package_name.model.** { *; }
Run Code Online (Sandbox Code Playgroud)

该命令保留了所有模型类,但仍然混淆了模型类内部的注释。我尝试添加以下行

-keepattributes *Annotation*
-keepattributes EnclosingMethod
Run Code Online (Sandbox Code Playgroud)

但是结果仍然是相同的。我的模型类包含这两个注释

@SerializedName("message")
@Expose
private String message;
Run Code Online (Sandbox Code Playgroud)

我如何才能使两个注释保持一致?

Muz*_*ain 6

Gson 在处理字段时使用存储在类文件中的通用类型信息。默认情况下,Proguard 会删除此类信息,因此请对其进行配置以保留所有信息。

尝试添加

-keepattributes Signature
-keepattributes EnclosingMethod
-keepattributes InnerClasses
-keepattributes Annotation
Run Code Online (Sandbox Code Playgroud)

用于使用 GSON @Expose 注释

-keepattributes *Annotation*
Run Code Online (Sandbox Code Playgroud)

对于 Gson 特定类

-keep class sun.misc.Unsafe { *; }
Run Code Online (Sandbox Code Playgroud)

防止 proguard 从 TypeAdapterFactory、JsonSerializer、JsonDeserializer 实例中剥离接口信息(因此它们可以在 @JsonAdapter 中使用)

-keep class * implements com.google.gson.TypeAdapterFactory
-keep class * implements com.google.gson.JsonSerializer
-keep class * implements com.google.gson.JsonDeserializer
Run Code Online (Sandbox Code Playgroud)


Ale*_*ych 5

尝试这个:

-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
Run Code Online (Sandbox Code Playgroud)

实际上,在github https://github.com/google/gson/blob/master/examples/android-proguard-example/proguard.cfg的官方仓库中有一个proguard配置