kotlinx.coroutines: AbstractMethodError when use withTimeout

I am getting the following error java.lang.AbstractMethodError: abstract method "kotlinx.coroutines.DisposableHandle kotlinx.coroutines.Delay.invokeOnTimeout(long, java.lang.Runnable, kotlin.coroutines.CoroutineContext)" when calling the withTimeout method into commonMain module.

    override suspend fun notifyCharacteristic(uuid: String, enabled: Boolean): Flow<ByteArray> {
        return withTimeout(2000) {
            bluetoothDataSource.watchConnectionState().filter {
                it == DeviceConnectionState.STATE_DISCOVERED
            }.first()

            val charac = retrieveCharacteristic(characteristics, uuid)
                ?: throw IllegalStateException("Characteristic is null")

            bluetoothDataSource.notifyCharacteristic(
                charac,
                enabled,
                "00002902-0000-1000-8000-00805f9b34fb"
            ).map { it.toByteArray() }
        }
    }

My project is configured in this way:

       getByName("commonMain") {
            dependencies {
                implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.0-M1")
            }
        }
        getByName("androidMain") {
            dependencies {
                implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.0-M1")
            }
        }
        getByName("iosMain") {
            dependencies {
                implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.0-M1")
            }
        }

Can be the notifyCharacteristic function bad written?

Thank you

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 4
  • Comments: 15 (4 by maintainers)

Most upvoted comments

@mkotyk

What platform? And which dependency are you using?

If on Android, I had the same issue and fixed it as follows:

Originally I used org.jetbrains.kotlinx:kotlinx-coroutines-core:, but reading comments here, I switched to org.jetbrains.kotlinx:kotlinx-coroutines-android:. That helped.

I’m seeing this too, and it’s related to using debounce with 1.4.0 … once I took the debounce out, the crash stopped.

Here’s my usage:

 val showBiometricsPrompt: LiveData<ShowBiometricsPrompt?>
        get() = combine(_username.debounce(250), enrollingBiometrics) { a, b -> a to b }
            .map { (username, enrollingBiometrics) ->
                if (biometricsAvailable() is BiometricsAvailable.Result.Yes &&
                    (enrollingBiometrics || biometricsEnabledForUser(BiometricsEnabledForUser.Request(username)) is BiometricsEnabledForUser.Result.Yes)
                ) {
                    ShowBiometricsPrompt(username, enrollingBiometrics)
                } else {
                    null
                }
            }
            .asLiveData()

I’ve came across this issue when I was working on Mockk tests in my project. In my case, it was caused by different versions of org.jetbrains.kotlinx:kotlinx-coroutines-android and org.jetbrains.kotlinx:kotlinx-coroutines-test. Updating both of them to 1.4.1 fixed the issue, maybe it will help somenone.

Likely a version mismatch like in #1033

kotlinx-coroutines-core is a multiplatform library (JVM, common, Native, JS), but with recent additions to multiplatform plugin you may forget about various variants and use it as is in common source set.

kotlinx-coroutines-android is not a multiplatform variant. It’s a standalone JVM library that contains various utilities for Android development, e.g. Dispatchers.Main implementation. Typically, you should include it if you are working with Android. And then strict version should be used to avoid version mismatch of kotlinx-coroutines-core and kotlinx-coroutines-android