-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
com.google.gson.JsonIOException: Abstract classes can't be instantiated! Register an InstanceCreator or a TypeAdapter for this type. #2379
Comments
Have you also updated Gson, or is the Gson version the same? Is Could you please try to create a minimal, reproducible example? |
I think this issue was caused by the latest update of gradle (8.0), which by default is R8 full mode. |
Marcono1234: I have updated latest gson version 2.10.1 |
Please check for JDK 17 support |
I add "android.enableR8.fullMode=false" on gradle.properties and the error was gone. |
@SearchDream Same here, adding the line got rid of the exact same error. Thank you ! |
@SearchDream @cdiazc Missing classes detected while running R8. Please add the missing classes or apply additional keep rules that are generated in D:****\app\build\outputs\mapping\release\missing_rules.txt. |
I also notice this kind of error, please check: https://stackoverflow.com/questions/70037537/proguard-missing-classes-detected-while-running-r8-after-adding-package-names-in |
I have the same error with R8 (full mode) when I updated the AGP to version 8.0 |
@pandelisgreen13 did you got any solution for it ? |
First applied recommendations from here, though they didn't help. What helped is replacing
with
|
@kzotin
|
Same issue for here too on release builds. The above mentioned fixes didn't worked . What worked was switch back to Gradle 7.5 for now. |
@jaison043 -keepclassmembers,allowobfuscation class * { |
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
|
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 #2379 (comment) 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 |
This rule worked perfectly for me, thanks! |
It's worked for me, thanks! |
Side note: If not using the @SerializedName annotation, using:
will remove the error but not fix the issue since data object members will still be null. So you need to add:
simply replace MyDataClass with your data class, this is mentioned here R8 FAQ In my case, I had to include both. |
This worked for us:
It will only work though if you're deserializing a class that has at least 1 field annotated with |
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
|
any update for R8 issue? 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 |
For gradle 8.0 with Android Studio Giraffe | 2022.3.1 Patch 2 worked for me OR android.enableR8.fullMode=false add this to gradle.properties. |
This worked with AGP 8.0 with the defaults in android.enableR8.fullMode |
@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 helped me to fix the issue. Hope it helps someone after staying the whole day trying several solutions. |
above methods didn't work for me.
|
@akardas16, what you are experiencing seems to be unrelated to this R8 issue. According to the exception, you are trying to deserialize |
This worked for me thanks! |
Having to add
when setting |
In My case, I have used @keep annotation for data classes, but the import was wrong It should Import - Additionally i already have these in my proguard rules.
This solved my issue. |
Gson 2.11.0 has been released which includes default ProGuard / R8 configuration. If your classes use Can you please try Gson 2.11.0 and let us know if that solves the issue for you? |
libs.versions - Copy.toml.txt Thanks, I tried it, but it did not help for my project. I still require the below.
if I comment the above completely, I get
if I use this:
I get
|
@coinzdude, you should probably report that to the R8 maintainers (and make sure you are using the latest R8 version), and maybe edit your comment and link to the bug report here then. So far all reports here were about successful builds, but runtime issues when using Gson. |
That makes sense to me, now. Thanks! I'll also try to build serial just to verify for play store release build. |
@kzotin Thanks for your comment. Works for my case as well. Wanted to understand why it was not working with guidelines mention in https://github.com/google/gson/blob/main/Troubleshooting.md#-jsonioexception-abstract-classes-cant-be-instantiated-r8 |
@mwshubham, which Gson version are you using? And does your class have a no-args constructor? For Gson 2.11.0 the |
This explains. We don't have classes with no-args construtor with gson 2.11.0. And hence keep is required. |
I also faced same issue after AGP upgrade to 8.7. I updated my data classes properties with @SerializedName("propertyName"). It solved my issue. |
Gson version
'2.10.1'
Java / Android version
JDK 17
Android Android Studio Flamingo | 2022.2.1
Used tools
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
The text was updated successfully, but these errors were encountered: