Auto Incrementing Build Numbers on XCode 4.2

Here is one small tip how to automatically increment build number in XCode 4.2 the one which you can see in target summary.

Here is how to increment it automatically with every build.

  • click first on target and then on build phases
  • click on the plus button in the lower right corner and chose user script
  • now in script field write
buildNumber=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" ${INFOPLIST_FILE})
buildNumber=$(($buildNumber + 1))
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" ${INFOPLIST_FILE}

Now every time when you build your project build number will increase. Of course you can always change it manually and it will continue increasing like it should.

Here is small explanation what this script do:

  • buildNumber is shell variable where we will store build number from XCode. To be able to read it from Project-Info.plist file we need to use small tool from Apple, called PlistBuddy. It’s included by default but it’s not in the path and that means that we need to specify full path to it, in our case /usr/libexec. We need to specify command sent to PlistBuddy and file on which that command will be performed, in our case command is Print CFBundleVersion which is property of build number in XCode written in our Project-Info.plist file and last argument is file name which we get from variable INFOPLIST_FILE.
  • here we increase value of buildNumber, same like int++ but shell is a bit different so we need to write it like this for XCode to be able to run it
  • here we run PlistBuddy with command Set where we say to it what to set and where, in our case we use CFBundleVersion increased by one and we are storing it to our Project-Info.plist file