Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode New Build System CocoaPods "Copy Pods Resources" has Assets.car in Output Files and conflict with "Copy Bundle Resources"

We are using new build system and Cocoapods 1.7.5 for our Xcode project. Our project (let's call it Y) is actually a development pod, but we also have written some codes to create an application demo (you know, to make builds faster and iterations quicker). These demo codes (AppDelegate.swift, launch tasks etc.) are not included in the development pod. The remaining ~90 percent of the source codes and resource files (such as i18n strings and image resources) get packed into the development pod for another project (let's call it X) to use.

While developing, most of the changes occur to the dev pod part of Y, so we need to make sure that every slice of change is included when X does pod install.

Recently we encountered a problem:

error: Multiple commands produce '/Users/x/Library/Developer/Xcode/DerivedData/Y-cawteybtitmeiafweribgqzkuhnr/Build/Products/Debug-iphoneos/Y.app/Assets.car':
1) Target 'Y' (project 'Y') has compile command with input '/Users/name/DEV/workspace/Y/SupportFiles/Assets.xcassets'
2) That command depends on command in Target 'Y' (project 'Y'): script phase “[CP] Copy Pods Resources”

After hours of searching multiple commands produce assets.car on Google, we finally saw a plausible explanation:

*.xcassets of Copy Bundle Resources --> Assets.car

*.xcassets of [CP] Copy Pods Resource --> other Assets.car

The first one covers second in the New Build System,That's the reason.

When we manually remove the ${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/Assets.car from output files of [CP] Copy Pods Resource, the error is gone and everything works fine. However, whenever we git checkout to another branch, or pod install, or pod update, there is a pretty high chance that the ${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/Assets.car appears again in the output files of [CP] Copy Pods Resource. We need to manually remove it again and again, which is tedious and frustrating.

Then we started to question: Who is behind all this? Who is responsible for adding ${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/Assets.car to the output files of [CP] Copy Pods Resource? We focused our sight onto Y.podspec and found these lines:

  s.resource_bundles = {
    'Y' => ['Resources/*'], # assets, lottie etc.
    'YAuto' => ['auto_resources/*'] # i18n strings etc.
  }

We thought we were using resource_bundles wrong, so we looked it up, again on Google. Surprisingly, using resource_bundles over resources is recommended by official documentation. Additionally, we couldn't find any inappropriate usage in terms of resource_bundles, leaving us out of options.

Can somebody help? I was thinking maybe we could patch Y.xcworkspace in the post_install script in Podfile, but I don't know how.

like image 701
Houston Duane Avatar asked Jan 26 '23 14:01

Houston Duane


2 Answers

I resolved this issue by writing a post_install script in Podfile which automatically deletes these Assets.car.

post_install do |installer|
    project_path = 'Y.xcodeproj'
    project = Xcodeproj::Project.open(project_path)
    project.targets.each do |target|
        build_phase = target.build_phases.find { |bp| bp.display_name == '[CP] Copy Pods Resources' }
        assets_path = '${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/Assets.car'
        if build_phase.present? && build_phase.output_paths.include?(assets_path) == true
            build_phase.output_paths.delete(assets_path)
        end
    end
    project.save(project_path)
end

It seems to be a CocoaPods bug that still has not been fixed by now.

like image 88
Houston Duane Avatar answered Apr 09 '23 11:04

Houston Duane


This issue comes about in many ways; some like the described problem above and other times for having a Share Extension or Watch app in addition to an iOS app. In all scenarios, it's caused by there being multiple Assets.car outputs in the [CP] Copy Pods Resources of those targets which create duplicate Assets.car files. The compiler obviously doesn't know how to handle that, so we shouldn't be allowing CocoaPods to generate multiple output files with the same name.

The recommended solution per the CocoaPods issue is to put this in your Podfile:

# Default platform for most targets
platform :ios, '11.0'

# Workaround duplicate Assets.car issue https://github.com/CocoaPods/CocoaPods/issues/8122
# This impacts the new Xcode build system
install! 'cocoapods', :disable_input_output_paths => true

Xcode 12 also fixes an accompanying issue that let to build performance issues when using the new build system with this workaround. The legacy build system is officially deprecated as of Xcode 12.

like image 22
iwasrobbed Avatar answered Apr 09 '23 11:04

iwasrobbed