kotlin - 无法解析 Kotlin Multiplatform 中的 cinterop IOS 导入
问题描述
我已按照 Kotlin 文档添加 iOS 依赖项。就我而言,依赖项是通过第三方提供的预编译框架。所以我遵循了没有 cocoapod 的框架的案例。
我将 MyFramework.def 文件放在 /src
language = Objective-C
modules = MyFramework
package = MyFramework
然后我将以下内容添加到 Kotlin 对象中的 build.gradle.kts ```
ios {
binaries {
framework {
baseName = "shared"
}
}
}
iosArm64() {
compilations.getByName("main") {
val JWBLe by cinterops.creating {
// Path to .def file
defFile("src/nativeInterop/cinterop/MyFramework.def")
compilerOpts("-framework", "MyFramework", "-F/Users/user/Projects/MyFramework/ios/SDK")
}
}
binaries.all {
// Tell the linker where the framework is located.
linkerOpts("-framework", "MyFramework", "-F/Users/user/Projects/MyFramework/ios/SDK")
}
}
sourceSets {
val commonMain by getting
val commonTest by getting {
dependencies {
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
}
}
val androidMain by getting {
dependencies {
implementation("com.google.android.material:material:1.2.1")
}
}
val androidTest by getting {
dependencies {
implementation(kotlin("test-junit"))
implementation("junit:junit:4.13")
}
}
val iosMain by getting
val iosTest by getting
}
然后我建立项目。该库确实被看到了,我看到在外部库中,有一个shared-cinterop-MyFramework.klib
但是,当我尝试将这个包导入到我的代码中时,src/iosMain/kotlin/com.example.testapp.shared/platform.kt
我得到了库的未解决错误。看来我还需要在 sourceSets 中添加一些东西?但我不确定。
解决方案
首先,我注意到 Gradle 脚本不正确。在这种情况下,iosArm64
目标被声明了两次 - 通过目标快捷方式,并再次在您配置 cinterop 的位置声明。为了避免这种重复,最好像这样配置 cinterop:
ios()
val iosArm = targets.getByName("iosArm64") as org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
// A bit dirty cast, but as I'm sure iosArm64 is the Native target, it should be fine. Needed to make highlighting below work as expected.
iosArm.apply {
compilations.getByName("main") {
val JWBLe by cinterops.creating {
// Path to .def file
defFile("src/nativeInterop/cinterop/MyFramework.def")
compilerOpts("-framework", "MyFramework", "-F/Users/user/Projects/MyFramework/ios/SDK")
}
}
binaries.all {
// Tell the linker where the framework is located.
linkerOpts("-framework", "MyFramework", "-F/Users/user/Projects/MyFramework/ios/SDK")
}
}
但是,此调整无助于从iosMain
. 在commonizer目前的状态下,只能共享平台库。因此,无论如何,将使用这些绑定的所有代码移动到src/iosArm64Main
文件夹中是目前可用的最佳选择。这里有一个来自官方跟踪器的问题来支持和订阅 -支持用户定义库的通用化。
推荐阅读
- mongodb - 我们如何将 $toUpper 与数组字段一起使用?
- heroku - 如何在我的 heroku 管道中设置开发阶段
- postgresql - 如何检索表中的数据?PostgreSQL、AngularJS1、Spring、休眠
- javascript - 如果存在 DataContext,则该方法不会启动
- javascript - 预览未在动态添加/删除文件字段中正确更新
- c++ - git_remote_connect 出现“无效命令”错误
- postgresql - 尝试在 Google Cloud 中创建 SQL 实例 - Kubernetes
- linux - ubuntu 14.06 上的 omnet++ 生成文件中的错误
- vba - DropIt Project - 根据文件夹中的文件数量移动文件夹
- mysql - 我可以为 SQL_CALC_FOUND_ROWS 使用 1 而不是星号吗?