【问题标题】:Xcode New Build System CocoaPods "Copy Pods Resources" has Assets.car in Output Files and conflict with "Copy Bundle Resources"Xcode New Build System CocoaPods“Copy Pods Resources”在输出文件中有 Assets.car 并与“Copy Bundle Resources”冲突
【发布时间】:2020-03-14 15:52:05
【问题描述】:

我们正在为我们的 Xcode 项目使用新的构建系统和 Cocoapods 1.7.5。我们的项目(我们称之为 Y)实际上是一个开发 pod,但我们也编写了一些代码来创建应用程序演示(您知道,使构建更快,迭代更快)。这些演示代码(AppDelegate.swift、启动任务等)不包含在开发 pod 中。其余约 90% 的源代码和资源文件(例如 i18n 字符串和图像资源)被打包到开发 pod 中以供另一个项目(我们称之为 X)使用。

在开发过程中,大部分更改发生在 Y 的 dev pod 部分,因此我们需要确保在 X 执行 pod install 时包含所有更改。

最近遇到一个问题:

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”

在 Google 上搜索 multiple commands produce assets.car 几个小时后,我们终于看到了一个似是而非的 explanation

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

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

New Build System中第一个覆盖第二个,就是这个原因。

当我们从[CP] Copy Pods Resource 的输出文件中手动删除${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/Assets.car 时,错误消失了,一切正常。然而,每当我们git checkout 到另一个分支,或pod install,或pod update 时,${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/Assets.car 很有可能再次出现在[CP] Copy Pods Resource 的输出文件中。我们需要一次又一次地手动删除它,既繁琐又令人沮丧。

然后我们开始质疑:这一切的幕后黑手是谁?谁负责将${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/Assets.car 添加到[CP] Copy Pods Resource 的输出文件中?我们将视线集中在Y.podspec 上,发现了以下几行:

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

我们认为我们使用了错误的resource_bundles,所以我们再次在 Google 上进行了查找。令人惊讶的是,official documentation 推荐使用 resource_bundles 而不是 resources。此外,我们找不到任何关于resource_bundles 的不当用法,让我们别无选择。

有人可以帮忙吗?我在想也许我们可以在Podfilepost_install 脚本中修补Y.xcworkspace,但我不知道怎么做。

【问题讨论】:

标签: ios xcode cocoapods podspec


【解决方案1】:

我通过在Podfile 中编写post_install 脚本解决了这个问题,该脚本会自动删除这些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

这似乎是一个 CocoaPods 的 bug,到现在还没有修复。

【讨论】:

  • 得到了undefined method 'include'? for nil:NilClass for build_phase.output_paths.include?
  • 您可能需要在 Podfile 的顶部添加 require 'xcodeproj'
  • 感谢您的回答和澄清。我实际上通过运行pod update 解决了它。问题可能在于我们正在使用的众多框架之一的旧版本。
【解决方案2】:

已经快一个月了,这里有一个更新。原来这个问题是误用了Y的另一个开发pod造成的。我们忘记在该开发 pod 的 Podspec 中使用 resource_bundles,这导致该开发 pod 的资源未打包到 .bundle 文件中,然后是 [CP] Copy Pods Resources 在输入中显式添加 xcassets文件,与Copy Bundle Resources 中的另一个文件发生冲突。

【讨论】:

  • 嗨,使用 resouce_bundles 我无法使用 Xcode 12.5 检索图像资产。 Pod.bundle 中总是有Assets.car,无法检索图片资源
【解决方案3】:

这个问题有很多方面;一些像上面描述的问题和其他时候除了 iOS 应用程序之外还有一个共享扩展或手表应用程序。在所有情况下,它都是由在创建重复的Assets.car 文件的那些目标的[CP] Copy Pods Resources 中有多个Assets.car 输出引起的。编译器显然不知道如何处理,所以我们不应该允许 CocoaPods 生成多个同名的输出文件。

the CocoaPods issue 推荐的解决方案是将其放入您的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 还修复了一个伴随的问题,即在使用新的构建系统和这个变通方法时构建性能问题。自 Xcode 12 起,旧版构建系统已正式弃用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-22
    • 2015-10-07
    • 2011-03-06
    • 1970-01-01
    相关资源
    最近更新 更多