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.

Community
  • 1
  • 1
jsherk
  • 6,128
  • 8
  • 51
  • 83
  • 6
    You want to add two to the build number every time that you archive it? – lnafziger Mar 24 '12 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 '12 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: https://github.com/musictheory/rchook (Feel free to copy if it helps you) – iccir Sep 24 '12 at 02:39

3 Answers3

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!

lnafziger
  • 25,760
  • 8
  • 60
  • 101
  • 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 '12 at 23:09
  • 1
    Ok, one correction.. In Xcode 4.2, it needs to be `== Release` instead of `== Archive` – jsherk Mar 25 '12 at 00:10
  • Thanks, corrected! That's actually what I had but thought that it was for something else, lol. – lnafziger Mar 25 '12 at 00:39
  • 2
    this might sound silly but.. why do you have to add this twice? won't that increment it by two? – Ben Jul 17 '12 at 02:36
  • 1
    Yeah, that's what he wanted. :-) If you only want it to bump once, then only add it once. – lnafziger Jul 17 '12 at 03:03
  • Oh, that's why then, it was already provided, lol. I'll edit it into my answer as well when I get to my computer. – lnafziger Jul 11 '13 at 13:26
  • So looking back over this, I ended up writing this script using a combination of different sources as well as testing on my end. I used three lines of your script (which showed how to read/write too/from the plist) but had to find a way to actually answer his question. It isn't like I just copied your script (which is how you make it sound in your comment). – lnafziger Jul 11 '13 at 13:45
  • I disagree, there is more than 3 lines, but it doesn't matter. – trojanfoe Jul 11 '13 at 19:57
  • 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 03 '14 at 13:51
  • @starkos yes - works better here, because `if [ $CONFIGURATION == Release ]` will ONLY work if your Archive Build Configuration is set to "Release"... Sometimes we want to create a "Debug" archive for internal testing via TestFlight. Just remember to set "Provide build settings from" to your default target, so it has access to the variables. – jowie May 20 '14 at 11:48
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

chasew
  • 8,438
  • 7
  • 41
  • 48
  • 2
    I also had great success adding this script to the Post-Actions of the scheme as suggested by @starkos above. Thanks! – metatheoretic Sep 11 '14 at 00:38
  • 2
    Remove the `${PROJECT_DIR}/` part of the paths if getting `Print: Entry, "CFBundleVersion", Does Not Exist`. – Paul Oct 15 '14 at 07:43
  • Question: Why exactly do it twice? – akw Mar 08 '15 at 22:01
  • See the OP. It asked to have the build number incremented before and after the build – chasew Apr 13 '15 at 22:07
  • @ccwasden Increment the build number is only after the archive, is there any way to do it before? – evya May 28 '18 at 07:27
  • @evya Create a build script as described, then drag it to be one of the first scripts to run (eg. before "link binaries with libraries") if you want it to run before – chasew May 31 '18 at 15:39
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.

orkoden
  • 18,946
  • 4
  • 59
  • 50