Search code examples
javaandroidgsonretrofitandroidx

Abstract method error after migrating to androidx


Logcat error

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)
        

Some libraries I've used that might be related

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'

This is the line of code that cause the error

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

This error happens after migrating to androidx. The libraries are updated.


Solution

  • I was able to fix the issue by updating the ProGuard rules.

    added the following.

    -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