2021-05-13

Xcode 11编译错误Multiple commands produce的解决

今天在工程中遇到一个问题,当在Xcode 11中使用CocoaPod并在编译的时候使用默认的New Build System编译,出现了错误提示:

Multiple commands produce '/Users/xxx/Library/Developer/Xcode/DerivedData/Runner-ghzverollryofhfjgwuzxnrzynml/Build/Products/Debug-iphonesimulator/Runner.app/Assets.car':
1) Target 'Runner' (project 'Runner') has compile command with input '/Users/xxx/Desktop/source/projectName/ios/Runner/Assets.xcassets'
2) That command depends on command in Target 'Runner' (project 'Runner'): script phase “[CP] Copy Pods Resources”

同时出现一个warning的提示:

duplicate output file '/Users/xxx/Library/Developer/Xcode/DerivedData/Runner-ghzverollryofhfjgwuzxnrzynml/Build/Products/Debug-iphonesimulator/Runner.app/Assets.car' on task: PhaseScriptExecution [CP] Copy Pods Resources /Users/xxx/Library/Developer/Xcode/DerivedData/Runner-ghzverollryofhfjgwuzxnrzynml/Build/Intermediates.noindex/Runner.build/Debug-iphonesimulator/Runner.build/Script-9FB25929BF420392D1CE58C4.sh

查找问题产生的原因:

在 Build System Release Notes for Xcode 10中,有一句话:

Targets which have multiple asset catalogs that aren't in the same build phase may produce an error regarding a "duplicate output file". (39810274)

Workaround: Ensure that all asset catalogs are processed by the same build phase in the target.

也就是我,我们在编译过程的多个阶段可能都产生了asset产物,导致New Build System出现了错误。

问题的解决方案:

1.修改编译系统:从默认的New Build System(Default)改成Legacy Build Sysytem

File->WorkSpace Settings-〉Build System

修改编译系统

绝大多数的情况下就能解决这个问题。但是我修改之后,会出现其他错误,调研了半天,发现我的工程配置必须要使用New Build System来编译。那只好寻找其他解决方案。在CocoaPod的github中,有人提过这个isseue,其中有两种解决方式:

2.修改cocoaPod的Podfile脚本

在Podfile中的platform :ios, '9.0'底下增加如下代码

install! 'cocoapods', :disable_input_output_paths => true

这样就会禁用CocoaPods脚本阶段的输入和输出路径(复制框架和复制资源)

在一般情况下这个解决方案是没有问题的,但是有些需要在脚本阶段复制资源和框架的工程来说可能带来其他问题。

3.修改Build Phase的输入输出路径的设置

在Xcode11
工程中的Build Phase的[CP] Copy Pods Resources阶段,将outputlist中的内容,添加到inputlist中。但是这个方法有个问题就是,每次你pod install后,都需要手动修改一下。

 

修改前

修改后

4.后续问题

修改完后,发现pod中的图片会覆盖主工程中的同名文件。也就是上文warning中提到的问题。

这主要是因为,在一些pod库中,图片是通过s.resource 来导入,这会导致cocoapod会将pod库中的图片直接复制到主工程中的Assets.car中,如果本来主工程中有同名文件,就会被覆盖

 s.resource = 'xxx/Assets/*.xcassets'

解决问题的方式是使用如下方式来导出pod中的文件

s.resource_bundles = {
    'xxxAsset' => ['xxx/Assets/*.xcassets']
 }

使用s.resource_bundles导出的话,会把图片拷贝到单独的一个bundle中,因此在pod中使用图片只能采用下面这种方式:

UIImage *image = [UIImage imageNamed:@"your-image-name"
                            inBundle:[NSBundle pod1_bundle]
       compatibleWithTraitCollection:nil];

不过由于不是所有的第三方库我们都可以修改,并且我的工程中被覆盖的只是appIcon.assets,也就是app的图标文件,我只需要将主工程中的AppIcon.assets修改一下名称,就可以解决,不需要再进行复杂的修改。

修改图标文件名称

具体的sources和resource_bundles的区别,请参考[cocoapod中的resources字段](https://guides.cocoapods.org/syntax/podspec.html#resources

 

本文转载自:https://www.jianshu.com/p/1e46bf6c48eb

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值