Create a gist now

Instantly share code, notes, and snippets.

Xcode Auto-increment Build & Version Numbers
# 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. Insure that your starting build number is set to a whole integer and not a float (e.g. 1, not 1.0)
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}"
# xcode-version-bump.sh
# @desc Auto-increment the version number (only) when a project is archived for export.
# @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. Check the checkbox "Run script only when installing"
# 6. Drag the "Run Script" below "Link Binaries With Libraries"
# 7. Insure your starting version number is in SemVer format (e.g. 1.0.0)
# This splits a two-decimal version string, such as "0.45.123", allowing us to increment the third position.
VERSIONNUM=$(/usr/libexec/PlistBuddy -c "Print CFBundleShortVersionString" "${PROJECT_DIR}/${INFOPLIST_FILE}")
NEWSUBVERSION=`echo $VERSIONNUM | awk -F "." '{print $3}'`
NEWSUBVERSION=$(($NEWSUBVERSION + 1))
NEWVERSIONSTRING=`echo $VERSIONNUM | awk -F "." '{print $1 "." $2 ".'$NEWSUBVERSION'" }'`
/usr/libexec/PlistBuddy -c "Set :CFBundleShortVersionString $NEWVERSIONSTRING" "${PROJECT_DIR}/${INFOPLIST_FILE}"
@blackliteon

Thank you! Will test them right now :-)

@alokc83
alokc83 commented Mar 18, 2013

Thanks !! Its really awesome. Just the same I was looking. Works as charm. Just wondering do I have to set $2 by myself. there is no increment thing for that. actually I am not sure when to increase $2. If you can point me in right direction.

@emcmanus

These need to be pre-build actions, otherwise Xcode will only copy the updated plist every other build.

To do this:

  1. Edit Scheme
  2. Expand "Build" section by clicking disclosure triangle
  3. Select "Pre-actions"
  4. Add "New Run-Script Action"
  5. Select "Provide build settings from $YOUR_PROJECT"
  6. Add an increment script
@guizmo
guizmo commented Jul 14, 2013

xcode-build-bump.sh works like a charm!

@nebiros
nebiros commented Jul 30, 2013

At Xcode 4.6.3 (4H1503) I got this error:

/Users/nebiros/Library/Developer/Xcode/DerivedData/ZwingIt-glpdhlyoebaqcfdefdfwkgxjapnj/Build/Intermediates/ZwingIt.build/Debug-iphoneos/ZwingIt.build/Script-048F0D0017A7FDD9000E38C5.sh: line 3: File Doesn't Exist, Will Create: /Users/nebiros/Dropbox/Projects/zwingit-ios-2//Users/nebiros/Dropbox/Projects/zwingit-ios-2/ZwingIt-Info.plist + 1: syntax error: invalid arithmetic operator (error token is "'t Exist, Will Create: /Users/nebiros/Dropbox/Projects/zwingit-ios-2//Users/nebiros/Dropbox/Projects/zwingit-ios-2/ZwingIt-Info.plist + 1")

Seems like ${INFOPLIST_FILE} var has all the file path, so ${PROJECT_DIR} is not necessary.

@dan55304

@nebiros, I got the same error and removed ${PROJECT_DIR} also. However, to make it more robust I played with it a little more and found "${PROJECT_DIR}"/"${INFOPLIST_FILE}" works. Not a bash expert so I don't know why.

@SpacyRicochet

Works like a charm :)

@KiritVaghela

great

@ferbass
ferbass commented Oct 31, 2013

Works in Xcode 5 thank you

@evliu
evliu commented Nov 6, 2013

super simple. thanks!

@itsaboutcode

In Xcode 5, method of adding script have changed. Please update the comments as per instructions given on the following link.

https://developer.apple.com/library/ios/recipes/xcode_help-project_editor/Articles/AddingaRunScriptBuildPhase.html

@iPhoNewsRO

thank you! all good with xcode 5 & 7.1

@nicolastinkl

thank you ~ running with xcode 5.1.1 & ios 7.1

@wyattisimo

Seems like you're reinventing the wheel a little bit here. Why not make use of agvtool?

https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/agvtool.1.html

@jasonmcdermott

Edit: I removed the $PROJECT_DIR variable & it worked. Thanks!

I'm getting this error:

Print: Entry, "CFBundleShortVersionString", Does Not Exist
Parse Error: Unclosed Quotes
Value Required for Set Command
Command /bin/sh failed with exit code 1

Xcode 5.1.1, iOS 7.0. My Version number is "1.1.14"

@JeremyWei

works on Xcode 6.1 & iOS 8.2

@silkentrance

@wyattisimo

avgtool does not support git, or is there a newer version that does?

@kiennt
kiennt commented Jul 11, 2015

On Xcode6.3.1, I need to change ${PROJECT_DIR}/${INFOPLIST_FILE} to $PRODUCT_SETTINGS_PATH

@nthegedus

Thanks, It's working fine on Xcode 6.4

@SLboat
SLboat commented Aug 3, 2015

Grest tool!

@rswilley
rswilley commented Aug 9, 2015

Works like a charm

@troya2
troya2 commented Aug 11, 2015

I've tried using agvtool, but ran into config variable issues. Does someone have a working example of using agvtool in Xcode build settings?

@CodeEagle

Hi,I wrote a Xcode Team Adding Build Number Shell Script Here:
TeamBuildNumberScript

@KBvsMJ
KBvsMJ commented Oct 10, 2015

Thanks, It's working fine on Xcode 6.4 & iOS8.4

@maykonmeier

Thanks, it's work well!
Xcode 7.0.1 and iOS 9.0

@kristofer

good on Xcode 7.1, iOS 9

@fishercraigj

This was perfect for me. Albeit I customized mine a little bit to my needs because I wanted date/time. Thought I would share incase someone wanted something like mine.

'''Adding run script in build phases that will auto generate a build number, IE: 1511021323 (year, month, day, hour, minute) that will be used in conjunction with HockeyApp releases.'''
timestamp() {
#date +" at %H:%M:%S on %m/%d/%Y"
date +"%y%m%d%H%M"
}

buildNumber=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "${PROJECT_DIR}/${INFOPLIST_FILE}")
buildNumber=$(timestamp)
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "${PROJECT_DIR}/${INFOPLIST_FILE}"

@helsont
helsont commented Jan 21, 2016

+1 Great!

@DavidGagne

+100 Excellent

@drulang
drulang commented May 27, 2016 edited

@nebiros +1 for xcode 7.3

@welemski
welemski commented Jun 9, 2016

cool! Just what I needed
Thanks!

@aminbenarieb
aminbenarieb commented Aug 17, 2016 edited

Cool, i used this to add debug suffix to my version.

if [ "${CONFIGURATION}" == "Debug" ]; then

    VERSIONSTRING=$(/usr/libexec/PlistBuddy -c "Print CFBundleShortVersionString" "${INFOPLIST_FILE}")

    VERSIONSTRING = "$VERSIONNUM debug"

    /usr/libexec/PlistBuddy -c  "Set :CFBundleShortVersionString $VERSIONSTRING" "${INFOPLIST_FILE}"

fi

Someone knows a better approach?

@oleynikd

Thanks! Works on Xcode 8.1

@jVirus
jVirus commented Dec 12, 2016

Brilliant, thank you!

@nguyentanhungbk
nguyentanhungbk commented Dec 20, 2016 edited

work well. Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment