gson: com.google.gson.JsonIOException: Abstract classes can't be instantiated! Register an InstanceCreator or a TypeAdapter for this type.

Gson version

‘2.10.1’

Java / Android version

JDK 17 Android Android Studio Flamingo | 2022.2.1

Used tools

  • Maven; version:
  • Gradle; version:
  • ProGuard (attach the configuration file please); version:

Description

When we convert json string to Model class it gives error related to adapter. I just updated android studio to latest version it works in older version.

Expected behavior

It should convert json string to Model class.

Actual behavior

It throws an error.

Reproduction steps

Exception stack trace

W  com.google.gson.JsonIOException: Abstract classes can't be instantiated! Register an InstanceCreator or a TypeAdapter for this type. Class name: com.xxx.xxx.data.pojo.other.GeneralResponse
2023-04-24 16:30:49.897 23084-23084 System.err              com.xxx.xxx                  W  	at com.google.gson.internal.ConstructorConstructor$3.construct(ConstructorConstructor.java:136)
2023-04-24 16:30:49.897 23084-23084 System.err              com.xxx.xxx                  W  	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$FieldReflectionAdapter.createAccumulator(ReflectiveTypeAdapterFactory.java:427)
2023-04-24 16:30:49.897 23084-23084 System.err              com.xxx.xxx                  W  	at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:383)
2023-04-24 16:30:49.898 23084-23084 System.err              com.xxx.xxx                  W  	at com.google.gson.Gson.fromJson(Gson.java:1227)
2023-04-24 16:30:49.898 23084-23084 System.err              com.xxx.xxx                  W  	at com.google.gson.Gson.fromJson(Gson.java:1137)
2023-04-24 16:30:49.898 23084-23084 System.err              com.xxx.xxx                  W  	at com.google.gson.Gson.fromJson(Gson.java:1047)
2023-04-24 16:30:49.898 23084-23084 System.err              com.xxx.xxx                  W  	at com.google.gson.Gson.fromJson(Gson.java:982)
2023-04-24 16:30:49.898 23084-23084 System.err              com.xxx.xxx                  W  	at com.xxx.xxx.data.repository.BaseRepository.checkSuccess(BaseRepository.kt:8)
2023-04-24 16:30:49.898 23084-23084 System.err              com.xxx.xxx                  W  	at com.xxx.xxx.data.repository.ApiRepository.login(ApiRepository.kt:51)
2023-04-24 16:30:49.898 23084-23084 System.err              com.xxx.xxx                  W  	at com.xxx.xxx.data.repository.ApiRepository$login$1.invokeSuspend(ApiRepository.kt:0)
2023-04-24 16:30:49.898 23084-23084 System.err              com.xxx.xxx                  W  	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
2023-04-24 16:30:49.898 23084-23084 System.err              com.xxx.xxx                  W  	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
2023-04-24 16:30:49.898 23084-23084 System.err              com.xxx.xxx                  W  	at android.os.Handler.handleCallback(Handler.java:938)
2023-04-24 16:30:49.899 23084-23084 System.err              com.xxx.xxx                  W  	at android.os.Handler.dispatchMessage(Handler.java:99)
2023-04-24 16:30:49.899 23084-23084 System.err              com.xxx.xxx                  W  	at android.os.Looper.loop(Looper.java:223)
2023-04-24 16:30:49.899 23084-23084 System.err              com.xxx.xxx                  W  	at android.app.ActivityThread.main(ActivityThread.java:7656)
2023-04-24 16:30:49.899 23084-23084 System.err              com.xxx.xxx                  W  	at java.lang.reflect.Method.invoke(Native Method)
2023-04-24 16:30:49.899 23084-23084 System.err              com.xxx.xxx                  W  	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
2023-04-24 16:30:49.899 23084-23084 System.err              com.xxx.xxx                  W  	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

About this issue

  • Original URL
  • State: open
  • Created a year ago
  • Reactions: 1
  • Comments: 29

Most upvoted comments

I add “android.enableR8.fullMode=false” on gradle.properties and the error was gone.

I think this issue was caused by the latest update of gradle (8.0), which by default is R8 full mode.

First applied recommendations from here, though they didn’t help.

What helped is replacing

-keepclassmembers,allowobfuscation class * {
 @com.google.gson.annotations.SerializedName <fields>;
}

with

-keep class * {
  @com.google.gson.annotations.SerializedName <fields>;
}

The previously mentioned rules didn’t work in my project. With the arrival of AGP 8.0 and Android Studio Flamingo, the rule that solved my problem was

-if class *
-keepclasseswithmembers class <1> {
    <init>(...);
    @com.google.gson.annotations.SerializedName <fields>;
}

The previously mentioned rules didn’t work in my project. With the arrival of AGP 8.0 and Android Studio Flamingo, the rule that solved my problem was

-if class *
-keepclasseswithmembers class <1> {
    <init>(...);
    @com.google.gson.annotations.SerializedName <fields>;
}

This rule worked perfectly for me, thanks!

@kzotin Thanks for update for me I need to add all below lines.

-keepclassmembers,allowobfuscation class * {
  @com.google.gson.annotations.SerializedName <fields>;
}

-keep class * {
  @com.google.gson.annotations.SerializedName <fields>;
}
-keepnames class com.fasterxml.jackson.databind.** { *; }
-dontwarn com.fasterxml.jackson.databind.**

In some projects, This solution works and in some other projects, it does not.

@Shvet, could you please provide some more information? Are the projects where this does not work also consistently using @SerializedName on all of their fields? And are you talking about this specific “Abstract classes can’t be instantiated!” issue, or some other issues with R8 (e.g. incorrect JSON, missing field values, other exceptions…)?

@Marcono1234 Sorry for late reply, I am talking about only “Abstract classes can’t be instantiated”. AS for more infomation I can not send you sample code as I can not upload them for public.

The previously mentioned rules didn’t work in my project. With the arrival of AGP 8.0 and Android Studio Flamingo, the rule that solved my problem was

-if class *
-keepclasseswithmembers class <1> {
    <init>(...);
    @com.google.gson.annotations.SerializedName <fields>;
}

This worked for me as well. debug build works fine, release did not. I put that rule at the end of my proguard-rules.pro file

gradle:8.1.0-alpha05

                    val gson = Gson()
                    val document = gson.fromJson(response, Document::class.java) // This is the line that would crash

not sure if needed, but I decorated all my anonymous class properties too

    data class Document(
        @SerializedName("items") val items: List<Item>
    )

data class Item(
        @SerializedName("deviceId") val deviceId: String,
        @SerializedName("name") val name: String,
        @SerializedName("label") val label: String,
        @SerializedName("components") val components: List<Component>
    )

We have added a section to the Troubleshooting Guide which hopefully helps with this:

JsonIOException: ‘Abstract classes can’t be instantiated!’ (R8)

Could you please check if the entry is useful for you or if we should improve it in any way?

@kzotin’s suggestion in https://github.com/google/gson/issues/2379#issuecomment-1531602410 might work as well, but in the R8 integration tests I added it appears R8 then just does not make the class abstract anymore but might still remove all constructors. So you will be dependent on JDK Unsafe then to create instances of the class (see also GsonBuilder.disableJdkUnsafe()). Would be good if someone could verify this by checking if using disableJdkUnsafe() will then throw a different exception saying “Unable to create instance of …”.