mamoe / mirai

高效率 QQ 机器人支持库
https://mirai.mamoe.net
GNU Affero General Public License v3.0
14.51k stars 2.55k forks source link

java.lang.NoSuchMethodError: 'void kotlinx.serialization.json.JsonBuilder.setExplicitNulls(boolean)' #1777

Closed whitescent closed 1 year ago

whitescent commented 2 years ago

问题描述

在编写 mirai plugin 的时候,使用 kotlin serialization 创建了一个带有 explicitNulls 属性的 Json 句柄,IDE 中显示有此方法,但是在插件运行的时候报错

import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.json.Json
import okhttp3.MediaType.Companion.toMediaType
import retrofit2.Call
import retrofit2.Retrofit
import retrofit2.http.GET
import retrofit2.http.Query

@OptIn(ExperimentalSerializationApi::class)
interface ApiInterface {

    @GET("search")
    fun search(
        @Query("group") group: Long,
        @Query("uid") uid: Long,
    ): Call<UserObj>

    companion object {
        private const val BASE_URL = ""

        private val json = Json {
            ignoreUnknownKeys = true
            explicitNulls = false
        }.asConverterFactory("application/json".toMediaType())

        fun create(): ApiInterface {
            return Retrofit
                .Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(
                    json
                )
                .build()
                .create(ApiInterface::class.java)
        }
    }
}
@Serializable
data class UserObj(
    val group: Long,
    val uid: Long,
    val mengunNum: Long?,
    val rollTimeStamp: Long?,
)

复现

因为在请求 api 的时候,api 返回可能会有为 null 的对象

例如: {"group":123456,"uid":3333333,"mengunNum":7} // rolltimestamp 为空

所以在创建 Json 句柄的时候,设置了 explicitNulls = false,但是只要请求 api 的时候,就会引发 NoSuchMethod 错误,反编译后的 class 也有这个方法,不知道哪里出了问题

{5BEB14E3-BE2D-BCCA-ED43-7614E61FDE20}

mirai-core 版本

2.9.0-M1

bot-protocol

ANDROID_PHONE

其他组件版本

No response

系统日志

2021-12-30 21:15:20 E/org.github.nthily: Exception in coroutine Plugin org.github.nthily of org.github.nthily
java.lang.NoSuchMethodError: 'void kotlinx.serialization.json.JsonBuilder.setExplicitNulls(boolean)'
        at org.mybot.api.ApiInterface$Companion$json$1.invoke(ApiInterface.kt:36)
        at org.mybot.api.ApiInterface$Companion$json$1.invoke(ApiInterface.kt:34)
        at kotlinx.serialization.json.JsonKt.Json(Json.kt:137)
        at kotlinx.serialization.json.JsonKt.Json$default(Json.kt:135)
        at org.mybot.api.ApiInterface$Companion.<clinit>(ApiInterface.kt:34)
        at org.mybot.api.ApiInterface.<clinit>(ApiInterface.kt)
        at org.mybot.api.Api.<clinit>(Api.kt:11)
        at org.mybot.functions.RollKt.roll(roll.kt:59)
        at org.mybot.Plugin$onEnable$4.invokeSuspend(Plugin.kt:55)
        at org.mybot.Plugin$onEnable$4.invoke(Plugin.kt)
        at org.mybot.Plugin$onEnable$4.invoke(Plugin.kt)
        at net.mamoe.mirai.event.EventChannel$subscribeAlways$1.invokeSuspend(EventChannel.kt:432)
        at net.mamoe.mirai.event.EventChannel$subscribeAlways$1.invoke(EventChannel.kt)
        at net.mamoe.mirai.event.EventChannel$subscribeAlways$1.invoke(EventChannel.kt)
        at net.mamoe.mirai.internal.event.Handler$onEvent$2.invokeSuspend(InternalEventListeners.kt:50)
        at net.mamoe.mirai.internal.event.Handler$onEvent$2.invoke(InternalEventListeners.kt)
        at net.mamoe.mirai.internal.event.Handler$onEvent$2.invoke(InternalEventListeners.kt)
        at kotlinx.coroutines.intrinsics.UndispatchedKt.startUndispatchedOrReturn(Undispatched.kt:89)
        at kotlinx.coroutines.BuildersKt__Builders_commonKt.withContext(Builders.common.kt:165)
        at kotlinx.coroutines.BuildersKt.withContext(Unknown Source)
        at net.mamoe.mirai.internal.event.Handler.onEvent(InternalEventListeners.kt:50)
        at net.mamoe.mirai.internal.event.InternalEventListenersKt.process(InternalEventListeners.kt:166)
        at net.mamoe.mirai.internal.event.InternalEventListenersKt.callAndRemoveIfRequired(InternalEventListeners.kt:115)
        at net.mamoe.mirai.event._EventBroadcast.broadcastImpl(Event.kt:175)
        at net.mamoe.mirai.IMirai.broadcastEvent(IMirai.kt:318)
        at net.mamoe.mirai.internal.MiraiImpl.broadcastEvent$suspendImpl(MiraiImpl.kt:308)
        at net.mamoe.mirai.internal.MiraiImpl.broadcastEvent(MiraiImpl.kt)
        at net.mamoe.mirai.event._EventBroadcast.broadcastPublic$suspendImpl(Event.kt:162)
        at net.mamoe.mirai.event._EventBroadcast.broadcastPublic(Event.kt)
        at net.mamoe.mirai.event.EventKt.broadcast(Event.kt:147)
        at net.mamoe.mirai.internal.network.components.EventDispatcherImpl$broadcast$2.invokeSuspend(EventDispatcher.kt:90)
        at net.mamoe.mirai.internal.network.components.EventDispatcherImpl$broadcast$2.invoke(EventDispatcher.kt)
        at net.mamoe.mirai.internal.network.components.EventDispatcherImpl$broadcast$2.invoke(EventDispatcher.kt)
        at kotlinx.coroutines.intrinsics.UndispatchedKt.startUndispatchedOrReturn(Undispatched.kt:89)
        at kotlinx.coroutines.BuildersKt__Builders_commonKt.withContext(Builders.common.kt:165)
        at kotlinx.coroutines.BuildersKt.withContext(Unknown Source)
        at net.mamoe.mirai.internal.network.components.EventDispatcherImpl.broadcast$suspendImpl(EventDispatcher.kt:89)
        at net.mamoe.mirai.internal.network.components.EventDispatcherImpl.broadcast(EventDispatcher.kt)
        at net.mamoe.mirai.internal.network.components.EventDispatcherImpl$broadcastAsync$job$1.invokeSuspend(EventDispatcher.kt:105)
        at net.mamoe.mirai.internal.network.components.EventDispatcherImpl$broadcastAsync$job$1.invoke(EventDispatcher.kt)
        at net.mamoe.mirai.internal.network.components.EventDispatcherImpl$broadcastAsync$job$1.invoke(EventDispatcher.kt)
        at kotlinx.coroutines.intrinsics.UndispatchedKt.startCoroutineUndispatched(Undispatched.kt:55)
        at kotlinx.coroutines.CoroutineStart.invoke(CoroutineStart.kt:112)
        at kotlinx.coroutines.AbstractCoroutine.start(AbstractCoroutine.kt:126)
        at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch(Builders.common.kt:56)
        at kotlinx.coroutines.BuildersKt.launch(Unknown Source)
        at net.mamoe.mirai.internal.network.components.EventDispatcherImpl.broadcastAsync-kKe4JaQ(EventDispatcher.kt:102)
        at net.mamoe.mirai.internal.network.components.EventDispatcher.broadcastAsync-kKe4JaQ$default(EventDispatcher.kt:35)
        at net.mamoe.mirai.internal.network.components.EventBroadcasterPacketHandler.impl(PacketHandler.kt:87)
        at net.mamoe.mirai.internal.network.components.EventBroadcasterPacketHandler.handlePacket(PacketHandler.kt:73)
        at net.mamoe.mirai.internal.network.components.PacketHandlerChain.handlePacket(PacketHandler.kt:42)
        at net.mamoe.mirai.internal.network.handler.NetworkHandlerSupport$collectReceived$3.invokeSuspend(NetworkHandlerSupport.kt:84)
        at net.mamoe.mirai.internal.network.handler.NetworkHandlerSupport$collectReceived$3.invoke(NetworkHandlerSupport.kt)
        at net.mamoe.mirai.internal.network.handler.NetworkHandlerSupport$collectReceived$3.invoke(NetworkHandlerSupport.kt)
        at kotlinx.coroutines.intrinsics.UndispatchedKt.startCoroutineUndispatched(Undispatched.kt:55)
        at kotlinx.coroutines.CoroutineStart.invoke(CoroutineStart.kt:112)
        at kotlinx.coroutines.AbstractCoroutine.start(AbstractCoroutine.kt:126)
        at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch(Builders.common.kt:56)
        at kotlinx.coroutines.BuildersKt.launch(Unknown Source)
        at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch$default(Builders.common.kt:47)
        at kotlinx.coroutines.BuildersKt.launch$default(Unknown Source)
        at net.mamoe.mirai.internal.network.handler.NetworkHandlerSupport.collectReceived$mirai_core(NetworkHandlerSupport.kt:82)
        at net.mamoe.mirai.internal.network.impl.netty.NettyNetworkHandler$PacketDecodePipeline$send$1.invokeSuspend(NettyNetworkHandler.kt:178)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
        at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:164)
        at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:472)
        at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:500)
        at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
        at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
        at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
        at java.base/java.lang.Thread.run(Unknown Source)

网络日志

2021-12-30 21:20:39 V/Net 993298343: Loading server list from cache.
2021-12-30 21:20:39 I/Net 993298343: Server list: msfwifi.3g.qq.com:8080, 180.102.111.213:443, 183.47.98.11:8080, 119.147.190.155:14000, 180.102.111.105:80, 42.81.184.253:443, 101.91.42.98:8080, 49.7.253.65:443, 42.81.184.253:80, 183.47.98.105:8080.
2021-12-30 21:20:39 I/Net 993298343: ECDH key is valid.
2021-12-30 21:20:39 V/Net 993298343: Send: wtlogin.exchange_emp
2021-12-30 21:20:39 V/Net 993298343: Recv: wtlogin.exchange_emp
2021-12-30 21:20:39 V/Net 993298343: Send: StatSvc.register(online)
2021-12-30 21:20:40 I/Net 993298343: Server time updated, serverTime: 1640870439, diff: -1ms=-0.001s
2021-12-30 21:20:40 V/Net 993298343: Recv: StatSvc.register
2021-12-30 21:20:40 V/Net 993298343: Loading BdhSession from cache file
2021-12-30 21:20:40 I/Net 993298343: Syncing friend message history...
2021-12-30 21:20:40 V/Net 993298343: Send: StatSvc.GetDevLoginInfo(StatSvc.GetDevLoginInfo)
2021-12-30 21:20:40 I/Net 993298343: Start loading friend list...
2021-12-30 21:20:40 I/Net 993298343: Start loading stranger list...
2021-12-30 21:20:40 V/Net 993298343: Send: OidbSvc.0x496(OidbSvc.0x496)
2021-12-30 21:20:40 V/Net 993298343: Send: OidbSvc.0x5d2_0(OidbSvc.0x5d2_0)
2021-12-30 21:20:40 V/Net 993298343: Send: friendlist.getFriendGroupList(friendlist.getFriendGroupList)
2021-12-30 21:20:40 V/Net 993298343: Recv: OidbSvc.0x496
2021-12-30 21:20:40 I/Net 993298343: Start loading group list...
2021-12-30 21:20:40 V/Net 993298343: Send: friendlist.GetTroopListReqV2(friendlist.GetTroopListReqV2)
2021-12-30 21:20:40 V/Net 993298343: Recv: OidbSvc.0x5d2_0
2021-12-30 21:20:40 V/Net 993298343: Recv: StatSvc.GetDevLoginInfo
2021-12-30 21:20:40 I/Net 993298343: No OtherClient online.
2021-12-30 21:20:40 I/Net 993298343: Successfully loaded stranger list: 4 in total
2021-12-30 21:20:40 V/Net 993298343: Recv: friendlist.GetTroopListReqV2
2021-12-30 21:20:40 V/Net 993298343: Send: friendlist.GetTroopMemberListReq(friendlist.GetTroopMemberListReq)
2021-12-30 21:20:40 V/Net 993298343: Send: friendlist.GetTroopMemberListReq(friendlist.GetTroopMemberListReq)
2021-12-30 21:20:40 V/Net 993298343: Send: friendlist.GetTroopMemberListReq(friendlist.GetTroopMemberListReq)
2021-12-30 21:20:40 V/Net 993298343: Send: friendlist.GetTroopMemberListReq(friendlist.GetTroopMemberListReq)
2021-12-30 21:20:40 V/Net 993298343: Send: friendlist.GetTroopMemberListReq(friendlist.GetTroopMemberListReq)
2021-12-30 21:20:40 V/Net 993298343: Send: friendlist.GetTroopMemberListReq(friendlist.GetTroopMemberListReq)
2021-12-30 21:20:40 V/Net 993298343: Send: friendlist.GetTroopMemberListReq(friendlist.GetTroopMemberListReq)
2021-12-30 21:20:40 V/Net 993298343: Send: friendlist.GetTroopMemberListReq(friendlist.GetTroopMemberListReq)
2021-12-30 21:20:40 V/Net 993298343: Send: friendlist.GetTroopMemberListReq(friendlist.GetTroopMemberListReq)
2021-12-30 21:20:40 V/Net 993298343: Send: friendlist.GetTroopMemberListReq(friendlist.GetTroopMemberListReq)
2021-12-30 21:20:40 V/Net 993298343: Send: friendlist.GetTroopMemberListReq(friendlist.GetTroopMemberListReq)
2021-12-30 21:20:40 V/Net 993298343: Send: friendlist.GetTroopMemberListReq(friendlist.GetTroopMemberListReq)
2021-12-30 21:20:40 V/Net 993298343: Send: friendlist.GetTroopMemberListReq(friendlist.GetTroopMemberListReq)
2021-12-30 21:20:40 V/Net 993298343: Send: friendlist.GetTroopMemberListReq(friendlist.GetTroopMemberListReq)
2021-12-30 21:20:40 V/Net 993298343: Recv: friendlist.getFriendGroupList
2021-12-30 21:20:40 V/Net 993298343: Loading friend list: 52/52
2021-12-30 21:20:40 I/Net 993298343: Successfully loaded friend list: 52 in total
2021-12-30 21:20:40 V/Net 993298343: Recv: friendlist.GetTroopMemberListReq
2021-12-30 21:20:40 V/Net 993298343: Recv: friendlist.GetTroopMemberListReq
2021-12-30 21:20:40 V/Net 993298343: Recv: friendlist.GetTroopMemberListReq
2021-12-30 21:20:40 V/Net 993298343: Recv: friendlist.GetTroopMemberListReq
2021-12-30 21:20:40 V/Net 993298343: Recv: friendlist.GetTroopMemberListReq
2021-12-30 21:20:40 V/Net 993298343: Recv: friendlist.GetTroopMemberListReq
2021-12-30 21:20:40 V/Net 993298343: Recv: friendlist.GetTroopMemberListReq
2021-12-30 21:20:40 V/Net 993298343: Recv: friendlist.GetTroopMemberListReq
2021-12-30 21:20:40 V/Net 993298343: Recv: friendlist.GetTroopMemberListReq
2021-12-30 21:20:40 V/Net 993298343: Recv: friendlist.GetTroopMemberListReq
2021-12-30 21:20:40 V/Net 993298343: Recv: friendlist.GetTroopMemberListReq
2021-12-30 21:20:40 V/Net 993298343: Recv: friendlist.GetTroopMemberListReq
2021-12-30 21:20:40 V/Net 993298343: Send: friendlist.GetTroopMemberListReq(friendlist.GetTroopMemberListReq)
2021-12-30 21:20:40 V/Net 993298343: Recv: friendlist.GetTroopMemberListReq
2021-12-30 21:20:40 V/Net 993298343: Send: friendlist.GetTroopMemberListReq(friendlist.GetTroopMemberListReq)
2021-12-30 21:20:40 V/Net 993298343: Recv: friendlist.GetTroopMemberListReq
2021-12-30 21:20:40 V/Net 993298343: Recv: friendlist.GetTroopMemberListReq
2021-12-30 21:20:40 V/Net 993298343: Send: friendlist.GetTroopMemberListReq(friendlist.GetTroopMemberListReq)
2021-12-30 21:20:40 V/Net 993298343: Recv: friendlist.GetTroopMemberListReq
2021-12-30 21:20:40 V/Net 993298343: Send: friendlist.GetTroopMemberListReq(friendlist.GetTroopMemberListReq)
2021-12-30 21:20:40 V/Net 993298343: Recv: friendlist.GetTroopMemberListReq
2021-12-30 21:20:40 I/Net 993298343: Syncing friend message history: Success.
2021-12-30 21:20:41 V/Net 993298343: Recv: friendlist.GetTroopMemberListReq
2021-12-30 21:20:41 V/Net 993298343: Send: friendlist.GetTroopMemberListReq(friendlist.GetTroopMemberListReq)
2021-12-30 21:20:41 V/Net 993298343: Recv: friendlist.GetTroopMemberListReq
2021-12-30 21:20:41 I/Net 993298343: Successfully loaded group list: 14 in total.
2021-12-30 21:20:41 I/Net 993298343: Scheduled refresh login session in 19d 23h 55min 0.0s.
2021-12-30 21:20:41 I/Net 993298343: Scheduled key refresh in 23h 55min 0.0s.
2021-12-30 21:20:41 V/Net 993298343: Send: MessageSvc.PbSendMsg(MessageSvc.PbSendMsg)
2021-12-30 21:20:41 V/Net 993298343: Recv: MessageSvc.PbSendMsg
2021-12-30 21:20:43 V/Net 993298343: Saving bdh session to cache
2021-12-30 21:20:43 V/Net 993298343: Send: ConfigPushSvc.PushResp(ConfigPushSvc.PushResp)
2021-12-30 21:20:43 V/Net 993298343: Recv: ConfigPushSvc.PushReq

补充信息

build.gradle.kts

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    val kotlinVersion = "1.6.10"
    kotlin("jvm") version kotlinVersion

    id("net.mamoe.mirai-console") version "2.9.0-M1"
    id("org.jetbrains.kotlin.plugin.serialization") version kotlinVersion

}

group = "org.mybot"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
}

dependencies {
    implementation ("org.jsoup:jsoup:1.14.3")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.6.10")
    implementation("com.squareup.okhttp3:okhttp:4.9.3")
    implementation("net.mamoe:mirai-core-utils:2.9.0")

    implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.1")
    implementation("com.squareup.retrofit2:retrofit:2.9.0")
    implementation("com.jakewharton.retrofit:retrofit2-kotlinx-serialization-converter:0.8.0")
}

val compileKotlin: KotlinCompile by tasks

compileKotlin.kotlinOptions {
    jvmTarget = "1.8"
    freeCompilerArgs = freeCompilerArgs + "-Xopt-in=kotlin.RequiresOptIn"
}
Karlatemp commented 2 years ago

use kt 1.5.30, mirai目前还未升级到1.6.10

Him188 commented 2 years ago

把 kotlinx-serialization-json 版本改成 1.2.2, 把 Kotlin 版本改成 1.5.31

whitescent commented 2 years ago

@Him188 explicitNulls 是 1.3.0-RC 才开始有的,我刚刚设置了一下 Kotlin 为 1.5.31,kotlinx-serialization-json 为 1.3.0 好像还是不行

Him188 commented 2 years ago

@Nthily 目前插件的类加载方式还有一些问题, 插件与 console 共享类加载器, 导致插件无法使用与 console 使用的不同的版本

sandtechnology commented 2 years ago

可以考虑一下relocation看看

Get Outlook for Androidhttps://aka.ms/AAb9ysg


From: Him188 @.> Sent: Thursday, December 30, 2021 10:36:07 PM To: mamoe/mirai @.> Cc: Subscribed @.***> Subject: Re: [mamoe/mirai] java.lang.NoSuchMethodError: 'void kotlinx.serialization.json.JsonBuilder.setExplicitNulls(boolean)' (Issue #1777)

@Nthilyhttps://github.com/Nthily 目前插件的类加载方式还有一些问题, 插件与 console 共享类加载器, 导致插件无法使用与 console 使用的不同的版本

― Reply to this email directly, view it on GitHubhttps://github.com/mamoe/mirai/issues/1777#issuecomment-1003055996, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AE3YYC55UXR6Q7NIW4HMWSLUTRU5PANCNFSM5K7XBCEQ. Triage notifications on the go with GitHub Mobile for iOShttps://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Androidhttps://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub. You are receiving this because you are subscribed to this thread.Message ID: @.***>

Him188 commented 1 year ago

@Karlatemp should close?