javaandroidkotlincometdkotlin-multiplatform-mobile

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


I am trying to implement a cometD client in a Kotlin Multiplatform App for android at the moment.

Here are my android dependencies

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

My code (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()
    }
}

The app gets compiled but crashes when the initialization fails with an error

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)

Solution

  • You don't need dependency org.cometd.java:cometd-java-client-websocket-javax:7.0.5 because you already have org.cometd.java:cometd-java-client-websocket-okhttp:7.0.5.

    Remove it and the error should go away.