Xcode-Increment build number only during ARCHIVE?

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

Better way of incrementing build number?

Xcode project's "Build number"

Can Xcode insert the version number into a library's 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.


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!


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


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.