User Class.forName instead of ServiceLoader to instantiate Dispatchers.Main on Android (#1572)
Fixes #1557
Fixes #878
Fixes #1606
diff --git a/kotlinx-coroutines-core/jvm/src/internal/MainDispatchers.kt b/kotlinx-coroutines-core/jvm/src/internal/MainDispatchers.kt
index 0dce51c..6f11cdf 100644
--- a/kotlinx-coroutines-core/jvm/src/internal/MainDispatchers.kt
+++ b/kotlinx-coroutines-core/jvm/src/internal/MainDispatchers.kt
@@ -20,13 +20,11 @@
private fun loadMainDispatcher(): MainCoroutineDispatcher {
return try {
val factories = if (FAST_SERVICE_LOADER_ENABLED) {
- MainDispatcherFactory::class.java.let { clz ->
- FastServiceLoader.load(clz, clz.classLoader)
- }
+ FastServiceLoader.loadMainDispatcherFactory()
} else {
- //We are explicitly using the
- //`ServiceLoader.load(MyClass::class.java, MyClass::class.java.classLoader).iterator()`
- //form of the ServiceLoader call to enable R8 optimization when compiled on Android.
+ // We are explicitly using the
+ // `ServiceLoader.load(MyClass::class.java, MyClass::class.java.classLoader).iterator()`
+ // form of the ServiceLoader call to enable R8 optimization when compiled on Android.
ServiceLoader.load(
MainDispatcherFactory::class.java,
MainDispatcherFactory::class.java.classLoader