Kotlin 1.6.0 (1.5.21 没有问题) + Proguard + Gson (Registering an InstanceCreator with Gson...)

Kotlin 1.6.0 (There are no problems in 1.5.21) + Proguard + Gson (Registering an InstanceCreator with Gson...)

我启用了 Proguard 并配置了规则

            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'

我有这个错误 -> 向 Gson 注册这个类型的 InstanceCreator 可能会解决这个问题。 切换到 kotlin 1.6.0 和 1.6.10 时发生错误。 Kotlin 1.5.21版本没有报错!

错误(我只在 Kotlin 1.6.0/1.6.10 上有错误。在 kotlin 1.5.21 上没有错误)--->

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.project.main, PID: 2943
java.lang.RuntimeException: Unable to invoke no-args constructor for class com.project.main.domain.entity.MyEntityClass. Registering an InstanceCreator with Gson for this type may fix this problem.
    at com.google.gson.internal.c$e.a(SourceFile:228)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(SourceFile:212)
    at com.google.gson.Gson.h(SourceFile:963)
    at com.google.gson.Gson.j(SourceFile:928)
    at com.google.gson.Gson.l(SourceFile:877)
    at com.project.main.data.cache.converter.MyEntityClassConverter.toMyEntityClass(SourceFile:14)
    at com.project.main.data.cache.dao.MyProjectDao_Impl.call(SourceFile:947)
    at com.project.main.data.cache.dao.MyProjectDao_Impl.call(SourceFile:889)
    at k1.c$a$a$a$a.invokeSuspend(SourceFile:128)
    at uh.a.resumeWith(SourceFile:33)
    at uk.z0.run(SourceFile:106)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
    at java.lang.Thread.run(Thread.java:923)
 Caused by: java.lang.UnsupportedOperationException: Abstract class can't be instantiated! Class name: com.project.main.domain.entity.MyEntityClass
    at com.google.gson.internal.l.a(SourceFile:120)
    at com.google.gson.internal.l$a.c(SourceFile:49)
    at com.google.gson.internal.c$e.a(SourceFile:225)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(SourceFile:212) 
    at com.google.gson.Gson.h(SourceFile:963) 
    at com.google.gson.Gson.j(SourceFile:928) 
    at com.google.gson.Gson.l(SourceFile:877) 
    at com.project.main.data.cache.converter.MyEntityClassConverter.toMyEntityClass(SourceFile:14) 
    at com.project.main.data.cache.dao.MyProjectDao_Impl.call(SourceFile:947) 
    at com.project.main.data.cache.dao.MyProjectDao_Impl.call(SourceFile:889) 
    at k1.c$a$a$a$a.invokeSuspend(SourceFile:128) 
    at uh.a.resumeWith(SourceFile:33) 
    at uk.z0.run(SourceFile:106) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) 
    at java.lang.Thread.run(Thread.java:923) 

我有这个错误的规则。 -保持 class com.project.main.domain.entity.MyEntityClass { ; } 但我要添加整个包裹 -保持 class com.project.main.domain.entity.* { *; }

我还有 GSON 的所有其他规则 -> rules

  @TypeConverter
  fun toMyEntityClass(value: String): MyEntityClass = 
gson.fromJson(value, object : TypeToken<MyEntityClass>() {}.type) //Error

我也试过只规定一个规则。该规则有助于 运行 1.5.21 上的项目并且有效。但它不适用于 1.6.0 - 1.6.10。我不想创建一个 Instance Creator。我的模型太多,会浪费很多时间。

-keep class ** { *; }

对于任何与 GSON 交互的 类 你必须遵守以下内容:

-keep class "MODEL CLASSES"

对于您的问题,您可以这样做:

-keep class com.project.main.domain.entity.** { *; }

它将保留所有 类 在实体包下。

问题自行解决,更新库后 GSON 2.9.0 和科特林 2.6.21 我什么都没做,只是更新到这些版本 我没有检查 1.6.0 和 1.6.10 是否与 gson 2.9.0 协同工作,但 gson 不适用于以前的发行版本 结果,我升级了 Kotlin 从 1.5.21 -> 1.6.21