Kotlin多平台:proguard的Kotlin序列化错误:将类标记为@Serializable或显式提供序列化程序

rhfm7lfc  于 2022-12-23  发布在  Kotlin
关注(0)|答案(1)|浏览(204)

当使用isMinifyEnabled = false构建应用程序时,它工作得非常好,但当我启用它时,它停止工作。
堆栈跟踪:

ea.j: Serializer for class 'b' is not found.
                                                                                                    
Mark the class as @Serializable or provide the serializer explicitly.
                                                                                                         
at ia.d1.d(Unknown Source:33)
                                                                                                         
at ia.c1.l(Unknown Source:5)
                                                                                                         
at ea.m.d(Unknown Source:22)
                                                                                                         
at ea.l.b(Unknown Source:0)
                                                                                                         
at ua.a$b.l(Unknown Source:79)
                                                                                                         
at ua.a$b.o(Unknown Source:8)
                                                                                                         
at ua.a$b.Z(Unknown Source:4)
                                                                                                         
at e8.a.b(Unknown Source:249)
                                                                                                         
at e8.a$a.l(Unknown Source:10)
...

可序列化类:

@Serializable
data class LoginResponse(val accessTokens: ServerTokens, val magisterTokens: TokenResponse, val tenantUrl: String, @Required val type: Int = 1) // Types: 1 = completion

还有代码:

client.webSocket(host = SERVER_URL, port = 8080, path = EXCHANGE_URL.encodedPath) {
    send(Json.encodeToString(loginRequest))
    incoming.consumeEach { frame ->
        println("Received frame: $frame")
        if (frame is Frame.Text) {
            val json = Json.parseToJsonElement(frame.readText()).jsonObject
            if (json["type"].toString().toInt() == 1) {
                response = Json.decodeFromString<LoginResponse>(frame.readText()) // <--- i assume this is the problematic code
            }
        } else if (frame is Frame.Close) {
            if (frame.readReason()?.knownReason != CloseReason.Codes.NORMAL) {
                println("Error: ${frame.readReason()?.message}")
                throw Exception("Received: ${frame.readReason()?.message}")
            }
        }
    }
}

我试过在https://github.com/Kotlin/kotlinx.serialization#android上应用序列化规则,但是没有用。仍然有同样的错误。(是的,我清理了项目并重新构建了)
我也尝试了自定义规则的网页上,但这些都没有工作.
关于我的项目的一些信息:
Kotlin1.7.10
Kotlin多平台
KotlinX序列化Json 1.4.0
代码2.0.3
我不知道这是否相关,但是@Serializable类位于多平台项目的shared部分,而序列化发生在androidApp部分

v7pvogib

v7pvogib1#

将以下内容添加到proguard规则中:

# Rules needed for kotlinx.serialization
-if @kotlinx.serialization.Serializable class **
-keep class <1> {
    *;
}

相关问题