安卓Module with the Main dispatcher is missing.报错

在使用Dispatchers.Main作为上下文启动协程时报错:

E/E: 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

解决办法:

gradle里增加kotlinx-coroutines-android,和kotlinx-coroutines-core版本号一致

如:

implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.9'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.9"

 

你可能感兴趣的:(安卓Module with the Main dispatcher is missing.报错)