What is a Build number and what is its use? Is it the 开发者_JAVA技巧same as version number?
Additionally, if you add CFBuildDate as a string and CFBuildNumber as a string into your info.plist, the following shell script (when added to your run script build phase /bin/bash will automatically update your build number and date:
# Auto Increment Version Script
buildPlist=${INFOPLIST_FILE}
CFBuildNumber=$(/usr/libexec/PlistBuddy -c "Print CFBuildNumber" $buildPlist)
CFBuildNumber=$(($CFBuildNumber + 1))
/usr/libexec/PlistBuddy -c "Set :CFBuildNumber $CFBuildNumber" $buildPlist
CFBuildDate=$(date +%Y%m%d%H%M%S)
/usr/libexec/PlistBuddy -c "Set :CFBuildDate $CFBuildDate" $buildPlist
I'm using Xcode 4.3.3 (4E3002) and managed to have build number (CFBundleVersion) automatically increased every build with the following steps:
- Select the menu item Product/Edit Scheme... (Command+<)
- Expanded the Build phase
- Select Pre-actions
- Clicked on the plus sign to add "New Run Script Action"
- Entered "/bin/bash" as the "Shell"
- Select one of the targets in "Provide build settings from"
Entered the following code:
buildPlist=$SRCROOT/$INFOPLIST_FILE PlistBuddy="/usr/libexec/PlistBuddy" CFBundleVersion=`$PlistBuddy -c "Print CFBundleVersion" $buildPlist` CFBundleVersion=$(($CFBundleVersion + 1)) $PlistBuddy -c "Set :CFBundleVersion $CFBundleVersion" $buildPlist
Have fun!
Many people use the build number to track the total number of times a project is "built" (simply compiled for small projects, maybe some more involved process for larger ones).
The build number is an absolute value incremented every build. A version number, on the other hand, is an arbitrary "label" or "tag" used as shorthand for a specific build number.
So say you've built your project 123 times, your build number is "123", but you might decide to refer to that as "version 1.0" for simplicity sake. If you build another 20 times to fix a bug, your build number is 143, but your version is "1.01" or "1.1" or whatever you decide to name it.
I've also seen projects that base their build numbers on source control. So a CVS/SVN team might use the revision number as their build number. I've also seen git projects that use the SHA of the latest commit as a build number (although some management tools assume the build number is an incremental value — and obviously in the case of SHAs, it's not).
The Build number is an internal number that indicates the current state of the app. It differs from the Version number in that it's typically not user facing and doesn't denote any difference/features/upgrades like a version number typically would.
Think of it like this:
- Build (CFBundleVersion): The number of the build. Usually you start this at 1 and increase by 1 with each build of the app. It quickly allows for comparisons of which build is more recent and it denotes the sense of progress of the codebase. These can be overwhelmingly valuable when working with QA and needing to be sure bugs are logged against the right builds.
- Marketing Version (CFBundleShortVersionString) - The user-facing number you are using to denote this version of your app. Usually this follows a Major.minor version scheme (e.g. MyAwesomeApp 1.2) to let users know which releases are smaller maintenance updates and which are big deal new features.
To use this effectively in your projects, Apple provides a great tool called agvtool. It allows you to easily set both the build number and the marketing version. It is particularly useful when scripting (for instance, easily updating the build number on each build or even querying what the current build number is). It can even do more exotic things like tag your SVN for you when you update the build number.
To use it:
- Set your project in Xcode, under Versioning, to use "Apple Generic".
- In terminal
- agvtool new-version 1 (set the Build number to 1)
- agvtool new-marketing-version 1.0 (set the Marketing version to 1.0)
See the man page of agvtool for a ton of good info
Build Number is for minor updates(usually get pretty high 1 at the beginning and can end at 1000), ex: if you change a couple lines of code but doesn't change the logic or make new features in your update. Version Number is for rather large updates ex: new features in your app. Then you can change it from 1.8 to 2.0.
If you want to use a DATE field for CFBuildDate, use:
# get UTC date
CFBuildDate=$(date -u +"%a %b %d %T GMT %Y")
btw great tip from cdasher
This works for me in Xcode 6:
cd ${SOURCE_ROOT}
buildPlist=${SOURCE_ROOT}/${PROJECT_NAME}/${PROJECT_NAME}-Info.plist
PlistBuddy="/usr/libexec/PlistBuddy"
buildNumber=`git rev-list HEAD --count`
buildNumber=$(($buildNumber + 1))
$PlistBuddy -c "Set :CFBundleVersion $buildNumber" $buildPlist
This works in Xcode 6 and also modifies the dSYM info dictionary, only when archiving:
if [ "${CONFIGURATION}" = "Release" ]; then
buildPlist="${SOURCE_ROOT}/${PROJECT_NAME}/${PROJECT_NAME}-Info.plist"
PlistBuddy="/usr/libexec/PlistBuddy"
CFBundleVersion=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "${TARGET_BUILD_DIR}/${INFOPLIST_PATH}")
CFBundleVersion=$(($CFBundleVersion + 1))
$PlistBuddy -c "Set :CFBundleVersion $CFBundleVersion" "$buildPlist"
$PlistBuddy -c "Set :CFBundleVersion $CFBundleVersion" "$INFOPLIST_FILE"
$PlistBuddy -c "Set :CFBundleVersion $CFBundleVersion" "${DWARF_DSYM_FOLDER_PATH}/${WRAPPER_NAME}.dSYM/Contents/Info.plist"
fi
CFBundleVersion (Bundle version) - aka Marketing version
which will be shown on your product's page on Appstore.
CFBundleShortVersionString (Bundle versions string, short) - bundle number, usually used as a minor version which can be seen by TestFlight users only
Here's how can you get them in Swift (4+)
programmatically:
let buildNumber = Bundle.main.infoDictionary?[kCFBundleVersionKey as String] as? String,
let appVersion = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString")
I needed the marketing version in my shell script once and here's how I got it using xcode build tools
:
xcodebuild -showBuildSettings -project ${SDK_PROJECT} | sed '1d;s/^ *//;s/"/\\"/g;s/ = \(.*\)/="\1"/;s/ = /=/;s/UID.*//' > xcodebuild-env.tmp
source xcodebuild-env.tmp
echo "${MARKETING_VERSION}"
精彩评论