Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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

This comment has been minimized.

Show comment Hide comment
@blackliteon

blackliteon Oct 2, 2012

Thank you! Will test them right now :-)

Thank you! Will test them right now :-)

@alokc83

This comment has been minimized.

Show comment Hide comment
@alokc83

alokc83 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.

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

This comment has been minimized.

Show comment Hide comment
@emcmanus

emcmanus Apr 23, 2013

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

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

This comment has been minimized.

Show comment Hide comment
@guizmo

guizmo Jul 14, 2013

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

guizmo commented Jul 14, 2013

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

@nebiros

This comment has been minimized.

Show comment Hide comment
@nebiros

nebiros 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.

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

This comment has been minimized.

Show comment Hide comment
@dan55304

dan55304 Jul 30, 2013

@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.

@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

This comment has been minimized.

Show comment Hide comment
@SpacyRicochet

SpacyRicochet Oct 29, 2013

Works like a charm :)

Works like a charm :)

@KiritVaghela

This comment has been minimized.

Show comment Hide comment
@KiritVaghela

KiritVaghela Oct 30, 2013

great

great

@ferbass

This comment has been minimized.

Show comment Hide comment
@ferbass

ferbass Oct 31, 2013

Works in Xcode 5 thank you

ferbass commented Oct 31, 2013

Works in Xcode 5 thank you

@evliu

This comment has been minimized.

Show comment Hide comment
@evliu

evliu Nov 6, 2013

super simple. thanks!

evliu commented Nov 6, 2013

super simple. thanks!

@itsaboutcode

This comment has been minimized.

Show comment Hide comment
@itsaboutcode

itsaboutcode Jan 2, 2014

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

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

This comment has been minimized.

Show comment Hide comment
@iPhoNewsRO

iPhoNewsRO Feb 9, 2014

thank you! all good with xcode 5 & 7.1

thank you! all good with xcode 5 & 7.1

@nicolastinkl

This comment has been minimized.

Show comment Hide comment
@nicolastinkl

nicolastinkl May 12, 2014

thank you ~ running with xcode 5.1.1 & ios 7.1

thank you ~ running with xcode 5.1.1 & ios 7.1

@wyattisimo

This comment has been minimized.

Show comment Hide comment
@wyattisimo

wyattisimo Jul 23, 2014

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

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

This comment has been minimized.

Show comment Hide comment
@jasonmcdermott

jasonmcdermott Aug 16, 2014

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"

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"

@ghost

This comment has been minimized.

Show comment Hide comment
@ghost

ghost Jan 29, 2015

works on Xcode 6.1 & iOS 8.2

ghost commented Jan 29, 2015

works on Xcode 6.1 & iOS 8.2

@silkentrance

This comment has been minimized.

Show comment Hide comment
@silkentrance

silkentrance Mar 18, 2015

@wyattisimo

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

@wyattisimo

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

@kiennt

This comment has been minimized.

Show comment Hide comment
@kiennt

kiennt Jul 11, 2015

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

kiennt commented Jul 11, 2015

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

@nthegedus

This comment has been minimized.

Show comment Hide comment
@nthegedus

nthegedus Jul 14, 2015

Thanks, It's working fine on Xcode 6.4

Thanks, It's working fine on Xcode 6.4

@SLboat

This comment has been minimized.

Show comment Hide comment
@SLboat

SLboat Aug 3, 2015

Grest tool!

SLboat commented Aug 3, 2015

Grest tool!

@rswilley

This comment has been minimized.

Show comment Hide comment
@rswilley

rswilley Aug 9, 2015

Works like a charm

rswilley commented Aug 9, 2015

Works like a charm

@troya2

This comment has been minimized.

Show comment Hide comment
@troya2

troya2 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?

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

This comment has been minimized.

Show comment Hide comment
@CodeEagle

CodeEagle Sep 15, 2015

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

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

@KBvsMJ

This comment has been minimized.

Show comment Hide comment
@KBvsMJ

KBvsMJ Oct 10, 2015

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

KBvsMJ commented Oct 10, 2015

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

@maykonmeier

This comment has been minimized.

Show comment Hide comment
@maykonmeier

maykonmeier Oct 23, 2015

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

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

@kristofer

This comment has been minimized.

Show comment Hide comment
@kristofer

kristofer Oct 23, 2015

good on Xcode 7.1, iOS 9

good on Xcode 7.1, iOS 9

@fishercraigj

This comment has been minimized.

Show comment Hide comment
@fishercraigj

fishercraigj Nov 2, 2015

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}"

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

This comment has been minimized.

Show comment Hide comment
@helsont

helsont Jan 21, 2016

+1 Great!

helsont commented Jan 21, 2016

+1 Great!

@DavidGagne

This comment has been minimized.

Show comment Hide comment
@DavidGagne

DavidGagne May 27, 2016

+100 Excellent

+100 Excellent

@drulang

This comment has been minimized.

Show comment Hide comment
@drulang

drulang May 27, 2016

@nebiros +1 for xcode 7.3

drulang commented May 27, 2016

@nebiros +1 for xcode 7.3

@welemski

This comment has been minimized.

Show comment Hide comment
@welemski

welemski Jun 9, 2016

cool! Just what I needed
Thanks!

welemski commented Jun 9, 2016

cool! Just what I needed
Thanks!

@aminbenarieb

This comment has been minimized.

Show comment Hide comment
@aminbenarieb

aminbenarieb Aug 17, 2016

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?

aminbenarieb commented Aug 17, 2016

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

This comment has been minimized.

Show comment Hide comment
@oleynikd

oleynikd Oct 27, 2016

Thanks! Works on Xcode 8.1

Thanks! Works on Xcode 8.1

@jVirus

This comment has been minimized.

Show comment Hide comment
@jVirus

jVirus Dec 12, 2016

Brilliant, thank you!

jVirus commented Dec 12, 2016

Brilliant, thank you!

@nguyentanhungbk

This comment has been minimized.

Show comment Hide comment
@nguyentanhungbk

nguyentanhungbk Dec 20, 2016

work well. Thank you

nguyentanhungbk commented Dec 20, 2016

work well. Thank you

@yunibaek

This comment has been minimized.

Show comment Hide comment
@yunibaek

yunibaek Mar 19, 2017

Thank you so much ! it worked : )

Thank you so much ! it worked : )

@tobeyun

This comment has been minimized.

Show comment Hide comment
@tobeyun

tobeyun Apr 30, 2017

Awesome! Worked brilliantly.

tobeyun commented Apr 30, 2017

Awesome! Worked brilliantly.

@misanche

This comment has been minimized.

Show comment Hide comment
@misanche

misanche May 12, 2017

Good work!

Good work!

@Jusung

This comment has been minimized.

Show comment Hide comment
@Jusung

Jusung May 26, 2017

Work well like charm in Xcode 8.3.2

Jusung commented May 26, 2017

Work well like charm in Xcode 8.3.2

@cusster

This comment has been minimized.

Show comment Hide comment
@cusster

cusster Aug 15, 2017

I may missed something out when setting up my project, but the above path, ${PROJECT_DIR}/${INFOPLIST_FILE}, throws an exception on my end. I noticed that the ${INFOPLIST_FILE} contains absolute path to my project's Info.plist. Removing the ${PROJECT_DIR}/ did it for me. Using Xcode 8.3.2

cusster commented Aug 15, 2017

I may missed something out when setting up my project, but the above path, ${PROJECT_DIR}/${INFOPLIST_FILE}, throws an exception on my end. I noticed that the ${INFOPLIST_FILE} contains absolute path to my project's Info.plist. Removing the ${PROJECT_DIR}/ did it for me. Using Xcode 8.3.2

@chill5018

This comment has been minimized.

Show comment Hide comment
@chill5018

chill5018 Oct 7, 2017

Thanks @cusster that worked for me also

Thanks @cusster that worked for me also

@freegor

This comment has been minimized.

Show comment Hide comment
@freegor

freegor Oct 15, 2017

Thanks @cusster! Have a nice day.

freegor commented Oct 15, 2017

Thanks @cusster! Have a nice day.

@Eric0625

This comment has been minimized.

Show comment Hide comment
@Eric0625

Eric0625 Nov 29, 2017

Still works in Xcode 9.1

Still works in Xcode 9.1

@AppleEducate

This comment has been minimized.

Show comment Hide comment

AppleEducate commented Feb 27, 2018

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