今天在工程中遇到一個問題,當(dāng)在Xcode 11中使用CocoaPod并在編譯的時候使用默認(rèn)的New Build System編譯,出現(xiàn)了錯誤提示:
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”
同時出現(xiàn)一個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
查找問題產(chǎn)生的原因:
在 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.
也就是我,我們在編譯過程的多個階段可能都產(chǎn)生了asset產(chǎn)物,導(dǎo)致New Build System出現(xiàn)了錯誤。
問題的解決方案:
1.修改編譯系統(tǒng):從默認(rèn)的New Build System(Default)改成Legacy Build Sysytem
File->WorkSpace Settings-〉Build System

絕大多數(shù)的情況下就能解決這個問題。但是我修改之后,會出現(xiàn)其他錯誤,調(diào)研了半天,發(fā)現(xiàn)我的工程配置必須要使用New Build System來編譯。那只好尋找其他解決方案。在CocoaPod的github中,有人提過這個isseue,其中有兩種解決方式:
2.修改cocoaPod的Podfile腳本
在Podfile中的platform :ios, '9.0'底下增加如下代碼
install! 'cocoapods', :disable_input_output_paths => true
這樣就會禁用CocoaPods腳本階段的輸入和輸出路徑(復(fù)制框架和復(fù)制資源)
在一般情況下這個解決方案是沒有問題的,但是有些需要在腳本階段復(fù)制資源和框架的工程來說可能帶來其他問題。
3.修改Build Phase的輸入輸出路徑的設(shè)置
在Xcode11
工程中的Build Phase的[CP] Copy Pods Resources階段,將outputlist中的內(nèi)容,添加到inputlist中。但是這個方法有個問題就是,每次你pod install后,都需要手動修改一下。


4.后續(xù)問題
修改完后,發(fā)現(xiàn)pod中的圖片會覆蓋主工程中的同名文件。也就是上文warning中提到的問題。
這主要是因為,在一些pod庫中,圖片是通過s.resource 來導(dǎo)入,這會導(dǎo)致cocoapod會將pod庫中的圖片直接復(fù)制到主工程中的Assets.car中,如果本來主工程中有同名文件,就會被覆蓋
s.resource = 'xxx/Assets/*.xcassets'
解決問題的方式是使用如下方式來導(dǎo)出pod中的文件
s.resource_bundles = {
'xxxAsset' => ['xxx/Assets/*.xcassets']
}
使用s.resource_bundles導(dǎo)出的話,會把圖片拷貝到單獨的一個bundle中,因此在pod中使用圖片只能采用下面這種方式:
UIImage *image = [UIImage imageNamed:@"your-image-name"
inBundle:[NSBundle pod1_bundle]
compatibleWithTraitCollection:nil];
不過由于不是所有的第三方庫我們都可以修改,并且我的工程中被覆蓋的只是appIcon.assets,也就是app的圖標(biāo)文件,我只需要將主工程中的AppIcon.assets修改一下名稱,就可以解決,不需要再進行復(fù)雜的修改。

具體的sources和resource_bundles的區(qū)別,請參考[cocoapod中的resources字段](https://guides.cocoapods.org/syntax/podspec.html#resources)