2

我正在按照此处的指南尝试在新的 KMM 项目中使用没有 CocoaPods 的 iOS 框架:

https://kotlinlang.org/docs/kmm-add-dependencies.html#without-cocoapods

我有一个现有的、可工作的 .xcframework,我在 .xcframework 下添加到项目中shared/src。我在同一个共享目录中添加了一个MyKit.def文件src/nativeInterop/cinterop/并更新了该文件:build.gradle.kts

MyKit.def 看起来像

language = Objective-C
modules = MyKit
package = MyKit

构建.gradle.kts

import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget

plugins {
    kotlin("multiplatform")
    id("com.android.library")
}

kotlin {
    android()

    val iosTarget: (String, KotlinNativeTarget.() -> Unit) -> KotlinNativeTarget =
        if (System.getenv("SDK_NAME")?.startsWith("iphoneos") == true)
            ::iosArm64
        else
            ::iosX64

    iosTarget("ios") {
        binaries {
            framework {
                baseName = "shared"
            }
        }
    }
    sourceSets {
        val commonMain by getting
        val commonTest by getting {
            dependencies {
                implementation(kotlin("test-common"))
                implementation(kotlin("test-annotations-common"))
            }
        }
        val androidMain by getting
        val androidTest by getting {
            dependencies {
                implementation(kotlin("test-junit"))
                implementation("junit:junit:4.13.2")
            }
        }
        val iosMain by getting
        val iosTest by getting
    }
    iosArm64() {
        compilations.getByName("main") {
            val MyKit by cinterops.creating {
                // Path to .def file
                defFile("src/nativeInterop/cinterop/MyKit.def")
                compilerOpts("-framework", "MyKit", "-F/src/MyKit.framework")
            }
        }

        binaries.all {
            // Linker options required to link to the library -- the framework binary is located under src/MyKit.framework/ios-arm64/MyKit.framework/
            linkerOpts("-framework", "MyKit", "-F/src/MyKit.framework/ios-arm64/MyKit.framework/")
        }
    }
}

android {
    compileSdkVersion(31)
    sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
    defaultConfig {
        minSdkVersion(26)
        targetSdkVersion(31)
    }
}

添加import MyKit.*到我的 MainActivity 后,我得到了unknown reference错误。

是否支持 iOS 二进制框架?他们.在文件名中有一个分隔符,所以这可能是个问题。我的-F路径有问题??我不清楚路径是否应该一直到带有Headers二进制文件本身的目录或只是到框架根目录。TIA

4

0 回答 0