有条件地使用不同的资产目录构建应用 [英] Conditionally build app with different asset catalogs

查看:112
本文介绍了有条件地使用不同的资产目录构建应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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/

我还有一个config.plist,其中包含与每个Xcode配置相关联的各种唯一设置,这些设置只有在构建时才能成功复制.这是运行脚本"构建阶段的一个片段,以实现此目的:

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:

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):

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?

推荐答案

您可以执行以下操作:

  • 为每个配置添加单独的资产目录,就像通常将资产目录添加到项目中一样.假设在此示例中,我们将这些目录称为MediaForDebug.xcassets,MediaForAdHoc.xcassets和MediaForRelease.xcassets
  • 确保新创建的资产目录都是您目标的成员(因此请不要在这里排除它们,我们将在以后进行)
  • 现在,如果尚未将其添加到您的构建设置中,请添加一个自定义的EXCLUDED_SOURCE_FILE_NAMES用户定义的设置.
  • 有关要为此用户定义的设置设置的值,请​​参见下面的屏幕截图.
  • 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_NAMES User-Defined setting.
  • See the screenshot below for the values to be set for this User-Defined setting.

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

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.

祝你好运!

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

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/1546 for 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.

UPDATE 1/19/2019:使用最新的稳定Xcode(10.1)和Cocoapods(v1.5.3),它们可以与上述解决方案一起使用.无需更改Cocoapods脚本或构建步骤,只需按照说明设置EXCLUDED_SOURCE_FILE_NAMES.

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_NAMES as described.

这篇关于有条件地使用不同的资产目录构建应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆