Automatic build numbering increase with xCode

I had this problem some days ago where i wanted to automatically increase the build number of my Application in xCode, every time that i actually made a new build. xCode comes with a tool called agvtool which is supposed to fill this gap, but the main problem with this is that it needs the project to be actually closed, for some reason that i certainly ignore.

After some searches, i finished at this interesting post by Chris Hanson, but this was not exactly what i wanted to do, again, i wanted a simple approach to increase the build number with every build.

So this is what i did.

First I created a new configuration settings file in xCode, as shown below.

Creating a new Configuration Settings File in xCode

Creating a new Configuration Settings File in xCode

After this, i ended up with an empty configuration file, so i added the following inside it:

CURRENT_PROJECT_VERSION = 1

xCode uses this variable to store your application’s build number, so we are going to tell it to use this file in order to get the value for the CURRENT_PROJECT_VERSION variable.

Go to your target’s properties, be sure to select All configurations and All Settings, then base your target’s config in your newly created version.xcconfig file, and set the version variable to our new version variable as shown below:

Configuring the Target to use the new variable in the version.xcconfig file.

Configuring the Target to use the new variable in the version.xcconfig file.

After all this is done, all you have to do is create a new “Run Script” build phase, and place the following inside it:

NEW_VERSION=`cat "$SRCROOT/version.xcconfig" | awk '/CURRENT_PROJECT_VERSION/ { print $3 + 1 }'`
sed -i '' "s/CURRENT_PROJECT_VERSION = .*/CURRENT_PROJECT_VERSION = $NEW_VERSION/" "$SRCROOT/version.xcconfig"
CURRENT_PROJECT_VERSION=$NEW_VERSION
touch "$SRCROOT/version.xcconfig"

Obviously, if you changed the file or variable names, you will have to adapt the script as needed.
Now, every time you build the app, the build number is automatically increased by one, and included in your application’s plist file as shown below:

Build number being shown in the built app.

Build number being shown in the built app.

A whole thanks goes to the guys at #macdev on freenode Diabolik and KonaBlend who helped with the bash scripting!


No comments yet, leave yours!.