java - 迁移到androidx后的抽象方法错误
问题描述
Logcat 错误
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 后会发生此错误。库已更新。
解决方案
我能够通过更新 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
推荐阅读
- python - 复制带有附件的 Smartsheet 行
- clojure - 如何使用 0 元和 2 元案例编写自定义归约函数
- pandas - 按特殊字符拆分数据框列名
- jquery - 复杂的 json 数据输出“未定义”值
- c++ - 局部变量的地址是 constexpr 吗?
- javascript - 选择/打开新下拉列表时自动关闭下拉列表(React)
- python - 如何使用循环向下迭代井字游戏中的一列以查看所有垂直值是否相同?
- recursion - 递归替换所有文件中的字符串
- php - Stripe js UI 未在 IOS PWA 上显示
- java - 如何对具有异常条件的多个字段进行排序