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
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
with
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
This rule worked perfectly for me, thanks!
@kzotin Thanks for update for me I need to add all below lines.
@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.
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
not sure if needed, but I decorated all my anonymous class properties too
We have added a section to the Troubleshooting Guide which hopefully helps with this:
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 usingdisableJdkUnsafe()will then throw a different exception saying “Unable to create instance of …”.