java.lang.ClassCastException:java.lang.Class无法通过retrofit2转换为java.lang.reflect.ParameterizedType

问题描述 投票:0回答:2

我有这个代码:

    val profile: UserProfile = userApi.profile()


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

当我运行 userApi.profile() 时,我收到此错误:

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

详细信息:我更新了 gradle、一些库、kotlin 版本和 android studio。该代码在更新之前曾经有效。后端没有任何变化。

更新:看起来所有 api 调用都会发生这种情况,而不仅仅是这个。

retrofitVersion = 2.9.0
kotlinVersion = 1.8.10
gradle = 8.0
retrofit2 classcastexception
2个回答
2
投票

将这两行添加到您的 proguard 规则文件中

-keep class * extends com.google.protobuf.GeneratedMessageLite { *; }
-keep,allowobfuscation,allowshrinking class kotlin.coroutines.Continuation

如果您不知道您的 proguard 规则文件是什么,请检查您的

app\build.gradle
文件。应该有几行,例如:

buildTypes {
    getByName("release") {
        isMinifyEnabled = true
        proguardFiles(
            getDefaultProguardFile("proguard-android-optimize.txt"),
            "proguard-rules.pro",
        )
    }
}

proguard-rules.pro
是您的 proguard 规则文件。


0
投票

如果您检查此链接https://github.com/square/retrofit/issues/3751它会告诉您将这些添加到您的

proguard-rules.pro
文件中

# 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 
© www.soinside.com 2019 - 2024. All rights reserved.