Cometd with Android: java.lang.RuntimeException: 找不到实现 class

Cometd with Android: java.lang.RuntimeException: Could not find an implementation class

目前我正尝试在 android 的 Kotlin 多平台应用程序中实现 cometD 客户端。

这是我的 android 依赖项

val androidMain by getting {
    dependencies {
        implementation("org.cometd.java:cometd-java-client-websocket-okhttp:7.0.5")
        implementation("org.cometd.java:cometd-java-client-websocket-javax:7.0.5")
    }
}

我的代码(CometdConnection.kt)

package com.roxorgaming.kotlinheadlessspike

import jakarta.websocket.ContainerProvider
import org.cometd.client.BayeuxClient
import org.cometd.client.websocket.javax.WebSocketTransport
import org.cometd.client.transport.ClientTransport


actual class CometdConnection actual constructor() {
    private lateinit var bayeuxClient: BayeuxClient

    actual fun initialise() {
        // Prepare the JSR 356 WebSocket transport.
        // Create a JSR 356 WebSocketContainer using the standard APIs.
        val webSocketContainer = ContainerProvider.getWebSocketContainer() //<--- this is where it fails
        // Create the CometD JSR 356 websocket transport.
        val wsTransport: ClientTransport = WebSocketTransport(null, null, webSocketContainer)

        // Configure BayeuxClient, with the websocket transport listed before the long-polling transport.
        bayeuxClient = BayeuxClient("http://localhost:8080/cometd", wsTransport)
    }

    actual fun handshake() {
        // Handshake with the server.
        bayeuxClient.handshake()
    }
}

应用程序已编译但在初始化失败并出现错误时崩溃

2021-12-06 11:18:09.234 19121-19121/com.roxorgaming.kotlinheadlessspike.android E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.roxorgaming.kotlinheadlessspike.android, PID: 19121
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.roxorgaming.kotlinheadlessspike.android/com.roxorgaming.kotlinheadlessspike.android.MainActivity}: java.lang.RuntimeException: Could not find an implementation class.
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
     Caused by: java.lang.RuntimeException: Could not find an implementation class.
        at jakarta.websocket.ContainerProvider.getWebSocketContainer(ContainerProvider.java:54)
        at com.roxorgaming.kotlinheadlessspike.CometdConnection.initialise(CometdConnection.kt:16)
        at com.roxorgaming.kotlinheadlessspike.android.MainActivity.onCreate(MainActivity.kt:20)
        at android.app.Activity.performCreate(Activity.java:7994)
        at android.app.Activity.performCreate(Activity.java:7978)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)

您不需要依赖性 org.cometd.java:cometd-java-client-websocket-javax:7.0.5 因为您已经拥有 org.cometd.java:cometd-java-client-websocket-okhttp:7.0.5.

删除它,错误就会消失。