简体   繁体   English

Xamarin 是否支持 Xamarin Android 绑定库中的 Kotlin 协程?

[英]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.我有一个带有 Kotlin 代码的 Android 绑定库,它会在遇到 Kotlin 代码中的 runBlocking{} 时退出。

E(28583:28691) ERROR [19] - The worker thread caught an exception: Failed resolution of: Lkotlinx/coroutines/BuildersKt; E(28583:28691) 错误 [19] - 工作线程捕获异常:解析失败:Lkotlinx/coroutines/BuildersKt; E(28583:28691) Java.Lang.NoClassDefFoundError: Failed resolution of: Lkotlinx/coroutines/BuildersKt; E(28583:28691) Java.Lang.NoClassDefFoundError:解析失败: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]] ---> Java.Lang.ClassNotFoundException:在路径上没有找到类“kotlinx.coroutines.BuildersKt”:DexPathList[[zip file“/data/app/de.mycompany.app.myproduct-1/base.apk” ],nativeLibraryDirectories=[/data/app/de.mycompany.app.myproduct-1/lib/arm, /v​​endor/lib, /system/lib]]

In my Kotlin AAR module, I have the following dependencies在我的 Kotlin AAR 模块中,我有以下依赖项

implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"实现"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
=> equivalent Xamarin.Kotlin.StdLib => 等效 Xamarin.Kotlin.StdLib
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.0.1'实现 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.0.1'
=> equivalent Xamarin.???? => 等效 Xamarin。????
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.0.1"实现“org.jetbrains.kotlinx:kotlinx-coroutines-android:1.0.1”
=> equivalent Xamarin.???? => 等效 Xamarin。????

I'm guessing that Xamarin.Kotlin.StdLib doesn't include support for the Kotlinx coroutines.我猜 Xamarin.Kotlin.StdLib 不包括对 Kotlinx 协程的支持。
Is there any way to get support for Kotlinx-coroutines in a Xamarin Android Binding library?有没有办法在 Xamarin Android 绑定库中获得对 Kotlinx 协程的支持?

You're right, Xamarin.Kotlin.StdLib doesn't include support for the Kotlinx coroutines.没错, Xamarin.Kotlin.StdLib不包括对 Kotlinx 协程的支持。

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.为了在 Xamarin Android 绑定库中拥有Kotlinx 协程,您必须添加KotlinX.Coroutines NuGet 包作为应用项目和绑定库项目的依赖项。

So if you have something like this:所以如果你有这样的事情:

MyAndroidApp.proj -> MyBindingLibrary.proj MyAndroidApp.proj -> MyBindingLibrary.proj

Both project have to add the KotlinX.Coroutines dependency.这两个项目都必须添加KotlinX.Coroutines依赖项。

NB In my case, Microsoft's binding was NOT working.注意,就我而言,Microsoft 的绑定不起作用。 I had to use Karamunting's binding .我不得不使用 Karamunting 的binding

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM