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

Missing type parameter RuntimeException #2069

Closed
usmanrana-ilsa opened this issue Feb 1, 2022 · 11 comments
Closed

Missing type parameter RuntimeException #2069

usmanrana-ilsa opened this issue Feb 1, 2022 · 11 comments
Labels

Comments

@usmanrana-ilsa
Copy link

Gson version

'2.8.9'

Java / Android version

Java 11 / Android all

I'm facing following exception with Gradle version '7.1.0', whereas '7.0.4' is working fine. The issue is happening in both cases when minifyEnabled = true/false in that module, but main app module has minifyEnabled=true and proguard rules are mentioned as well.

Fatal Exception: java.lang.RuntimeException
Missing type parameter.
com.google.gson.reflect.TypeToken.getSuperclassTypeParameter (TypeToken.java:84)
com.google.gson.reflect.TypeToken.<init> (TypeToken.java:62)
@Marcono1234
Copy link
Collaborator

Could you please provide a minimal reproducible example?

@usmanrana-ilsa
Copy link
Author

usmanrana-ilsa commented Feb 2, 2022

  • Put Gson in a library module with release as minifyEnabled=false or true (doesn't matter)
  • Set your main app module as minifyEnabled=true
  • Update your gradle version to 7.1.0
  • Install the release version with debuggable= false

The issue will happen when you will try to parse json to list. But with debuggable=true it works fine. It works fine with gradle version 7.0.4 as well.

@Marcono1234
Copy link
Collaborator

Marcono1234 commented Feb 6, 2022

Just to verify, your ProGuard rules contain the following, correct?

-keepattributes Signature

This StackOverflow question seems to describe the same issue for Gradle 7.1.0, and also contains a (slightly cumbersome and error-prone) workaround. It would it be interesting nonetheless to understand why this started appearing with Gradle 7.1.0.

Unfortunately I am not so familiar with Android development, and don't have the necessary development setup at the moment. Would be great if someone more familiar with Android, or one of the Gson project members could have a look.

Edit: Is this even about Gradle 7.1, or is this actually about the Android Gradle plugin 7.1.0? Maybe this is related to #1930 and to recent changes to R8 which removes generic signatures. Please verify that your rules are correct.

@usmanrana-ilsa
Copy link
Author

yes, adding that to ProGuard rules also didn't work. Issue is when using Android Gradle plugin 7.1.0 . Otherwise same code works fine with '7.0.4'. Also the stackOverflow link worked for me which recommends using 'Type listType = TypeToken.getParameterized(List.class, Country.class).getType();'

@Marcono1234
Copy link
Collaborator

Have you also tried adding the following, which was added in #1930:

# 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

@keobrien04
Copy link

keobrien04 commented Feb 7, 2022 via email

@forlayo
Copy link

forlayo commented Mar 2, 2022

In my case was just adding the following to proguard configuration:

# 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

Here you go the full set of options that are needed for Gson -> https://github.com/google/gson/blob/master/examples/android-proguard-example/proguard.cfg

@iamarjun
Copy link

iamarjun commented Mar 9, 2022

In my case was just adding the following to proguard configuration:

# 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

Here you go the full set of options that are needed for Gson -> https://github.com/google/gson/blob/master/examples/android-proguard-example/proguard.cfg

is Proguard.cfg any different from proguard-rules.pro?, will adding this to proguard-rules.pro work?

@forlayo
Copy link

forlayo commented Mar 9, 2022

@iamarjun It’s same, the name of the file is something that can change from one project to other. If you’re adding it to your proguard rules file it will work

@easyonebd
Copy link

if anyone has the same problem, didn't gotten any solution yet, you can try this. I faced the same issue and adding these 3 lines in progurd saved my day.

-keep class com.google.gson.reflect.TypeToken
-keep class * extends com.google.gson.reflect.TypeToken
-keep public class * implements java.lang.reflect.Type

@eamonnmcmanus
Copy link
Member

Thanks to everyone who has commented explaining how this can be fixed with ProGuard configuration. Since this isn't a Gson issue as such, I'm going to close it. The information here will remain valuable to others running into the same problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

8 participants
@forlayo @eamonnmcmanus @keobrien04 @Marcono1234 @iamarjun @easyonebd @usmanrana-ilsa and others