Incrementing Build Numbers in Xcode

(This was originally posted in April of 2009 and has been reposted here for posterity)

I admit, I’m a sucker for numbers. When I release a project, it’d be kind of cool to know how many times I’ve built it. With that in mind, and with a good deal of help from my friend Quinn and a couple of online tutorials, I was able to come up with a neat little script that can do just that.

Here’s what you need to do:

  1. Select the target you’re working with, and add a new “Run Script Phase” to it
  2. Paste the following code into the script box:
    buildNumber=$(/usr/libexec/PlistBuddy -c "Print DDBuildNumber" Info.plist)
    buildNumber=$(($buildNumber + 1))
    /usr/libexec/PlistBuddy -c "Set :DDBuildNumber $buildNumber" Info.plist
  3. Compile with impunity

The idea is simple: It uses your project’s Info.plist file to store an integer value under the key “DDBuildNumber”. Every time you build, it grabs that number, increments it, and saves it back to the plist.

A caveat: It seems like Xcode is caching Info.plist, so you may not see the change reflected immediately in Xcode. However, if you open Info.plist from the Finder (or just Quick Look it), you’ll see that the number has changed.