Xcode-Increment build number only during ARCHIVE?

XcodeMacosXcode4xcode4.2Osx Lion

Xcode Problem Overview


I have found a few other posts that show how to add a script to increment the build number with a script:

https://stackoverflow.com/questions/9258344/xcode-better-way-of-incrementing-build-number

https://stackoverflow.com/questions/7371345/xcode-projects-build-number

https://stackoverflow.com/questions/4694070/can-xcode-insert-the-version-number-into-a-librarys-filename-when-building

But what I want to do, is only increase the build number when I use ARCHIVE (both before and after).

Example: If current build number is 21, then when I choose Product > Archive the build number will be increased to 22, it will go through its process of building and creating the Archive file with the build number of 22, and then when it is finished archiving, it will increase the build number to 23.

Xcode Solutions


Solution 1 - Xcode

Add the following script, as in the example listed in the first link that you posted, BUT do it twice. Once at the beginning of the build and once at the end:

if [ $CONFIGURATION == Release ]; then
    echo "Bumping build number..."
    plist=${PROJECT_DIR}/${INFOPLIST_FILE}

# increment the build number (ie 115 to 116)
    buildnum=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "${plist}")
    if [[ "${buildnum}" == "" ]]; then
        echo "No build number in $plist"
        exit 2
    fi

    buildnum=$(expr $buildnum + 1)
    /usr/libexec/Plistbuddy -c "Set CFBundleVersion $buildnum" "${plist}"
    echo "Bumped build number to $buildnum"

else
    echo $CONFIGURATION " build - Not bumping build number."
fi

Many thanks to the authors of the questions that you have linked to in your question for the information that got me started on this answer!

Solution 2 - Xcode

This is very similar to @Inafziger's answer, but a more concise set of code, with the added benefit that the check for "Release" is done with a checkbox in XCode rather than a runtime variable:

enter image description here

Follow these instructions twice, dragging one to the beginning and one to the end (one to run before build and one to run after build):

# xcode-build-bump.sh
# @desc Auto-increment the build number every time the project is run. 
# @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"
# 6. Ensure that your starting build number is set to a whole integer and not a float (e.g. 1, not 1.0)
# 7. Check the checkbox "Run script only when installing"
 
buildNumber=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "${PROJECT_DIR}/${INFOPLIST_FILE}")
buildNumber=$(($buildNumber + 1))
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "${PROJECT_DIR}/${INFOPLIST_FILE}"

See https://gist.github.com/sekati/3172554

Solution 3 - Xcode

Xcode includes the command line tool agvtool to increase version numbers. So you don't have to do everything manually with PListBuddy.

xcrun agvtool next-version -all

increases your build number.

xcrun agvtool new-marketing-version 2.0

sets a new user visible version number.

See the full documentation for details.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjsherkView Question on Stackoverflow
Solution 1 - XcodelnafzigerView Answer on Stackoverflow
Solution 2 - XcodeccwasdenView Answer on Stackoverflow
Solution 3 - XcodeorkodenView Answer on Stackoverflow