首页 > 解决方案 > 无法解析 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 中添加一些东西?但我不确定。

标签: kotlinkotlin-multiplatform

解决方案


首先,我注意到 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文件夹中是目前可用的最佳选择。这里有一个来自官方跟踪器的问题来支持和订阅 -支持用户定义库的通用化


推荐阅读