MacOS Ventura M1下Kotlin Multiplatform生成XCFramework报错求助
解决KMM项目生成XCFramework时
Could not initialize class llvm.llvm错误 问题原因
该错误多因LLVM工具链与MacOS Ventura不兼容,或Android Studio内置的Kotlin版本、LLVM组件存在系统适配缺陷导致。
解决方案
1. 升级Kotlin与Gradle版本
- 打开项目根目录
build.gradle.kts(或build.gradle),将Kotlin版本升级至1.8.0及以上:plugins { kotlin("multiplatform") version "1.8.22" apply false } - 更新Gradle wrapper版本到7.5及以上,修改
gradle/wrapper/gradle-wrapper.properties:distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip
2. 手动指定LLVM工具链路径
若升级后问题仍存在,添加Xcode内置LLVM路径配置:
- 确保已安装最新稳定版Xcode,并更新Command Line Tools
- 在项目
build.gradle.kts中添加配置:kotlin { targets.withType<org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget> { binaries.all { linkerOpts("-L/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx") linkerOpts("-L/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib") } } }
3. 清理缓存后重建
- 执行命令清理Gradle缓存与构建产物:
./gradlew clean rm -rf ~/.gradle/caches - 重启Android Studio,重新执行
assembleSharedCodeReleaseXCFramework任务
4. 适配Android Studio版本
Android Studio Dolphin(2021.3.1)发布于Ventura之前,存在系统适配局限:
- 建议升级至Electric Eel及以上版本的Android Studio
- 若暂不升级IDE,可在
Preferences > Build, Execution, Deployment > Build Tools > Gradle中,选择使用本地安装的Gradle版本替代IDE内置版本
验证操作
完成配置后重新运行构建任务,若错误消失则问题解决。若仍有异常,检查Xcode Command Line Tools路径:
xcode-select --print-path
确保输出为/Applications/Xcode.app/Contents/Developer,否则执行:
sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer
内容的提问来源于stack exchange,提问作者Xavier Duvan Melo
相关产品推荐
相关产品推荐

