question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

java.lang.AssertionError: illegal type variable reference

See original GitHub issue

i use gson 2.8.5 in android project,run app with proguard on real Android Phone. when execute code Type listType = new TypeToken<List<T>>() {}.getType(),then crash:

 java.lang.AssertionError: illegal type variable reference
        at libcore.reflect.TypeVariableImpl.resolve(TypeVariableImpl.java:111)
        at libcore.reflect.TypeVariableImpl.getGenericDeclaration(TypeVariableImpl.java:125)
        at libcore.reflect.TypeVariableImpl.hashCode(TypeVariableImpl.java:47)
        at java.util.Arrays.hashCode(Arrays.java:4153)
        at com.google.gson.internal.$Gson$Types$ParameterizedTypeImpl.hashCode($Gson$Types.java:502)
        at com.google.gson.reflect.TypeToken.<init>(TypeToken.java:64)
......

i have add these code in proguard.pro:

-keepattributes Exceptions
-keepattributes InnerClasses
-keepattributes Signature
-keepattributes Deprecated
-keepattributes SourceFile
-keepattributes LineNumberTable
-keepattributes *Annotation*
-keepattributes EnclosingMethod

# Gson specific classes
-keep class sun.** { *; }
#-keep class com.google.gson.stream.** { *; }

# Prevent proguard from stripping interface information from TypeAdapterFactory,
# JsonSerializer, JsonDeserializer instances (so they can be used in @JsonAdapter)
-keep class * implements com.google.gson.TypeAdapterFactory
-keep class * implements com.google.gson.JsonSerializer
-keep class * implements com.google.gson.JsonDeserializer

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:10
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
kkmike999commented, Jul 5, 2018

@lyubomyr-shaydariv maybe some other config cause this error. i config build.gradle

android {
 buildTypes{
    alpha {
            minifyEnabled true
            zipAlignEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro', 'proguard-rules-2.0.pro'
            signingConfig signingConfigs.releaseConfig
            multiDexKeepProguard file('keep_in_main_dex.pro')
            debuggable = false

            // matchingFallbacks属性来设置回退策略(必填,http://codezjx.com/2017/11/23/gradle-plugin-3-0-0-migration/)
            matchingFallbacks = ['release']
        }
    }
}

the problem solve. But when config debuggable = ture, the error occur T_T.

1reaction
lyubomyr-shaydarivcommented, Mar 2, 2020

@rosuH No need to have it such complex. You can simply create a method like this:

public static <T> List<T> fromJsonToList(final String json, final Class<T> elementClass) {
	return gson.fromJson(json, TypeToken.getParameterized(List.class, elementClass).getType());
}

that will do the job. I’d only recommend this method if you provide the element types dynamically, not statically (also note that gson can be re-used, no need to new it). However, I believe you only need something like this:

private static final TypeToken<List<Short>> listOfShorts = new TypeToken<List<Short>>() {};
private static final TypeToken<List<Float>> listOfFloats = new TypeToken<List<Float>>() {};

public static void main(final String... args) {
	final String json = "[1,2,3,4,5,6]";
	System.out.println(gson.<List<Short>>fromJson(json, listOfShorts.getType()));
	System.out.println(gson.<List<Float>>fromJson(json, listOfFloats.getType()));
}

that would produce [1, 2, 3, 4, 5, 6] and [1.0, 2.0, 3.0, 4.0, 5.0, 6.0] respectively. It is a little more verbose, but you don’t need to implement custom methods, and you can reuse type tokens too.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Gson illegal type variable reference - java - Stack Overflow
Inside this class there is a method doRequest that creates a TypeToken (from com.google.json.reflect ) for a Gson request. java.lang.reflect.
Read more >
Gson:java.lang.AssertionError: illegal type variable reference
1、问题现象:APP使用XX加固助手加固之后,在安卓7.x.x 系统中会崩溃:错误信息为java.lang.AssertionError: illegal type variable reference。
Read more >
Android:java.lang.AssertionError: illegal type variable reference
AssertionError : illegal type variable reference,1、问题现象:APP使用XX加固助手加固之后,在安卓7.x.x系统中会崩溃:错误信息为java.lang.
Read more >
混淆导致的java.lang.AssertionError: illegal type ... - 程序员宅基地
在做混淆的时候,经常会出现各种奇怪的错误, 以下便是一种:java.lang.AssertionError: illegal type variable reference at libcore.reflect.
Read more >
java.lang (Java SE 19 & JDK 19) - Oracle Help Center
The Void class is a non-instantiable class that holds a reference to a Class object representing the type void. The class Math provides...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found