您收到错误是因为默认情况下,Kotlin Native 只为单一架构生成二进制文件。CocoaPods 在尝试将其视为具有多种架构的“胖”二进制文件时会失败。因为无论如何你都需要多个架构(至少 arm64 用于设备,x86_64 用于模拟器),我使用的方法是创建这两个架构,然后将它们与lipo
CocoaPods 出售所得的胖框架或只是拖动/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"
}