Auto incrementing build number in Xcode 4

March 11, 2013

Updated: Please see Auto incrementing build number in Xcode revisited

I’ve recently been doing a bit of iOS development and have started using HockeyApp for distributing beta versions and collecting crash reports. Hockey requires that your builds have a unique version number, they have a great knowledge base article showing How to do versioning for beta versions on iOS or Mac. The only problem I found with their solution was that the file containing the build number needs to exist beforehand. So, here’s an update to the script to create the file if it doesn’t already exist:

echo "Updating build number..."
if [ "$CONFIGURATION" == "Release" ]; then
    set -o noclobber
    echo "Build file: ${SRCROOT}/buildnumber.xcconfig"
    echo "BUILD_NUMBER = 0" > "${SRCROOT}/buildnumber.xcconfig"
    set +o noclobber
    /usr/bin/perl -pe 's/(BUILD_NUMBER = )(\d+)/$1.($2+1)/eg' -i buildnumber.xcconfig
fi

Also, don’t forget to add the buildnumber.xcconfig file to your .gitignore.