Proguard 混淆注解

Proguard obfuscating Annotations

我需要保持所有模型 类 不被混淆,所以我在 proguard 规则中添加了这一行以保持所有模型 类:

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

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

-keepattributes *Annotation*
-keepattributes EnclosingMethod

但是,结果还是一样。我的模型 类 包含这两个注释:

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

如何使这两个注释不被混淆?

-保持@package.annotationclassnamepublicclass*

添加到您的混淆器中:它防止特定的 classes 被混淆。

  1. 必填:-dontshrink.https://www.guardsquare.com/en/proguard/manual/usage
  2. 非强制性:尝试使用 -dontoptimize
  3. 我不太明白这个问题,你指的是字段还是内部 class ? 如果在 class 中有一个内部 class,则需要指定其内部字段。 例如,如果这是 class :

    public class Parent {
    
    protected Child child;
    
    protected class Child {
        private String name;
        private int age;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    }
    

    }

在proguard中,需要指定Child class :

-keep class com.package.name.Parent$Child {*; }

Gson 在处理字段时使用存储在 class 文件中的通用类型信息。 Proguard 默认会删除此类信息,因此请将其配置为保留所有信息。

正在尝试添加

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

使用 GSON @Expose 注解

-keepattributes *Annotation*

对于 Gson 特定 classes

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

防止混淆器从 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

您可能需要在您的项目中创建一个名为 DontObfuscate 的注解 更多检查这个 Managing obfuscation with annotations

试试这个:

-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

实际上,在 github https://github.com/google/gson/blob/master/examples/android-proguard-example/proguard.cfg

的官方仓库中有一个 proguard 配置

规则

-keep class com.google.gson.annotations.*

将保留包 com.google.gson.annotations 中的所有注释,包括您使用过的 SerializedName 和 Expose。