[英]Gson Nested classes are null while using proguard
警告:類 'com.google.gson.internal.bind.ReflectiveTypeAdapterFactory' 在類上調用 Class.getDeclaredFields。
啟用 proguard 后發現了以下提到的一些問題:
已經參考了以下錯誤,但沒有運氣:
我能夠通過對proguard-rules.pro
文件做三件事來解決這個問題:
首先,確保ProGuard
不會更改您使用Gson
序列化的任何自定義類的名稱。 假設您有一個名為Classname
的類。 要豁免它,請將其添加到您的progaurd-rules.pro
:
-keepclassmembernames class com.your.package.classname { <fields>; }
(將com.your.package.classname
替換為您的實際包和類名)
我不得不為十幾節課這樣做。 不要忘記免除那些也是自定義的類的任何成員變量。 使用classname$innerclass
而不是classname
免除內部classname
。
其次,添加Gson
庫推薦的規則。 他們可以在這里找到。 以下是寫作時的情況:
##---------------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
問題的方法 - 當然,在我完成上述步驟之后。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.