architecture-components-samples icon indicating copy to clipboard operation
architecture-components-samples copied to clipboard

ClassCastException java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType

Open PKReejesh opened this issue 2 years ago • 1 comments

I am getting two errors

  1. java.lang.IllegalArgumentException: Unable to create call adapter for class androidx.lifecycle.LiveData from the caller.

  2. Caused by: java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType , which is pointed at the line

if (getRawType(returnType) != LiveData::class.java) { return null }

File : https://github.com/android/architecture-components-samples/blob/master/GithubBrowserSample/app/src/main/java/com/android/example/github/util/LiveDataCallAdapterFactory.kt

This happens in the release apk. Recent changes : Added co routine components.

Requesting to explain the issue please.

PKReejesh avatar Jul 07 '23 10:07 PKReejesh

When debugging with this line, inside LiveDataCallAdapterFactory get method,

String name = returnType.getClass().getName(); //to get full.package.name.of.Cat
Log.i(TAG,"Check#in between:"+name);

Found differences in release and debug apk.

Debug apk printed the log as : Check#in between:libcore.reflect.ParameterizedTypeImpl Release apk printed the log as : Check#in between:java.lang.Class

Also with breakpoints, I was able to see that, the Type variable was wrapped with a LiveData

So,

Adding -keep class androidx.lifecycle.LiveData { *; }

to proguard solved the issue.

PKReejesh avatar Jul 10 '23 13:07 PKReejesh