在 Cocoapod 中导入 Kotlin/Native 框架

Import Kotlin/Native framework in Cocoapod

我正在尝试在私有 CocoaPod 中添加一个使用 Kotlin/Native 构建的销售框架,但出现错误:

[iOS] xcodebuild: fatal error: lipo: input file (/Users/jeandaube/Library/Developer/Xcode/DerivedData/App-auugdpsmbbpvarfzghxatkvwftsn/Build/Products/Release-iphonesimulator/App.app/Frameworks/MyProject.framework/MyProject) must be a fat file when the -remove option is specified

我是否应该首先更改使用 Konan 导出框架的格式?

Kotlin/Native 不生成多体系结构(又名 fat)二进制文件(参见 https://en.wikipedia.org/wiki/Fat_binary),因此在它生成的框架上尝试 运行 lipo 没有意义.

您收到错误是因为默认情况下,Kotlin Native 只为单一架构生成二进制文件。 CocoaPods 在尝试将其视为具有多种架构的 "fat" 二进制文件时会失败。由于无论如何您都需要多种架构(设备至少需要 arm64,模拟器至少需要 x86_64),我使用的方法是创建这两种架构,然后将它们与 lipo 合成的胖框架合并然后可以通过 CocoaPods 出售或仅 drag/drop 安装在 Xcode.

def outputDirectory = "$export_dir/$projectName/$projectVersion"
def outputFramework = "$outputDirectory/${projectName}.framework"

konanArtifacts {
    // Declare building into a framework, build arm64 for device, x64 for simulator                                                      
    framework(projectName, targets: ['ios_arm64', 'ios_x64' ]) {
        // The multiplatform support is disabled by default.                                   
        enableMultiplatform true
    }
}

// combine original arm64 and x64 libraries into a single library in
// the exported framework folder
task combineArchitectures(type: Exec, dependsOn: compileKonanLibrary) {
    executable 'lipo'
    args = [
            '-create',
            '-arch',
            'arm64',
            new File(compileKonanLibraryIos_arm64.artifact, 'Library'),
            '-arch',
            'x86_64',
            new File(compileKonanLibraryIos_x64.artifact, 'Library'),
            '-output',
            "$outputFramework/Library"
    ]
}

// export the arm64 (doesn't matter which really) framework, skipping
// the library binary itself
task exportFramework(type: Copy, dependsOn: compileKonanLibrary) {
    from compileKonanLibraryIos_arm64.artifact
    into outputFramework
    exclude projectName
    finalizedBy combineArchitectures
}

// build a pod spec by copying and updating a template file
task exportPodspec(type: Copy) {
    from "Library.podspec"
    into outputDirectory
    filter {
        it.replaceAll('@@projectName@@', projectName)
            .replaceAll('@@projectVersion@@', projectVersion)
    }
}

task export {
    dependsOn "exportFramework"
    dependsOn "exportPodspec"
}