首页 > 解决方案 > 无法在 iOS 中构建 KMM 应用程序的发布版本

问题描述

我正在尝试在 iOS 上为我的 KMM 应用程序构建发布版本。进度失败并显示消息
Exception in thread "main" java.lang.IllegalStateException: Could not find 'libCryptoKitWrapper.a' binary in neither of [CryptoKitWrapper/build/Release-iphoneos]
异常跟踪到我正在使用的 Swift 库。为了让它在 kotlin 上工作,我正在使用这种方法。令人惊讶的是,有问题的文件可以在列出的目录中找到。这里可能有什么问题?相关 cinterop 任务:

iosTarget("ios") {
    val platform = when (preset?.name) {
        "iosX64" -> "iphonesimulator"
        "iosArm64" -> "iphoneos"
        else -> error("Unsupported target $name")
    }
    compilations.getByName("main") {
        cinterops.create("CryptoKitWrapper") {
            val interopTask = tasks[interopProcessingTaskName]
            interopTask.dependsOn(":CryptoKitWrapper:build${platform.capitalize()}")
            includeDirs.headerFilterOnly("$rootDir/CryptoKitWrapper/build/Release-$platform/include")
        }
    }
}

标签: kotlingradlekotlin-multiplatform

解决方案


问题是由于 Gradle 将设置的位置解释libraryPaths为相对路径,从当前操作系统目录派生。要观察这一点,请尝试./gradlew :shared:cinteropCryptoKitWrapperIos --infoSwiftLibSampledir 和../gradlew cinteropCryptoKitWrapperIos --infofrom执行SwiftLibSample/shared。第一个应该可以正常工作。

要解决此问题,可以执行以下操作。不要硬编码文件的路径,而是在块.def内设置此参数。cinterops{...}我尝试了这个,灵感来自 @SalomonBRYS 在https://github.com/JetBrains/kotlin-native/issues/2314的回答

cinterops.create("CryptoKitWrapper") {
                ...
                extraOpts("-libraryPath", "$rootDir/CryptoKitWrapper/build/Release-$platform")
                ...

如果您想收到有关此问题的更新,请在官方 Kotlin 问题跟踪器KT-48082上关注此问题

编辑:以下内容与原始问题没有直接关系,我只想从下面的评论部分总结我的提示。

要启用项目布局中使用的位码,如https://github.com/MJegorovas/SwiftLibSample中所示,应该:

  1. 使静态库包含 Bitcode。在这种情况下,"BITCODE_GENERATION_MODE=bitcode"选项应该跨xcodebuild参数。

  2. 使 cinterop 工具将库与位码链接。将-lto-embed-bitcode链接器选项添加到.def文件。


推荐阅读