xcode 有条件地构建具有不同资产目录的应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28458966/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:38:27  来源:igfitidea点击:

Conditionally build app with different asset catalogs

iosxcodexcodebuild

提问by codeman

My XCode project is setup in a way that has multiple configurations, allowing me to use the same code base for different variations of my app, but have unique elements in each such as app name, version, bundle identifier, icon, launch screen, etc. I've followed this website in order to do most of the setup: http://appfoundry.be/blog/2014/07/04/Xcode-Env-Configuration/

我的 XCode 项目的设置方式具有多种配置,允许我对应用程序的不同变体使用相同的代码库,但每个项目都有独特的元素,例如应用程序名称、版本、包标识符、图标、启动屏幕等. 我关注这个网站是为了完成大部分设置:http: //appfoundry.be/blog/2014/07/04/Xcode-Env-Configuration/

I also have a config.plist containing various unique settings associated with each XCode configuration that successfully only gets copied upon being built. Here's a snippet of the Run Script build phase in order to do that:

我还有一个 config.plist,其中包含与每个 XCode 配置相关联的各种独特设置,这些设置仅在构建时成功复制。这是运行脚本构建阶段的一个片段,以便做到这一点:

RESOURCE_PATH=${SRCROOT}/${PRODUCT_NAME}/config/${CONFIGURATION}

BUILD_APP_DIR=${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.app

echo "Copying all files under ${RESOURCE_PATH} to ${BUILD_APP_DIR}"
cp -v "${RESOURCE_PATH}/"* "${BUILD_APP_DIR}/"

My next goal is to be able to copy a particular configuration's asset catalog when being built, so as to avoid bundling all of the different configuration's images into the build, causing it to become bloated. I've tried the same solution as above with the Run Script, changing the copy line to include the recursive option (since asset catalog is essentially a directory):

我的下一个目标是能够在构建时复制特定配置的资产目录,以避免将所有不同配置的图像捆绑到构建中,导致它变得臃肿。我已经使用 Run Script 尝试了与上面相同的解决方案,更改复制行以包含递归选项(因为资产目录本质上是一个目录):

cp -rv "${RESOURCE_PATH}/"* "${BUILD_APP_DIR}/"

However, when I do this, my app fails to build and says it's unable to find the app icon and launch image. Any ideas?

但是,当我这样做时,我的应用程序无法构建并说它无法找到应用程序图标和启动图像。有任何想法吗?

回答by Mike Seghers

This is what you can do to make this work:

这是你可以做的事情:

  • Add a separate asset catalog per configuration, just as you would normally add an asset catalog to your project. Let's assume for this example we call these catalogs MediaForDebug.xcassets, MediaForAdHoc.xcassets and MediaForRelease.xcassets
  • Make sure the newly created asset catalogs are all member of your target (so DO NOT exclude them here, we'll do that later on)
  • Now, if not already added to your build settings, add a custom EXCLUDED_SOURCE_FILE_NAMESUser-Defined setting.
  • See the screenshot below for the values to be set for this User-Defined setting.
  • 为每个配置添加单独的资产目录,就像您通常向项目添加资产目录一样。让我们假设在这个例子中我们将这些目录称为 MediaForDebug.xcassets、MediaForAdHoc.xcassets 和 MediaForRelease.xcassets
  • 确保新创建的资产目录都是目标的成员(所以不要在这里排除它们,我们稍后会这样做)
  • 现在,如果尚未添加到您的构建设置中,请添加自定义EXCLUDED_SOURCE_FILE_NAMES用户定义设置。
  • 请参阅下面的屏幕截图,了解要为此用户定义设置设置的值。

EXCLUDED_SOURCE_FILE_NAMES for config dependant asset catalogs

EXCLUDED_SOURCE_FILE_NAMES 用于配置相关资产目录

By doing this, Xcode will only compile the appropriate asset catalogs, and ignore the others. I've tested this on one of our projects, and it works as intended.

通过这样做,Xcode 将只编译适当的资产目录,而忽略其他的。我已经在我们的一个项目中对此进行了测试,它按预期工作。

Good luck!

祝你好运!

REMARK: If you are using CocoaPods, you might run into troubles because CocoaPods adds a build step of its own to compile your assets, and those of your dependencies into on .car file. See https://github.com/CocoaPods/CocoaPods/issues/1546for the discussion on this issue. TL;DR: make sure this step doesn't execute in your build phase, or edit the CocoaPods script they execute to not do the asset building.

备注:如果您使用 CocoaPods,您可能会遇到麻烦,因为 CocoaPods 添加了自己的构建步骤来编译您的资产,并将您的依赖项编译到 .car 文件中。有关此问题的讨论,请参阅https://github.com/CocoaPods/CocoaPods/issues/1546。TL;DR:确保此步骤不在您的构建阶段执行,或编辑它们执行的 CocoaPods 脚本以不进行资产构建。

UPDATE 1/19/2019: With the latest stable Xcode (10.1) and Cocoapods (v1.5.3) they work out of the box with the solution above. No need to alter the Cocoapods scripts or build steps, just set EXCLUDED_SOURCE_FILE_NAMESas described.

2019 年 1 月 19 日更新:使用最新的稳定版 Xcode (10.1) 和 Cocoapods (v1.5.3),他们可以使用上述解决方案开箱即用。无需更改 Cocoapods 脚本或构建步骤,只需EXCLUDED_SOURCE_FILE_NAMES按照描述进行设置。