android / architecture-components-samples

Samples for Android Architecture Components.
https://d.android.com/arch
Apache License 2.0
23.36k stars 8.28k forks source link

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

Open PKReejesh opened 1 year ago

PKReejesh commented 1 year ago

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 commented 1 year ago

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.