icerockdev / moko-mvvm

Model-View-ViewModel architecture components for mobile (android & ios) Kotlin Multiplatform development
https://moko.icerock.dev/
Apache License 2.0
995 stars 95 forks source link

Can't have it working with coroutines-core:1.4.2-native-mt and io.ktor:ktor-client-core:1.5.0 on IOS #125

Closed brtwister2 closed 2 years ago

brtwister2 commented 3 years ago

I'm using this dependencies:

implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutineVersion-native-mt")
implementation ("io.ktor:ktor-client-core:1.5.0")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.0.1")

and when I add the moko mvvm implementations I start get error when making http requests with ktor in this part of the code:

    private val apiUrl =
        "https://gist.githubusercontent.com/jblorenzo/f8b2777c217e6a77694d74e44ed6b66b/raw/0dc3e572a44b7fef0d611da32c74b187b189664a/gistfile1.txt"

    fun getGrainList(success: (List<Grain>) -> Unit, failure: (Throwable?) -> Unit) {
        GlobalScope.launch(ApplicationDispatcher) {
            try {
                val url = apiUrl
                val json = HttpClient().get<String>(url)
                Json.decodeFromString(GrainList.serializer(), json)
                    .entries
                    .also(success)
            } catch (ex: Exception) {
                failure(ex)
            }
        }
    }

build.gradle.kts:

kotlin {
    android()
    ios {
        binaries {
            framework {
                baseName = "shared"
            }
        }
    }
    sourceSets {
        val commonMain by getting {
            dependencies {
                implementation("org.jetbrains.kotlin:kotlin-stdlib-common")
                implementation ("io.ktor:ktor-client-core:1.5.0")
                implementation(
                    "org.jetbrains.kotlinx:kotlinx-serialization-json:1.0.1")

                implementation("dev.icerock.moko:mvvm-core:$mokomvvmVersion")
                implementation("dev.icerock.moko:mvvm-livedata:$mokomvvmVersion")
                implementation("dev.icerock.moko:mvvm-state:$mokomvvmVersion")

                implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutineVersion-native-mt")

            }
        }
        val androidMain by getting {
            dependencies {
                implementation("androidx.core:core-ktx:1.3.2")
                implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutineVersion")
                implementation("io.ktor:ktor-client-android:1.5.0")
                implementation("dev.icerock.moko:mvvm-livedata-material:$mokomvvmVersion")
                implementation("dev.icerock.moko:mvvm-livedata-glide:$mokomvvmVersion")
                implementation("dev.icerock.moko:mvvm-livedata-swiperefresh:$mokomvvmVersion")
                implementation("dev.icerock.moko:mvvm-databinding:$mokomvvmVersion")
                implementation("dev.icerock.moko:mvvm-viewbinding:$mokomvvmVersion")

            }
        }
        val iosMain by getting {
            dependencies {
                implementation("io.ktor:ktor-client-ios:1.5.0")
            }
        }
    }

}
kotlin.Error: Ktor native HttpClient requires kotlinx.coroutines version with `native-mt` suffix (like `1.3.9-native-mt`). Consider checking the dependencies.
    at 0   shared                              0x000000010b40af1f kfun:kotlin.Throwable#<init>(kotlin.String?){} + 95 (/Users/teamcity2/buildAgent/work/11ac87a349af04d5/runtime/src/main/kotlin/kotlin/Throwable.kt:23:37)
    at 1   shared                              0x000000010b40381d kfun:kotlin.Error#<init>(kotlin.String?){} + 93 (/Users/teamcity2/buildAgent/work/11ac87a349af04d5/runtime/src/main/kotlin/kotlin/Exceptions.kt:12:44)
    at 2   shared                              0x000000010b79b7dd kfun:io.ktor.client.utils#checkCoroutinesVersion(){} + 493 (/Users/administrator/Documents/agent/work/8d547b974a7be21f/ktor-client/ktor-client-core/posix/src/io/ktor/client/utils/CoroutineUtilsPosix.kt:29:15)
    at 3   shared                              0x000000010b745637 kfun:io.ktor.client.HttpClient#<init>(io.ktor.client.engine.HttpClientEngine;io.ktor.client.HttpClientConfig<out|io.ktor.client.engine.HttpClientEngineConfig>){} + 2999 (/Users/administrator/Documents/agent/work/8d547b974a7be21f/ktor-client/ktor-client-core/common/src/io/ktor/client/HttpClient.kt:135:9)
    at 4   shared                              0x000000010b7461a0 kfun:io.ktor.client.HttpClient#<init>(io.ktor.client.engine.HttpClientEngine;io.ktor.client.HttpClientConfig<out|io.ktor.client.engine.HttpClientEngineConfig>;kotlin.Boolean){} + 144 (/Users/administrator/Documents/agent/work/8d547b974a7be21f/ktor-client/ktor-client-core/common/src/io/ktor/client/HttpClient.kt:81:9)
    at 5   shared                              0x000000010b7448bd kfun:io.ktor.client#HttpClient(io.ktor.client.engine.HttpClientEngineFactory<0:0>;kotlin.Function1<io.ktor.client.HttpClientConfig<0:0>,kotlin.Unit>){0§<io.ktor.client.engine.HttpClientEngineConfig>}io.ktor.client.HttpClient + 845 (/Users/administrator/Documents/agent/work/8d547b974a7be21f/ktor-client/ktor-client-core/common/src/io/ktor/client/HttpClient.kt:43:18)
    at 6   shared                              0x000000010b799690 kfun:io.ktor.client#HttpClient(kotlin.Function1<io.ktor.client.HttpClientConfig<*>,kotlin.Unit>){}io.ktor.client.HttpClient + 640 (/Users/administrator/Documents/agent/work/8d547b974a7be21f/ktor-client/ktor-client-core/posix/src/io/ktor/client/HttpClient.kt:18:46)
    at 7   shared                              0x000000010b7998b0 kfun:io.ktor.client#HttpClient$default(kotlin.Function1<io.ktor.client.HttpClientConfig<*>,kotlin.Unit>?;kotlin.Int){}io.ktor.client.HttpClient + 304 (/Users/administrator/Documents/agent/work/8d547b974a7be21f/ktor-client/ktor-client-core/posix/src/io/ktor/client/HttpClient.kt:16:15)
    at 8   shared                              0x000000010b38cfc0 kfun:com.raywenderlich.android.multigrain.shared.GrainApi.$getGrainList$lambda-0COROUTINE$0.invokeSuspend#internal + 3152 (/Users/bruno/Documents/projetos/KMM-PixGuiaPratico/shared/src/commonMain/kotlin/com/raywenderlich/android/multigrain/shared/GrainApi.kt:52:33)
    at 9   shared                              0x000000010b42d7f0 kfun:kotlin.coroutines.native.internal.BaseContinuationImpl#resumeWith(kotlin.Result<kotlin.Any?>){} + 800 (/Users/teamcity2/buildAgent/work/11ac87a349af04d5/runtime/src/main/kotlin/kotlin/coroutines/ContinuationImpl.kt:30:39)
    at 10  shared                              0x000000010b5bc552 kfun:kotlinx.coroutines.DispatchedTask#run(){} + 2818 (/opt/buildAgent/work/44ec6e850d5c63f0/kotlinx-coroutines-core/common/src/internal/DispatchedTask.kt:106:71)
    at 11  shared                              0x000000010b39484a kfun:com.raywenderlich.android.multigrain.shared.NsQueueDispatcher.dispatch$lambda-0#internal + 90 (/Users/bruno/Documents/projetos/KMM-PixGuiaPratico/shared/src/iosMain/kotlin/com/raywenderlich/android/multigrain/shared/Dispatcher.kt:50:13)
    at 12  shared                              0x000000010b394960 kfun:com.raywenderlich.android.multigrain.shared.NsQueueDispatcher.$dispatch$lambda-0$FUNCTION_REFERENCE$1.invoke#internal + 64 (/Users/bruno/Documents/projetos/KMM-PixGuiaPratico/shared/src/iosMain/kotlin/com/raywenderlich/android/multigrain/shared/Dispatcher.kt:49:35)
    at 13  shared                              0x000000010b3949c0 kfun:com.raywenderlich.android.multigrain.shared.NsQueueDispatcher.$dispatch$lambda-0$FUNCTION_REFERENCE$1.$<bridge-UNN>invoke(){}#internal + 64 (/Users/bruno/Documents/projetos/KMM-PixGuiaPratico/shared/src/iosMain/kotlin/com/raywenderlich/android/multigrain/shared/Dispatcher.kt:49:35)
    at 14  shared                              0x000000010b394aa9 _4b4d4d2d506978477569615072617469636f3a736861726564_knbridge4 + 185 (/Users/bruno/Documents/projetos/KMM-PixGuiaPratico/shared/src/iosMain/kotlin/com/raywenderlich/android/multigrain/shared/Dispatcher.kt:49:35)
    at 15  libdispatch.dylib                   0x00007fff2010532f _dispatch_call_block_and_release + 12
    at 16  libdispatch.dylib                   0x00007fff20106508 _dispatch_client_callout + 8
    at 17  libdispatch.dylib                   0x00007fff20112ff7 _dispatch_main_queue_callback_4CF + 1045
    at 18  CoreFoundation                      0x00007fff2038fdbb __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
    at 19  CoreFoundation                      0x00007fff2038a63e __CFRunLoopRun + 2685
    at 20  CoreFoundation                      0x00007fff203896d6 CFRunLoopRunSpecific + 567
    at 21  GraphicsServices                    0x00007fff2c257db3 GSEventRunModal + 139
    at 22  UIKitCore                           0x00007fff24696cf7 -[UIApplication _run] + 912
    at 23  UIKitCore                           0x00007fff2469bba8 UIApplicationMain + 101
    at 24  MultiGrain                          0x000000010b2b0bdb main + 75 (/Users/bruno/Documents/projetos/KMM-PixGuiaPratico/iosApp/iosApp/AppDelegate.swift:<unknown>)
    at 25  libdyld.dylib                       0x00007fff2025a3e9 start + 1
    at 26  ???                                 0x0000000000000001 0x0 + 1

I believe it got something to do with the dependencie org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.2 inside moko mvvm which should have be the "native-mt" implementation.

Can I get any help?

Regards.

Alex009 commented 3 years ago

for gradle version 1.4.2 is newest than 1.4.2-native-mt. to override this dependency resolution logic add into root build gradle:

allprojects {
    configurations.all {
        resolutionStrategy {
            force("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.2-native-mt")
        }
    }
}
brtwister2 commented 3 years ago

I will try it now! Thank you!