使用Retrofit并设置minifyEnabled true时无法接收数据

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

我尝试使用android从服务器获取数据,当设置minifyEnabled false时我工作得很好,但是当我设置minifyEnabled true时我无法修改数据,一切都是空

我的java版本是17,我的retrosit版本是

com.squareup.retrofit2:retrofit:2.6.0
com.squareup.retrofit2:converter-gson:2.5.0
com.squareup.okhttp3:logging-interceptor:3.12.0

我尝试了 stackoverflow 上的其他答案

-keepclassmembers,allowobfuscation class * {
  @com.google.gson.annotations.SerializedName <fields>;
}
-dontwarn retrofit2.**
-dontwarn org.codehaus.mojo.**
-keep class retrofit2.** { *; }
-keepattributes Signature
-keepattributes Exceptions
-keepattributes *Annotation*

-keepattributes RuntimeVisibleAnnotations
-keepattributes RuntimeInvisibleAnnotations
-keepattributes RuntimeVisibleParameterAnnotations
-keepattributes RuntimeInvisibleParameterAnnotations

-keepattributes EnclosingMethod

-keepclasseswithmembers class * {
    @retrofit2.* <methods>;
}

-keepclasseswithmembers interface * {
    @retrofit2.* <methods>;
}

我的调用api的代码

interface ApiService {
    @POST("/ipay-decode/")
    suspend fun handleDataTest(
        @Body requestBody: BaseRequest,
    ): BaseResponse
}

data class BaseRequest(
    @SerializedName("encrypted")
    val encrypted: String
)

open class BaseResponse {
    @SerializedName("message")
    var message: String = ""
}

任何人都可以帮我解决问题吗?

android kotlin retrofit proguard
1个回答
0
投票

这是由于启用 minify 后您的模型变得模糊。要解决此问题,请将模型包的路径添加到 Proguard 规则中。

这样的东西应该有效:

-keep class your.package.name.model.** { *; }
© www.soinside.com 2019 - 2024. All rights reserved.