KMM项目iOS模拟器构建失败求助(含CocoaPods配置)
我正在开发依赖CocoaPods的Kotlin Multiplatform(KMM)项目,Android模拟器运行正常,但iOS模拟器构建失败。构建日志显示两个核心问题:链接器警告未找到指定搜索路径,且报错找不到Pods_iosApp框架,链接器命令执行失败(退出码1),同时存在脚本构建阶段的提示。
完整构建日志
Ld
/Users/andrea/Folders/KMP/ComposeWeather/build/ios/Debug-iphonesimulator/ComposeWeather.app/ComposeWeather
normal (in target 'iosApp' from project 'iosApp')
cd /Users/andrea/Folders/KMP/ComposeWeather/iosApp
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang
-Xlinker -reproducible -target x86_64-apple-ios14.1-simulator -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator17.2.sdk
-O0 -L/Users/andrea/Folders/KMP/ComposeWeather/build/ios/EagerLinkingTBDs/Debug-iphonesimulator
-L/Users/andrea/Folders/KMP/ComposeWeather/build/ios/Debug-iphonesimulator
-F/Users/andrea/Folders/KMP/ComposeWeather/build/ios/EagerLinkingTBDs/Debug-iphonesimulator
-F/Users/andrea/Folders/KMP/ComposeWeather/build/ios/Debug-iphonesimulator
-F/Users/andrea/Folders/KMP/ComposeWeather/iosApp/../shared/build/xcode-frameworks/Debug/iphonesimulator17.2
-F/Users/andrea/Folders/KMP/ComposeWeather/iosApp/../composeApp/build/xcode-frameworks/Debug/iphonesimulator17.2
-filelist /Users/andrea/Folders/KMP/ComposeWeather/build/ios/iosApp.build/Debug-iphonesimulator/iosApp.build/Objects-normal/x86_64/ComposeWeather.LinkFileList
-Xlinker -rpath -Xlinker /usr/lib/swift -Xlinker -rpath -Xlinker @executable_path/Frameworks -Xlinker -rpath -Xlinker
@loader_path/Frameworks -dead_strip -Xlinker -object_path_lto -Xlinker
/Users/andrea/Folders/KMP/ComposeWeather/build/ios/iosApp.build/Debug-iphonesimulator/iosApp.build/Objects-normal/x86_64/ComposeWeather_lto.o
-Xlinker -export_dynamic -Xlinker -no_deduplicate -Xlinker -objc_abi_version -Xlinker 2 -fobjc-link-runtime -L/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/iphonesimulator
-L/usr/lib/swift -Xlinker -add_ast_path -Xlinker /Users/andrea/Folders/KMP/ComposeWeather/build/ios/iosApp.build/Debug-iphonesimulator/iosApp.build/Objects-normal/x86_64/ComposeWeather.swiftmodule
-ObjC -lc++ -framework composeApp -framework composeApp -Xlinker -sectcreate -Xlinker __TEXT -Xlinker __entitlements -Xlinker /Users/andrea/Folders/KMP/ComposeWeather/build/ios/iosApp.build/Debug-iphonesimulator/iosApp.build/ComposeWeather.app-Simulated.xcent
-Xlinker -sectcreate -Xlinker __TEXT -Xlinker __ents_der -Xlinker /Users/andrea/Folders/KMP/ComposeWeather/build/ios/iosApp.build/Debug-iphonesimulator/iosApp.build/ComposeWeather.app-Simulated.xcent.der
-framework Pods_iosApp -Xlinker -no_adhoc_codesign -Xlinker -dependency_info -Xlinker /Users/andrea/Folders/KMP/ComposeWeather/build/ios/iosApp.build/Debug-iphonesimulator/iosApp.build/Objects-normal/x86_64/ComposeWeather_dependency_info.dat
-o /Users/andrea/Folders/KMP/ComposeWeather/build/ios/Debug-iphonesimulator/ComposeWeather.app/ComposeWeatherld: 警告:未找到搜索路径'/Users/andrea/Folders/KMP/ComposeWeather/iosApp/../shared/build/xcode-frameworks/Debug/iphonesimulator17.2'
ld: 未找到框架'Pods_iosApp' clang: 错误:链接器命令执行失败,退出码为1(使用-v查看调用详情)
** BUILD FAILED **
以下构建命令执行失败:
Ld
/Users/andrea/Folders/KMP/ComposeWeather/build/ios/Debug-iphonesimulator/ComposeWeather.app/ComposeWeather
normal (in target 'iosApp' from project 'iosApp')(1次失败) 警告:由于未指定任何输出,脚本构建阶段「Compile Kotlin Framework」将在每次构建时运行。要解决此警告,可向脚本阶段添加输出依赖,或取消勾选脚本阶段中的「基于依赖分析」选项,使其在每次构建时运行。(in target 'iosApp' from project 'iosApp')
build.gradle.kts中的CocoaPods配置
cocoapods { version = "1.0.0" summary = "Some description for the Shared Module" homepage = "Link to the Shared Module homepage" ios.deploymentTarget = "16.0" podfile = project.file("../iosApp/Podfile") framework { baseName = "composeApp" isStatic = true } // Maps custom Xcode configuration to NativeBuildType xcodeConfigurationToNativeBuildType["CUSTOM_DEBUG"] = NativeBuildType.DEBUG xcodeConfigurationToNativeBuildType["CUSTOM_RELEASE"] = NativeBuildType.RELEASE }
排查方向与解决线索
检查Pods_iosApp框架生成情况:
- 进入
iosApp目录执行pod install,确保依赖安装完成,查看iosApp/Pods/Target Support Files/Pods_iosApp/或构建输出目录中是否生成Pods_iosApp.framework。 - 必须打开Xcode的
.xcworkspace文件而非.xcodeproj,否则Pods配置不会被加载。
- 进入
修复缺失的搜索路径警告:
- 执行
./gradlew shared:podInstall手动触发shared模块的框架生成,检查../shared/build/xcode-frameworks/Debug/iphonesimulator17.2路径是否被创建。 - 若
shared模块未配置CocoaPods支持,需在其build.gradle.kts中添加cocoapods块,或移除当前项目中对该无效路径的引用。
- 执行
处理静态框架配置冲突:
当前composeApp框架设置为isStatic = true,需确认iOS项目的Pods依赖是否也采用静态链接模式。若存在动态依赖冲突,可尝试将isStatic改为false,或在Podfile中添加use_frameworks! :linkage => :static统一链接方式。修复脚本构建阶段警告:
在Xcode的「Compile Kotlin Framework」脚本阶段,添加输出文件路径(如$(SRCROOT)/../composeApp/build/xcode-frameworks/Debug/iphonesimulator17.2/composeApp.framework),或取消勾选「基于依赖分析」选项。清理与重建项目:
- 执行
./gradlew clean清理Gradle缓存,删除iosApp/Pods后重新执行pod install。 - 在Xcode中执行
Product > Clean Build Folder,再重新构建项目。
- 执行
内容的提问来源于stack exchange,提问作者Andrea Liureta

