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

IllegalStateException: TypeToken must be created when using GSON 2.10.1 and R8 on Android #2646

Open
snijsure opened this issue Mar 13, 2024 · 3 comments
Labels
bug proguard-r8 Issues relating to the use of ProGuard and/or R8, such as problems due to obfuscation

Comments

@snijsure
Copy link

snijsure commented Mar 13, 2024

Gson version

2.10.1

Java / Android version

Java 17, Android 34

Description

I just updated my Android project to use latest GSON library 2.10.1 and I am getting following exception, when using this library on Android project with AGP 8.1 and R8 enabled.

Caused by: java.lang.IllegalStateException: TypeToken must be created with a type argument: new TypeToken<...>() {}; When using code shrinkers (ProGuard, R8, ...) make sure that generic signatures are preserved.

Per this stackoverflow thread I need to add additional rules -- https://stackoverflow.com/questions/76224936/google-gson-preserve-generic-signatures

Perhaps a naive question -- shouldn't these rules be packaged with the library?

As consumer of GSON library, do I still need to explicitly include rules specified here in the file below, as far as I can tell those changes are merged.

https://github.com/google/gson/blob/main/gson/src/main/resources/META-INF/proguard/gson.pro

@snijsure snijsure added the bug label Mar 13, 2024
@snijsure snijsure changed the title .IllegalStateException: TypeToken must be created when using GSON 2.10.1 .IllegalStateException: TypeToken must be created when using GSON 2.10.1 and R8 on Android Mar 13, 2024
@snijsure
Copy link
Author

Never mind found android documentation

@Marcono1234
Copy link
Collaborator

shouldn't these rules be packaged with the library?

Yes you are right, and the gson.pro file you mentioned will be packaged in the JAR. However, those changes have not been released yet. They will be part of the upcoming version 2.11.0, see also #2632.

@snijsure snijsure reopened this Mar 14, 2024
@snijsure snijsure changed the title .IllegalStateException: TypeToken must be created when using GSON 2.10.1 and R8 on Android IllegalStateException: TypeToken must be created when using GSON 2.10.1 and R8 on Android Mar 14, 2024
@snijsure
Copy link
Author

@Marcono1234 thanks. I will keep this open. In my testing for my Android app at least I had to add following entries.
I am using AGP 8.1.2 and retrofit 2.9.0

Those rules were previously recommended here.

https://github.com/google/gson/blob/main/gson/src/main/resources/META-INF/proguard/gson.pro

# Keep no-args constructor of classes which can be used with @JsonAdapter
# By default their no-args constructor is invoked to create an adapter instance
-keepclassmembers class * extends com.google.gson.TypeAdapter {
  <init>();
}
-keepclassmembers class * implements com.google.gson.TypeAdapterFactory {
  <init>();
}
-keepclassmembers class * implements com.google.gson.JsonSerializer {
  <init>();
}
-keepclassmembers class * implements com.google.gson.JsonDeserializer {
  <init>();
}

# Keep fields annotated with @SerializedName for classes which are referenced.
# If classes with fields annotated with @SerializedName have a no-args
# constructor keep that as well. Based on
# https://issuetracker.google.com/issues/150189783#comment11.
# See also https://github.com/google/gson/pull/2420#discussion_r1241813541
# for a more detailed explanation.
-if class *
-keepclasseswithmembers,allowobfuscation class <1> {
  @com.google.gson.annotations.SerializedName <fields>;
}
-if class * {
  @com.google.gson.annotations.SerializedName <fields>;
}
-keepclassmembers,allowobfuscation,allowoptimization class <1> {
  <init>();
}

@Marcono1234 Marcono1234 added the proguard-r8 Issues relating to the use of ProGuard and/or R8, such as problems due to obfuscation label Mar 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 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