Skip to content
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

GSON deserialization fails with ClassCastException: Cannot cast com.google.gson.internal.LinkedTreeMap to androidx.collection.ArrayMap #2655

Open
4 tasks done
nitinsethi86 opened this issue Mar 21, 2024 · 1 comment
Labels
bug needs-info proguard-r8 Issues relating to the use of ProGuard and/or R8, such as problems due to obfuscation

Comments

@nitinsethi86
Copy link

nitinsethi86 commented Mar 21, 2024

Gson version

2.8.6

Java / Android version

JDM 17 and All Android versions.

Used tools

  • Maven; version: N/A
  • Gradle; version: 8.3
  • ProGuard (attach the configuration file please); version: 8.2.47
  • ...

Description

We recently moved from AGP 8.0.2 to AGP 8.2.2.

We use gson version 2.8.6 with below set of proguard rules:

GSON 2.2.4 specific rules

Gson uses generic type information stored in a class file when working with fields. Proguard

removes such information by default, so configure it to keep all of it.

For using GSON @expose annotation

-keepattributes EnclosingMethod

Gson specific classes

-keep class sun.misc.Unsafe { ; }
-keep class com.google.gson.stream.
* { *; }

Prevent R8 from leaving Data object members always null

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

Retain generic signatures of TypeToken and its subclasses with R8 version 3.0 and higher.

-keep,allowobfuscation,allowshrinking class com.google.gson.reflect.TypeToken
-keep,allowobfuscation,allowshrinking class * extends com.google.gson.reflect.TypeToken

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

After upgrade, we have started seeing ClasscastException like below:

java.lang.ClassCastException: Cannot cast com.google.gson.internal.LinkedTreeMap to androidx.collection.ArrayMap

We can fix it either by using Map as TypeToken or moving to 8.0.77 R8 version.

Is this a known issue? Do we need to update the R8 rules? I tried both the latest versions of R8 in 8.2, 8.1 series and both of them don't work. I tried the latest rules from Gson too. It didn't work.

Expected behavior

No exception is thrown and deserialization happens

Actual behavior

The classcastexception is thrown.

Reproduction steps

  1. ...
  2. ...

Exception stack trace

java.lang.ClassCastException: Cannot cast com.google.gson.internal.LinkedTreeMap to androidx.collection.ArrayMap


@Marcono1234
Copy link
Collaborator

I tried the latest rules from Gson too. It didn't work.

Did you try the rules from https://github.com/google/gson/blob/main/gson/src/main/resources/META-INF/proguard/gson.pro? They will also be included automatically in the next Gson release.

A few questions:

  • This only happens for builds with R8 enabled; if R8 is disabled this does not occur, right?
    (I assume yes since you mention that downgrading R8 also solves this.)
  • Can you please try the latest Gson version 2.10.1 and see if that makes a difference?
  • Could you please share the code snippet (or ideally a small self-contained example) showing the code which throws the exception?
    The ClassCastException can also have different causes, see the Troubleshooting Guide, and maybe your AGP upgrade is now triggering this for some reason (?).

@Marcono1234 Marcono1234 added needs-info proguard-r8 Issues relating to the use of ProGuard and/or R8, such as problems due to obfuscation labels Mar 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug needs-info proguard-r8 Issues relating to the use of ProGuard and/or R8, such as problems due to obfuscation
Projects
None yet
Development

No branches or pull requests

2 participants