57

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.

3
  • 6
    You want to add two to the build number every time that you archive it?
    – lnafziger
    Mar 24, 2012 at 22:05
  • I want to add 1 before it's archived then add 1 after its archived. So in the example above, the archive itself will have build number 22. But I do not want to increase build number everytime I build it, only when I archive it.
    – jsherk
    Mar 24, 2012 at 22:46
  • 1
    Thanks! I found this question while making my own archive script and love your "increment the build number twice" idea. I ended up incorporating it into mine: github.com/musictheory/rchook (Feel free to copy if it helps you)
    – iccir
    Sep 24, 2012 at 2:39

3 Answers 3

80

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!

10
  • 1
    That is awesome! Thanks! Works perfect. In Xcode 4.2 you do not even need to create the script in a directory somewhere, you can just copy and paste it right into the Build Phase section itself.
    – jsherk
    Mar 24, 2012 at 23:09
  • 1
    Ok, one correction.. In Xcode 4.2, it needs to be == Release instead of == Archive
    – jsherk
    Mar 25, 2012 at 0:10
  • 2
    this might sound silly but.. why do you have to add this twice? won't that increment it by two?
    – Ben
    Jul 17, 2012 at 2:36
  • 1
    Yeah, that's what he wanted. :-) If you only want it to bump once, then only add it once.
    – lnafziger
    Jul 17, 2012 at 3:03
  • 6
    In Xcode 5, you can skip the if [ $CONFIGURATION == Release ]; then check and add this to the Post-actions of your Archive scheme instead (Project > Scheme > Edit Scheme…).
    – J. Perkins
    Apr 3, 2014 at 13:51
23

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

6
  • 2
    I also had great success adding this script to the Post-Actions of the scheme as suggested by @starkos above. Thanks! Sep 11, 2014 at 0:38
  • 2
    Remove the ${PROJECT_DIR}/ part of the paths if getting Print: Entry, "CFBundleVersion", Does Not Exist.
    – Paul
    Oct 15, 2014 at 7:43
  • Question: Why exactly do it twice?
    – akw
    Mar 8, 2015 at 22:01
  • See the OP. It asked to have the build number incremented before and after the build
    – wdes
    Apr 13, 2015 at 22:07
  • @ccwasden Increment the build number is only after the archive, is there any way to do it before?
    – evya
    May 28, 2018 at 7:27
12

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.

4
  • Should I add this script before or after the build? Aug 24, 2016 at 4:15
  • Before. You want the new version to be included.
    – orkoden
    Aug 24, 2016 at 8:34
  • 1
    Wow - amazed this doesn't have more votes, it's seemingly the perfect answer. Thanks @orkoden
    – NSTJ
    Apr 22, 2017 at 14:02
  • 1
    Although cleaner than @ccwasden 's solution, this one makes xcode reload the xcodeproj and collapse the grouping in the left navigator everytime I build (Xcode 9.1 9B55)
    – codrut
    Nov 20, 2017 at 14:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.