英文:
Google Gson preserve generic signatures
问题
Crashes in Firebase Crashlytics appear with a note how to fix a problem:
Fatal Exception: java.lang.IllegalStateException: TypeToken must be created with a type argument: new TypeToken<...>() {}; When using code shrinkers (ProGuard, R8, ...) make sure that generic signatures are preserved.
My generic class between angle brackets <...> is named ApiResponse. I created it with jsonschema2pojo.
英文:
Crashes in Firebase Crashlytics appear with a note how to fix a problem:
Fatal Exception: java.lang.IllegalStateException: TypeToken must be created with a type argument: new TypeToken<...>() {}; When using code shrinkers (ProGuard, R8, ...) make sure that generic signatures are preserved.
My generic class between angle brackets <...> is named ApiResponse. I created it with jsonschema2pojo.
答案1
得分: 4
以下是翻译好的内容:
需要对Gson进行序列化/反序列化类的排除。对于包含您的类的包,看起来像这样
# 将在Gson上序列化/反序列化的应用程序类
-keep class com.myapplication.model.api.** { *; }
还需添加以下内容
# Gson在处理字段时使用存储在类文件中的通用类型信息。
# Proguard默认情况下会删除这样的信息,要保留它。
-keepattributes Signature
# 这也是R8兼容模式下所需的,因为多个优化将删除通用签名,如类合并和参数删除。
-keep class com.google.gson.reflect.TypeToken { *; }
-keep class * extends com.google.gson.reflect.TypeToken
# 可选的。用于使用GSON @Expose注解
-keepattributes AnnotationDefault,RuntimeVisibleAnnotations
英文:
An exclusion for serialized/deserialized classes over Gson is needed. For a package containing your classes looks like this
# Application classes that will be serialized/deserialized over Gson
-keep class com.myapplication.model.api.** { *; }
Also add this
# Gson uses generic type information stored in a class file when working with
# fields. Proguard removes such information by default, keep it.
-keepattributes Signature
# This is also needed for R8 in compat mode since multiple
# optimizations will remove the generic signature such as class
# merging and argument removal. See:
# https://r8.googlesource.com/r8/+/refs/heads/main/compatibility-faq.md#troubleshooting-gson-gson
-keep class com.google.gson.reflect.TypeToken { *; }
-keep class * extends com.google.gson.reflect.TypeToken
# Optional. For using GSON @Expose annotation
-keepattributes AnnotationDefault,RuntimeVisibleAnnotations
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论