Skip to content

Instantly share code, notes, and snippets.

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

Copy link

blackliteon commented Oct 2, 2012

Thank you! Will test them right now :-)

@alokc83

This comment has been minimized.

Copy link

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.

Copy link

emcmanus commented 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
@guizmo

This comment has been minimized.

Copy link

guizmo commented Jul 14, 2013

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

@nebiros

This comment has been minimized.

Copy link

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.

Copy link

dan55304 commented 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.

@SpacyRicochet

This comment has been minimized.

Copy link

SpacyRicochet commented Oct 29, 2013

Works like a charm :)

@KiritVaghela

This comment has been minimized.

Copy link

KiritVaghela commented Oct 30, 2013

great

@ferbass

This comment has been minimized.

Copy link

ferbass commented Oct 31, 2013

Works in Xcode 5 thank you

@evliu

This comment has been minimized.

Copy link

evliu commented Nov 6, 2013

super simple. thanks!

@itsaboutcode

This comment has been minimized.

Copy link

itsaboutcode commented 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

@rursache

This comment has been minimized.

Copy link

rursache commented Feb 9, 2014

thank you! all good with xcode 5 & 7.1

@nicolastinkl

This comment has been minimized.

Copy link

nicolastinkl commented May 12, 2014

thank you ~ running with xcode 5.1.1 & ios 7.1

@wyattisimo

This comment has been minimized.

Copy link

wyattisimo commented 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

@jasonmcdermott

This comment has been minimized.

Copy link

jasonmcdermott commented 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"

@ghost

This comment has been minimized.

Copy link

ghost commented Jan 29, 2015

works on Xcode 6.1 & iOS 8.2

@silkentrance

This comment has been minimized.

Copy link

silkentrance commented Mar 18, 2015

@wyattisimo

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

@kiennt

This comment has been minimized.

Copy link

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.

Copy link

nthegedus commented Jul 14, 2015

Thanks, It's working fine on Xcode 6.4

@SLboat

This comment has been minimized.

Copy link

SLboat commented Aug 3, 2015

Grest tool!

@rswilley

This comment has been minimized.

Copy link

rswilley commented Aug 9, 2015

Works like a charm

@troya2

This comment has been minimized.

Copy link

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.

Copy link

CodeEagle commented Sep 15, 2015

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

@KBvsMJ

This comment has been minimized.

Copy link

KBvsMJ commented Oct 10, 2015

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

@maykonmeier

This comment has been minimized.

Copy link

maykonmeier commented Oct 23, 2015

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

@kristofer

This comment has been minimized.

Copy link

kristofer commented Oct 23, 2015

good on Xcode 7.1, iOS 9

@fishercraigj

This comment has been minimized.

Copy link

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

@helsont

This comment has been minimized.

Copy link

helsont commented Jan 21, 2016

+1 Great!

@DavidGagne

This comment has been minimized.

Copy link

DavidGagne commented May 27, 2016

+100 Excellent

@drulang

This comment has been minimized.

Copy link

drulang commented May 27, 2016

@nebiros +1 for xcode 7.3

@welemski

This comment has been minimized.

Copy link

welemski commented Jun 9, 2016

cool! Just what I needed
Thanks!

@aminbenarieb

This comment has been minimized.

Copy link

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.

Copy link

oleynikd commented Oct 27, 2016

Thanks! Works on Xcode 8.1

@jVirus

This comment has been minimized.

Copy link

jVirus commented Dec 12, 2016

Brilliant, thank you!

@nguyentanhungbk

This comment has been minimized.

Copy link

nguyentanhungbk commented Dec 20, 2016

work well. Thank you

@yunibaek

This comment has been minimized.

Copy link

yunibaek commented Mar 19, 2017

Thank you so much ! it worked : )

@tobeyun

This comment has been minimized.

Copy link

tobeyun commented Apr 30, 2017

Awesome! Worked brilliantly.

@misanche

This comment has been minimized.

Copy link

misanche commented May 12, 2017

Good work!

@Jusung

This comment has been minimized.

Copy link

Jusung commented May 26, 2017

Work well like charm in Xcode 8.3.2

@cusster

This comment has been minimized.

Copy link

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.

Copy link

chill5018 commented Oct 7, 2017

Thanks @cusster that worked for me also

@freegor

This comment has been minimized.

Copy link

freegor commented Oct 15, 2017

Thanks @cusster! Have a nice day.

@Eric0625

This comment has been minimized.

Copy link

Eric0625 commented Nov 29, 2017

Still works in Xcode 9.1

@AppleEducate

This comment has been minimized.

Copy link

AppleEducate commented Feb 27, 2018

@LordParsley

This comment has been minimized.

Copy link

LordParsley commented Sep 21, 2018

For Xcode 10 support:

https://apple-dev.groups.io/g/xcode/topic/xcode_10_info_plist/22699344?p=,,,20,0,0,0::recentpostdate%2Fsticky,,,20,2,0,22699344

Adding the generated info.plist -- ${TARGET_BUILD_DIR}/${INFOPLIST_PATH} -- as in input file to the script got the dependencies right so Xcode does things in the desired order.

@jndefosse

This comment has been minimized.

Copy link

jndefosse commented Sep 25, 2018

For us, the script don't update the build number but by changing the script the build number change (code for xcode 10):

buildNumber=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "$PRODUCT_SETTINGS_PATH")
buildNumber=$(cut -d'.' -f2 <<<$buildNumber)
buildNumber=$(date +"%y%m%d")"."$(($buildNumber + 1))
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "$PRODUCT_SETTINGS_PATH"
#180925.1008 First is the current date and after the dot the build number
@iminsight

This comment has been minimized.

Copy link

iminsight commented Sep 26, 2018

For us, the script don't update the build number but by changing the script the build number change (code for xcode 10):

buildNumber=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "$PRODUCT_SETTINGS_PATH")
buildNumber=$(cut -d'.' -f2 <<<$buildNumber)
buildNumber=$(date +"%y%m%d")"."$(($buildNumber + 1))
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "$PRODUCT_SETTINGS_PATH"
#180925.1008 First is the current date and after the dot the build number

Thanks @jndefosse, I use the commit count as the build number.

buildNumber=$(git rev-list --count HEAD)
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "$PRODUCT_SETTINGS_PATH"
@foscomputerservices

This comment has been minimized.

Copy link

foscomputerservices commented Oct 22, 2019

I needed to compare buildNumber with the current value in the plist before updating the plist. If the plist is updated on every build, then the SwiftUI previewer will continually stop:

currentNumber=$(/usr/libexec/PlistBuddy -c "Print :CFBundleVersion" "$PRODUCT_SETTINGS_PATH")
if [ "$buildNumber" -ne "$currentNumber" ] ; then
  /usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "$PRODUCT_SETTINGS_PATH"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.