有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!


共 (1) 个答案

  1. # 1 楼答案

    我可以通过对我的proguard-rules.pro文件执行三项操作来解决此问题:

    首先,确保ProGuard不会更改用Gson序列化的任何自定义类的名称。假设您有一个名为Classname的类。若要豁免,请将此项添加到progaurd-rules.pro

    -keepclassmembernames class com.your.package.classname { <fields>; }
    

    (用实际的包和类名替换com.your.package.classname

    我得上十几节课。别忘了豁免这些类的任何自定义成员变量。用classname$innerclass而不是classname豁免内部类

    第二步,添加Gson库建议的规则They can be found here.在这里,他们是作为书面:

    ##       -Begin: proguard configuration for Gson       
    # 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
    
    # For using GSON @Expose annotation
    -keepattributes *Annotation*
    
    # Gson specific classes
    -dontwarn sun.misc.**
    #-keep class com.google.gson.stream.** { *; }
    
    # Application classes that will be serialized/deserialized over Gson
    -keep class com.google.gson.examples.android.model.** { <fields>; }
    
    # Prevent proguard from stripping interface information from TypeAdapter, TypeAdapterFactory,
    # JsonSerializer, JsonDeserializer instances (so they can be used in @JsonAdapter)
    -keep class * extends com.google.gson.TypeAdapter
    -keep class * implements com.google.gson.TypeAdapterFactory
    -keep class * implements com.google.gson.JsonSerializer
    -keep class * implements com.google.gson.JsonDeserializer
    
    # Prevent R8 from leaving Data object members always null
    -keepclassmembers,allowobfuscation class * {
      @com.google.gson.annotations.SerializedName <fields>;
    }
    
    # Retain generic signatures of TypeToken and its subclasses with R8 version 3.0 and higher.
    -keep,allowobfuscation,allowshrinking class com.google.gson.reflect.TypeToken
    -keep,allowobfuscation,allowshrinking class * extends com.google.gson.reflect.TypeToken
    
    ##       -End: proguard configuration for Gson       
    

    最后,添加以下两条规则:

    -dontwarn java.lang.reflect.**
    -keep class kotlin.** { *; }
    

    当然,在我完成了上述步骤之后,这些就是我修复嵌套的null问题的方法