https://kotlinlang.org logo
#compose-desktop
Title
# compose-desktop
d

Daniele B

03/18/2024, 4:24 PM
I am getting this error, even though I included the "kotlinx-coroutines-core" in the commonMain and the "kotlinx-coroutines-swing" in the jvmMain block of the shared gradle file:
Copy code
Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: Module with the Main dispatcher is missing. Add dependency providing the Main dispatcher, e.g. 'kotlinx-coroutines-android' and ensure it has the same version as 'kotlinx-coroutines-core'
	at kotlinx.coroutines.internal.MainDispatchersKt.throwMissingMainDispatcherException(MainDispatchers.kt:77)
	at kotlinx.coroutines.internal.MissingMainCoroutineDispatcher.missing(MainDispatchers.kt:108)
	at kotlinx.coroutines.internal.MissingMainCoroutineDispatcher.isDispatchNeeded(MainDispatchers.kt:92)
	at kotlinx.coroutines.internal.DispatchedContinuationKt.resumeCancellableWith(DispatchedContinuation.kt:315)
	at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable(Cancellable.kt:26)
	at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable$default(Cancellable.kt:21)
	at kotlinx.coroutines.CoroutineStart.invoke(CoroutineStart.kt:88)
	at kotlinx.coroutines.AbstractCoroutine.start(AbstractCoroutine.kt:123)
	at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch(Builders.common.kt:52)
	at kotlinx.coroutines.BuildersKt.launch(Unknown Source)
	at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch$default(Builders.common.kt:43)
	at kotlinx.coroutines.BuildersKt.launch$default(Unknown Source)
any idea how to solve this?
g

Garret Yoder

03/18/2024, 4:26 PM
On desktop you need to provide a dispatcher based on what you want to use (swing/jfx) for example -
kotlinx-coroutines-jvm = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-swing", version.ref = "coroutines" }
d

Daniele B

03/18/2024, 4:35 PM
I have added "kotlinx-coroutines-swing" already, but I get this error
g

Garret Yoder

03/18/2024, 4:49 PM
where do you have that added?
d

Daniele B

03/18/2024, 4:50 PM
I added in the gradle file of the shared module
g

Garret Yoder

03/18/2024, 4:52 PM
is your project desktop only or multiplatform? If so, you need to add it for the jvm configuration.
d

Daniele B

03/18/2024, 5:40 PM
it's a multiplatform project, and I am using coroutines only in the shared module
I found the problem! I had created an extra dependencies block in the JVM!!!
my fault!
a

albrechtroehm

03/29/2024, 3:18 PM
Is this working for you with compose-multiplatform > 1.5.11? When i tried to update last time i had similar issues but did not had the time to dig in too much
d

Daniele B

03/29/2024, 4:17 PM
I am using Compose Multiplatform 1.6.1. It works good. It was just my mistake.
a

albrechtroehm

03/29/2024, 4:20 PM
Hmm really strange, I'm seeing this only when updating to 1.6.0 and I already have swing coroutines in my desktopMain block. Would you mind posting some more of your gradle file?
Looks like Dispatchers.Main is not ideal to be used. After changing to Dispatchers.Default everything was fine again 🙂 I created this open class to use coroutines in ViewModels very similar to android, but maybe thats not the best idea 🤷
Copy code
open class ViewModel {
    protected val viewModelScope = CoroutineScope(Dispatchers.Default + SupervisorJob())
}
p

Pedro Romano Barbosa

04/02/2024, 12:18 AM
@Daniele B I'm also using Compose Multiplatform 1.6.1. Where did you insert the coroutines swing dependency?
Copy code
desktopMain.dependencies { ... }
Here?
d

Daniele B

04/02/2024, 10:41 AM
@Pedro Romano Barbosa yes, exactly
6 Views