如何在 Xcode 4 中自动增加捆绑版本?

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

How to auto-increment Bundle Version in Xcode 4?

iosxcodebuildxcode4

提问by markdorison

I am trying to figure out how to have the Bundle version number increment automatically in my Xcode 4 project (for ad-hoc and release builds). I found some scripts online that purport to do this but I am unsure of whether to place them in the "Pre-actions" or "Post-actions". I also am unsure what value I should place in the plist; a number that the script will then change or a variable?

我试图弄清楚如何在我的 Xcode 4 项目中自动增加 Bundle 版本号(用于临时和发布版本)。我在网上找到了一些声称要执行此操作的脚本,但我不确定是将它们放在“操作前”还是“操作后”中。我也不确定应该在 plist 中放置什么值;脚本将更改的数字或变量?

All the options that I have tried thus far do not seem to work so any help would be greatly appreciated.

到目前为止,我尝试过的所有选项似乎都不起作用,因此我们将不胜感激。

Below is the most recent script I was attempting to use:

以下是我尝试使用的最新脚本:

conf=${CONFIGURATION}
arch=${ARCHS:0:4}
# Only increase the build number on Device and AdHoc/AppStore build
if [ $conf != "Debug" ] && [ $conf != "Release" ] && [ $arch != "i386" ]
then
buildPlist=${INFOPLIST_FILE}
buildVersion=$(/usr/libexec/PlistBuddy -c "Print CFBuildVersion" $buildPlist)
buildNumber=$(/usr/libexec/PlistBuddy -c "Print CFBuildNumber" $buildPlist)
buildNumber=$(($buildNumber + 1))
/usr/libexec/PlistBuddy -c "Set :CFBuildNumber $buildNumber" $buildPlist
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildVersion.$buildNumber" $buildPlist
/usr/libexec/PlistBuddy -c "Set :CFBundleShortVersionString $buildVersion.$buildNumber" $buildPlist
fi

回答by TONy.W

1, Set CFBundleVersion to 1.0.1 or something like x.x.x

1、设置CFBundleVersion为1.0.1或者xxx之类的

1

1

2, Add build phases to run shell script autoVersion.sh

2、添加构建阶段来运行shell脚本autoVersion.sh

2

2

3, save below script named autoVersion.sh

3、保存下面的脚本名为autoVersion.sh

#!/bin/sh
# Auto Increment Version Script
# set CFBundleVersion to 1.0.1 first!!!
# the perl regex splits out the last part of a build number (ie: 1.1.1) and increments it by one
# if you have a build number that is more than 3 components, add a '\.\d+' into the first part of the regex.
buildPlist=${INFOPLIST_FILE}
newVersion=`/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "$buildPlist" | /usr/bin/perl -pe 's/(\d+\.\d+\.)(\d+)/.(+1)/eg'`
#echo $newVersion;
/usr/libexec/PListBuddy -c "Set :CFBundleVersion $newVersion" "$buildPlist"

4, run shell: cp autoVersion.sh ~/Documents/ and chmod 777 ~/Documents/autoVersion.sh

4、运行shell:cp autoVersion.sh ~/Documents/和chmod 777 ~/Documents/autoVersion.sh

5, Build & Enjoy it. :)

5,构建并享受它。:)

perl code from: https://gist.github.com/1436598

perl 代码来自:https: //gist.github.com/1436598

回答by Canopus

You may find the following post helpful:

您可能会发现以下帖子很有帮助:

Auto-Incrementing Build Numbers for Release Builds in Xcodefrom iPhone Development by Jeff LaMarche http://iphonedevelopment.blogspot.com/2011/07/auto-incrementing-build-numbers-for.html

Xcode 中发布版本的自动递增版本号来自 iPhone 开发作者 Jeff LaMarche http://iphonedevelopment.blogspot.com/2011/07/auto-incrementing-build-numbers-for.html

回答by Anirudh Ramachandran

The same idea as Alix's answer, but much simpler:

与 Alix 的答案相同的想法,但要简单得多:

buildNumber=`/bin/date +%Y%m%d%H%M%S`
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "${PROJECT_DIR}/${INFOPLIST_FILE}"

Add this as a Run Scriptitem on Build Phaseson your Target. This has the advantage of being monotonically increasingas well.

将此作为Run Script项目添加Build Phases到您的Target. 这也具有单调递增的优点。

回答by Adam Waite

For anyone wanting to integrate version incrementing into a command line build script (perhaps for continuous integration), see the following commands:

对于任何想要将版本递增集成到命令行构建脚本(可能用于持续集成)的人,请参阅以下命令:

    # cd into the project folder containing the plist
    cd /Users/waitea/iOS/E.ON/iOS/trunk/Eon

    # grab the version numbers
    major_version=$(grep -C 2 CFBundleVersion App-Info.plist | grep -o '[0-9]\+.[0-9]\+.[0-9]\+')
    major_version_min=$(echo $major_version | grep -o '[0-9]\+.[0-9]\+\.')
    minor_version=$(echo $major_version | grep -o '[0-9]\+$')

    # increment the minor version
    increment=`expr $minor_version + 1`
    incremented_whole_version="$major_version_min$increment"

    # replace the build number in the plist using perl
    /usr/bin/perl -p -i -e "s/$major_version/$incremented_whole_version/g" App-Info.plist

That will increment the rightmost number in a x.x.x style version number. Tweak the reg-ex's to alter for your convention.

这将增加 xxx 样式版本号中最右边的数字。调整 reg-ex 以适应您的约定。

Took me a while so I thought I'd share to give back to the community!

花了我一段时间,所以我想我会分享以回馈社区!

EDIT - I created a continuous integration system that'll do this for you

编辑 - 我创建了一个持续集成系统来为你做这件事

https://github.com/adamwaite/XcodeProject

https://github.com/adamwaite/XcodeProject

回答by Alix

This might help you. I am using it in my projects. https://gist.github.com/alokc83/5207294

这可能对你有帮助。我在我的项目中使用它。 https://gist.github.com/alokc83/5207294

#!/bin/sh
# xcode-build-number-generator.sh
# @desc Automaticvally create build number every time using curent day, month and year
# @usage
# 1. Select: your Target in Xcode
# 2. Select: Build Phases Tab
# 3. Select: Add Build Phase -> Add Run Script
# 4. Paste code below in to new "Run Script" section
# 5. Drag the "Run Script" below "Link Binaries With Libraries"


#Credits 
# sekati@github for intial direction about automatic versioning
# http://www.codinghorror.com/blog/2007/02/whats-in-a-version-number-anyway.html (For unferstanding the Software Versoining)
#Feel free to leave comment or report issues


MONTH=`date | awk '{print }'`

case "$MONTH" in
  'Jan' )
         MONTHNUMBER=1
     ;;
    'Feb' )
         MONTHNUMBER=2
    ;;
    'Mar' )
    MONTHNUMBER=3
    echo "Month is $MONTHNUMBER"
    ;;
    'Apr' )
         MONTHNUMBER=4
    ;;
    'May' )
         MONTHNUMBER=5
        ;;
    'Jun' )
         MONTHNUMBER=6
        ;;
    'Jul' )
         MONTHNUMBER=7
        ;;
    'Aug' )
         MONTHNUMBER=8
        ;;
    'Sep' )
         MONTHNUMBER=9
        ;;
    'Oct' )
         MONTHNUMBER=10
        ;;
    'Nov' )
         MONTHNUMBER=11
        ;;
    'Dec' )
         MONTHNUMBER=12
        ;;
esac

DATE=`date | awk '{print }'`
echo "Date = $DATE"
YEAR=`date | awk '{print }'`
echo "Date = $YEAR"

### only uncomment section below if testing the format in terminal
#echo "BuildNumber1 = $MONTH$DATE$YEAR"
#echo "or BUILD NUMBER = $DATE$MONTH$YEAR"
#echo "or BUILD NUMBER = $MONTHNUMBER$DATE$YEAR Format is |Month Number Date Year|"
#echo "or BUILD NUMBER = $DATE$MONTHNUMBER$YEAR format is |Date MonthNumber Year|"
############################

#### Uncomment only one one style or last one will be in effect
#buildNumber=$MONTH$DATE$YEAR
#buildNumber=$DATE$MONTH$YEAR
buildNumber=$MONTHNUMBER$DATE$YEAR
#buildNumber=$DATE$MONTHNUMBER$YEAR


echo "Final Build number is $buildNumber"
## Below command write buildNumber in the property list
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "${PROJECT_DIR}/${INFOPLIST_FILE}"

回答by Dru Freeman

I've found that using tiered xcconfigs helps this problem.

我发现使用分层 xcconfigs 有助于解决这个问题。

Working on complex builds with apps, libraries, and SDKs you have to be able to coordinate not merely build numbers per project, but build number compatibility.

使用应用程序、库和 SDK 处理复杂的构建时,您不仅必须能够协调每个项目的构建编号,还必须能够协调构建编号的兼容性。

You can make a build management header that is effectively a text file with build iteration numbers (or versioning info i.e. beta, dev, rel) and import it through the xcconfig import chain per project.

您可以制作一个构建管理标头,它实际上是一个带有构建迭代号(或版本信息,即 beta、dev、rel)的文本文件,并通过每个项目的 xcconfig 导入链导入它。

At that point you can have a target build script step that will embed your build/versioning info. This is also best done by putting holding text in your plist and running PlistBuddy on your derived file/built file sections. (This way your source control changes are minimal)

此时,您可以有一个目标构建脚本步骤,它将嵌入您的构建/版本控制信息。这也最好通过将保持文本放入 plist 并在派生文件/构建文件部分运行 PlistBuddy 来完成。(这样您的源代码控制更改最少)

If you can write a build execution script that does the necessary build number twiddling (or better yet, use a system like bamboo which creates the file for you), you can keep that separate from your code. Granted, if you need to do it and keep track, you may have to check in the changed build number to allow it to increment.

如果您可以编写一个构建执行脚本来处理必要的构建号(或者更好的是,使用像竹子这样的系统为您创建文件),您可以将其与您的代码分开。当然,如果您需要这样做并保持跟踪,您可能必须签入更改的内部版本号以允许它增加。

I've been able as a result of this to maintain build numbers along the line of: 2.0.3 b34 (3473) Where we have a build number and an SVN checkout build point. (Please no git hazing, I'm old school)

因此,我能够按照以下方式维护内部版本号: 2.0.3 b34 (3473) 我们有内部版本号和 SVN 结帐构建点。(请不要欺负我,我是老派)

Pre/Post actions are more for Uber notifications or processes: Email that the build started/failed/ etc Copy the done project to the done project server.

Pre/Post 操作更多用于 Uber 通知或流程:构建开始/失败/等的电子邮件 将完成的项目复制到完成的项目服务器。

Everything else works better as a Build Script.

其他一切都可以作为构建脚本更好地工作。

(And as always: make the script phase call an external script file. DON'T push your script into the project, it's hell on source controlling the project file)

(和往常一样:让脚本阶段调用外部脚本文件。不要将您的脚本推送到项目中,这是对项目文件进行源代码控制的地狱)

Hope this helps.

希望这可以帮助。

回答by Jay

FWIW - this is what I'm currently using to increase the build number only for release builds(which includes archiving). Works fine under Xcode 5.1.

FWIW - 这是我目前用于增加仅用于发布版本(包括存档)的版本号的方法。在 Xcode 5.1 下工作正常。

Just copy/paste the snippet into a Run scriptbuild phase directly in Xcode:

只需直接在 Xcode中将代码片段复制/粘贴到运行脚本构建阶段即可:

buildnum=$(/usr/libexec/PlistBuddy -c "Print :CFBundleVersion" "$PRODUCT_SETTINGS_PATH")

if [ "$CONFIGURATION" = "Release" ]; then
buildnum=$((buildnum + 1))
echo "Build number updated to $buildnum"
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildnum" "$PRODUCT_SETTINGS_PATH"
fi;

回答by rordulu

If you're using a version system like "x.x.x" you can add this run script. It'll increase the version number (x.x.x+1) every time a new build is made:

如果你使用像“xxx”这样的版本系统,你可以添加这个运行脚本。每次进行新构建时,它都会增加版本号 (xxx+1):

if [ "${CONFIGURATION}" != "Debug" ]; then

VERSIONNUM=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "${PROJECT_DIR}/${INFOPLIST_FILE}")

NEWSUBVERSION=`echo $VERSIONNUM | awk -F "." '{print }'`

NEWSUBVERSION=$(($NEWSUBVERSION + 1))

NEWVERSIONSTRING=`echo $VERSIONNUM | awk -F "." '{print  "."  "."  ".'$NEWSUBVERSION'" }'`

/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $NEWVERSIONSTRING" "${PROJECT_DIR}/${INFOPLIST_FILE}"

fi

回答by Sergey V.

I haven't found a great solution for Xcode 10.1. I have modified some scripts to reach the goal. And everything now works fine.

我还没有为 Xcode 10.1 找到一个很好的解决方案。我修改了一些脚本以达到目标。现在一切正常。

version=$(/usr/libexec/PlistBuddy -c "Print :CFBundleShortVersionString" "${INFOPLIST_FILE}")
build=$(/usr/libexec/PlistBuddy -c "Print :CFBundleVersion" "${INFOPLIST_FILE}")
/usr/libexec/PlistBuddy -c "Set :PreferenceSpecifiers:1:DefaultValue ${version}" "${CODESIGNING_FOLDER_PATH}/Settings.bundle/Root.plist"