java.lang.ClassCastException:java.lang.Class无法使用retrofit2转换为java.lang.reflect.ParameterizedType

mar*_*pop 11 android classcastexception android-gradle-plugin retrofit2 android-r8

我有这个代码:

    val profile: UserProfile = userApi.profile()


    @GET("users/profile")
    suspend fun profile(): UserProfile
Run Code Online (Sandbox Code Playgroud)

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

java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType
                                                                                                        
Run Code Online (Sandbox Code Playgroud)

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

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

retrofitVersion = 2.9.0
kotlinVersion = 1.8.10
gradle = 8.0
Run Code Online (Sandbox Code Playgroud)

And*_*idz 15

如果您检查此链接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 
Run Code Online (Sandbox Code Playgroud)


小智 5

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

-keep class * extends com.google.protobuf.GeneratedMessageLite { *; }
-keep,allowobfuscation,allowshrinking class kotlin.coroutines.Continuation
Run Code Online (Sandbox Code Playgroud)

如果您不知道您的混淆器规则文件是什么,请检查您的app\build.gradle文件。应该有几行,例如:

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

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