I have a multiplatform project: Android and Deskto...
# coroutines
I have a multiplatform project: Android and Desktop. I have defined the core coroutine package in `commonMain`:
Copy code
on Android, the app works without the need of adding the android-specific coroutine package on
on Desktop, it gives me this error:
Module with the Main dispatcher is missing. Add dependency providing the Main dispatcher, e.g. 'kotlin-coroutine-android' and ensure it has the same version as 'kotlin-coroutine-core'
what is the reason why on Android, the target specific package is not needed?
I didn't work with multiplatform projects yet, but I suppose there are Android specific and desktop specific dependencies in your project. And some Android dependency itself depends on
so you have it implicitly in the project. However this is not the case for the desktop part of a project.
good point, it makes sense, thanks!
Dispatchers.Main is defined only contexts where there is a main dispatcher. In Android, the Main dispatcher is dispatcher dispatching coroutine to the UI thread of Android. In a desktop application, by default, there is no main dispatcher. To make it simple, if you don't have a UI in your app, you should not be using Dispatchers.Main (at least in the common code). If you have a UI, you should make sure to have the dependency that specifies Mian dispatcher for your UI framework. If, for example, you use swing to render the UI on desktop, you should add the swing dependency to your desktop dependencies. Read more here: https://github.com/Kotlin/kotlinx.coroutines/blob/master/ui/coroutines-guide-ui.md