gson 迁移到androidx后出现抽象方法错误

628mspwn  于 2022-11-06  发布在  Android
关注(0)|答案(1)|浏览(127)

日志目录错误

java.lang.AbstractMethodError: abstract method "java.lang.Object com.google.gson.TypeAdapter.read(com.google.gson.stream.JsonReader)"
        at com.google.gson.TypeAdapter$1.read(SourceFile:199)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(SourceFile:131)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(SourceFile:222)
        at com.google.gson.Gson.fromJson(SourceFile:927)
        at com.google.gson.Gson.fromJson(SourceFile:892)
        at com.google.gson.Gson.fromJson(SourceFile:841)
        at com.google.gson.Gson.fromJson(SourceFile:813)

我用过的一些库可能与

implementation 'com.google.android.gms:play-services-location:17.1.0'
implementation "com.google.android.gms:play-services-gcm:17.0.0"
implementation "com.google.android.gms:play-services-auth:18.1.0"
implementation "com.google.android.gms:play-services-analytics:17.0.0"
implementation("com.google.guava:guava:30.0-android")
implementation "com.squareup.retrofit2:retrofit:2.9.0"
implementation "com.squareup.retrofit2:converter-gson:2.9.0"
implementation "com.squareup.retrofit2:adapter-rxjava2:2.9.0"
implementation 'com.google.firebase:firebase-crashlytics:17.2.2'
implementation 'com.google.firebase:firebase-analytics:18.0.0'

这是导致错误的代码行

test.add(gson.fromJson(arr.getJSONObject(i).toString(), Test.class));

迁移到androidx后会发生此错误。库已更新。

fivyi3re

fivyi3re1#

我能够通过更新ProGuard规则修复此问题。
添加了以下内容。

-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

相关问题