简体   繁体   English

Kotlin Mutliplatform:混淆器的 Kotlin 序列化错误:将类标记为 @Serializable 或显式提供序列化器

[英]Kotlin Mutliplatform: Kotlin Serialization error with proguard: Mark the class as @Serializable or provide the serializer explicitly

When building the app with isMinifyEnabled = false it works perfectly fine, but when I enable it, it stops working.使用isMinifyEnabled = false构建应用程序时,它工作得很好,但是当我启用它时,它停止工作。

Stack trace:堆栈跟踪:

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)
...

The serializable class:可序列化类:

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

And the code:和代码:

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}")
            }
        }
    }
}

I have tried applying the serialization rules at https://github.com/Kotlin/kotlinx.serialization#android , but they didn't work.我尝试在https://github.com/Kotlin/kotlinx.serialization#android应用序列化规则,但它们没有用。 Still had the same error.仍然有同样的错误。 (Yes I cleaned the project and rebuilt) (是的,我清理了项目并重建了)

I have also tried the custom rules on that pages, but those didn't work either.我还在该页面上尝试了自定义规则,但这些也不起作用。

Some information about my project:关于我的项目的一些信息:

Kotlin 1.7.10科特林 1.7.10
Kotlin Multiplatform Kotlin 多平台
KotlinX Serialization Json 1.4.0 KotlinX 序列化 Json 1.4.0
Ktor 2.0.3科托尔2.0.3

I don't know if it's relevant, but the @Serializable classes are in the shared part of the multiplatform project, while the serialization happens in the androidApp part我不知道它是否相关,但@Serializable类在多平台项目的shared部分,而序列化发生在androidApp部分

Add the following to your proguard rules:将以下内容添加到您的混淆器规则中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM