xcode 根据活动配置将文件复制到捆绑包

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5389219/
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-14 20:36:39  来源:igfitidea点击:

Copy files to bundle depending on active configuration

xcodeiosconfigurationbundle

提问by Nick Weaver

is it possible to control which files are copied to the bundle depending on the active configuration? I don't want to add another target for this, so this not an option.

是否可以根据活动配置控制将哪些文件复制到包中?我不想为此添加另一个目标,所以这不是一个选项。

The real life example is a splash video which is in fact 8mb in size and is long. Every start of the app shows this video which is annoying. I don't want to break too much code so the solution is a very short splash video, which is the candidate to be copied to the bundle when the debug configuration is active.

现实生活中的例子是一个启动视频,它实际上是 8mb 大小并且很长。应用程序的每次启动都会显示这个令人讨厌的视频。我不想破坏太多代码,所以解决方案是一个非常短的启动视频,当调试配置处于活动状态时,它是要复制到包的候选者。

Yes, I could make the debug video very small so it doesn't hurt if it is shipped with the release but for the sake of learning new things I need a way to control which file is copied depending on the configuration. I suppose a Run Script would do this, but maybe there is a simple solution which I don't see.

是的,我可以将调试视频制作得非常小,因此如果它随发行版一起提供就不会受到影响,但是为了学习新事物,我需要一种方法来根据配置控制复制哪个文件。我想运行脚本可以做到这一点,但也许有一个我没有看到的简单解决方案。

Thanks in advance!

提前致谢!

回答by Nick Weaver

I didn't find any better solution than using a Run Script. Best reference is Copy file to the App Resources directory if debug configuration is selected.

我没有找到比使用运行脚本更好的解决方案。如果选择了调试配置,最好的参考是将文件复制到应用程序资源目录

I solved my problem with this run script:

我用这个运行脚本解决了我的问题:

RESOURCE_PATH=$SRCROOT/ResourcesCopiedByRunScript

FILENAME_IN_BUNDLE=splashVideo.mp4

BUILD_APP_DIR=${BUILT_PRODUCTS_DIR}/${FULL_PRODUCT_NAME}

if [ "$CONFIGURATION" == "Debug" ]; then
    cp "$RESOURCE_PATH/shortDebugSplashVideo.mp4" "$BUILD_APP_DIR/$FILENAME_IN_BUNDLE"
else
    cp "$RESOURCE_PATH/productionSplashVideo.mp4" "$BUILD_APP_DIR/$FILENAME_IN_BUNDLE"
fi

回答by JWWalker

If you can give the two different files the same name, you can put them in the build products folders for the appropriate configuration. Then add one of them to the project, and make it a reference relative to the build product.

如果您可以为这两个不同的文件指定相同的名称,则可以将它们放在构建产品文件夹中以进行适当的配置。然后将其中之一添加到项目中,并使其成为相对于构建产品的参考。

If you want to have the files live elsewhere, you can put symbolic links in the build products folders. But I'm not sure if it would work if the files have different names but the symlinks have the same name.

如果您想让文件存在于其他地方,您可以将符号链接放在构建产品文件夹中。但是我不确定如果文件具有不同的名称但符号链接具有相同的名称是否会起作用。

I've done this trick on Mac projects, I'm assuming it would work for iOS too.

我已经在 Mac 项目上完成了这个技巧,我假设它也适用于 iOS。

Edit: Here's an example of something I've done: I want the debug build of my app to link against the debug build of a certain library, and I want the release build of the app to link against the release build of the library. So I put the debug library (or a symlink to it) into the Debug subfolder of the app build folder, and I put the release library into the Release subfolder of the app build folder. Then, when I add the library to the project, I specify that the reference is "relative to build product".

编辑:这是我所做的一个例子:我希望我的应用程序的调试版本链接到某个库的调试版本,我希望应用程序的发布版本链接到库的发布版本。因此,我将调试库(或指向它的符号链接)放入应用程序构建文件夹的 Debug 子文件夹中,并将发布库放入应用程序构建文件夹的 Release 子文件夹中。然后,当我将库添加到项目时,我指定引用是“相对于构建产品”。

回答by Ramis

The best way to do it using Exclude Source File Namesunder Build Settingsas it shown in the image.

最好的方法是使用构建设置下的排除源文件名,如图所示。

enter image description here

在此处输入图片说明

回答by Igor

You can replace one file to another using run script before compile step

您可以在编译步骤之前使用运行脚本将一个文件替换为另一个文件

RESOURCE_PATH=$SRCROOT/SampleProject/SupportingFiles/

LICENSE=license.txt
LICENSE_PROD=license_prod.txt

if [ "$CONFIGURATION" == "Release" ]; then
cp $RESOURCE_PATH/$LICENSE_PROD $RESOURCE_PATH/$LICENSE
fi