java - 使用 proguard 时 Gson 嵌套类为空

标签 java android gson proguard dexguard

警告:类 'com.google.gson.internal.bind.ReflectiveTypeAdapterFactory' 在类上调用 Class.getDeclaredFields。

启用 proguard 后发现了以下提到的一些问题:

  • 嵌套的静态类为空。
  • 类中自定义对象的列表为空

  • 已经引用了以下错误,但没有运气:

    Proguard issue while using GSON
    Using GSON with proguard enabled

    最佳答案

    我可以通过对我的 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 的原因我遇到的问题 - 当然,在我完成上述步骤之后。

    关于java - 使用 proguard 时 Gson 嵌套类为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54880395/

    相关文章:

    java - 验证上传的文件与下载的文件是否相同

    java - 如何在不创建额外实例变量的情况下创建对象数组?

    android - 从 android Geocoder 获取波斯语信息

    java - 使用 Android-M 数据绑定(bind)应用的单行字体不起作用

    java - 使用GSON解析JSON得到 "content"

    java - 如何使用 Java 从 TCP 发送和接收 RST 标志?

    java - "nested exception is java.sql.SQLException: Incorrect syntax near '? ' "

    java - 如何为 Android Google Analytics 添加页面标题参数

    java - Json 输出生成双引号中的数字,如字符串

    java - 使用gson解析json出现空指针异常