Search code examples
androidxamarin.androidkotlin-coroutines

Does Xamarin support Kotlin coroutines in a Xamarin Android Binding Library?


I have an Android Binding Library with Kotlin code, and it exits whenever it hits runBlocking{} in the Kotlin code.

E(28583:28691) ERROR [19] - The worker thread caught an exception: Failed resolution of: Lkotlinx/coroutines/BuildersKt; E(28583:28691) Java.Lang.NoClassDefFoundError: Failed resolution of: Lkotlinx/coroutines/BuildersKt; ---> Java.Lang.ClassNotFoundException: Didn't find class "kotlinx.coroutines.BuildersKt" on path: DexPathList[[zip file "/data/app/de.mycompany.app.myproduct-1/base.apk"],nativeLibraryDirectories=[/data/app/de.mycompany.app.myproduct-1/lib/arm, /vendor/lib, /system/lib]]

In my Kotlin AAR module, I have the following dependencies

implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
=> equivalent Xamarin.Kotlin.StdLib
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.0.1'
=> equivalent Xamarin.????
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.0.1"
=> equivalent Xamarin.????

I'm guessing that Xamarin.Kotlin.StdLib doesn't include support for the Kotlinx coroutines.
Is there any way to get support for Kotlinx-coroutines in a Xamarin Android Binding library?


Solution

  • You're right, Xamarin.Kotlin.StdLib doesn't include support for the Kotlinx coroutines.

    In order to have the Kotlinx-coroutines in a Xamarin Android Binding library, you have to add the KotlinX.Coroutines NuGet package as a dependency of the app project and the binding library project.

    So if you have something like this:

    MyAndroidApp.proj -> MyBindingLibrary.proj

    Both project have to add the KotlinX.Coroutines dependency.

    N.B. In my case, Microsoft's binding was NOT working. I had to use Karamunting's binding.