Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType with retrofit2

I have this code:

    val profile: UserProfile = userApi.profile()


    @GET("users/profile")
    suspend fun profile(): UserProfile

When i run userApi.profile() i get this error:

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

Details: I updated gradle, some libraries, kotlin version and android studio. This code used to work before updates. Nothing changed on backend side.

Update: looks like this is happening to all api calls, not just this one.

retrofitVersion = 2.9.0
kotlinVersion = 1.8.10
gradle = 8.0
like image 712
marius pop Avatar asked Sep 09 '25 17:09

marius pop


1 Answers

If you check this link https://github.com/square/retrofit/issues/3751 it will tell you to add these in your proguard-rules.pro file

# Keep generic signature of Call, Response (R8 full mode strips signatures from non-kept items). 
 -keep,allowobfuscation,allowshrinking interface retrofit2.Call 
 -keep,allowobfuscation,allowshrinking class retrofit2.Response 
  
 # With R8 full mode generic signatures are stripped for classes that are not 
 # kept. Suspend functions are wrapped in continuations where the type argument 
 # is used. 
 -keep,allowobfuscation,allowshrinking class kotlin.coroutines.Continuation 
like image 73
Androidz Avatar answered Sep 13 '25 06:09

Androidz