如何通过Podfile脚本删除xcconfig中特定LD_FLAG链接标记
永久移除Pods生成xcconfig中重复SwiftProtobuf链接标记方案
问题现象
编译Xcode项目时出现重复类实现报错:
Class _TtC13SwiftProtobuf17AnyMessageStorage is implemented in both SwiftProtobuf.framework/SwiftProtobuf and [MyX].app. One of the two will be used. Which one is undefined
手动删除Pods生成的.xcconfig文件里OTHER_LDFLAGS配置项中的-framework "SwiftProtobuf"即可临时修复,但每次执行pod install/pod update后,该配置会被CocoaPods自动重新生成,问题复现。
实现方式
将下面的post_install钩子脚本添加到项目Podfile的末尾,脚本会在每次pod依赖安装完成后自动执行,遍历所有生成的.xcconfig配置文件,移除OTHER_LDFLAGS中引发冲突的SwiftProtobuf链接标记,实现永久修复。
post_install do |installer| installer.pods_project.targets.each do |target| target.build_configurations.each do |config| xcconfig_path = config.base_configuration_reference.real_path # 读取原始xcconfig内容 raw_content = File.read(xcconfig_path) # 匹配并删除冲突的framework链接参数 patched_content = raw_content.gsub(/-framework "SwiftProtobuf"/, '') # 回写修改后的配置 File.write(xcconfig_path, patched_content) end end end
注意事项
- 脚本添加完成后,需要重新执行一次
pod install让配置生效,后续所有pod操作都会自动触发该脚本处理配置,不需要再手动修改xcconfig文件。 - 如果你本地生成的链接参数是不带引号的
-framework SwiftProtobuf格式,把脚本中gsub匹配的字符串替换成对应格式即可正常匹配删除。
内容的提问来源于stack exchange,提问作者Maurice Barbar
相关产品推荐
相关产品推荐

