Proguard Gson TypeToken Issue

When use Gson to convert JSON to List of Object, we face with how to get the Type for second params of Gson().fromJson(String, Type).

The best way we need reflector with TypeToken to enable us to get Type of List<Object> with this code

val type = object: TypeToken<List<Person>>() {}.type

But if we still use that way to reflect our List to Type and we enable the Proguard without add specific rules to Gson, we will get this error when we run release app.

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.
  at i4.a.<init>(SourceFile:3)
  at k4.c.<init>(Unknown Source:0)

So to fix this I found the other way to reflect our List to Type and we do not need to add specific ProGuard rules to Gson

val type = TypeToken.getParameterized(List::class.java, Person::class.java).type

references : https://stackoverflow.com/a/70970276

Komentar