在Cocoapod中导入Kotlin / Native框架

时间:2018-02-27 20:55:11

标签: ios kotlin cocoapods kotlin-native

我试图在私人CocoaPod中添加使用Kotlin / Native构建的销售框架但是我收到错误:

  • 我使用Kotlin / Native生成了一个iOS框架。
  • 我将框架文件夹(由Konan编译/生成)复制到我的自定义pod文件夹
  • 在podspec中,我在" vendored_frameworks"中添加了框架路径。列表
  • 我启动了{{1}}
  • 我收到错误:

{{1}}

我是否应该以某种方式改变我首先使用Konan导出框架的格式?

2 个答案:

答案 0 :(得分:8)

您正在收到错误,因为默认情况下,Kotlin Native仅为单个体系结构生成二进制文件。当CocoaPods试图将它作为一种"脂肪"具有多种架构的二进制文件既然你无论如何都需要多个架构(至少arm64用于设备而x86_64用于模拟器),我使用的方法是创建两个架构,然后将它们与lipo合并。结果胖框架然后可以通过CocoaPods销售,或者只是在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"
}

答案 1 :(得分:2)

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