Time and time again a thread will come up on the xcode-users mailing list about how to make Xcode put a build number in an application’s Info.plist.
The first answer is always agvtool, but it’s the wrong answer, as agvtool is a version manager for public versions. It’s what generates all those wacky numbers you see in Mac software, such as “Mac OS X Version 10.6.2 Build 10C540″ or “Xcode version 3.2.1 build 1613.0″. It’s certainly not the simple build number people are looking for.
Everyone has their own way of dealing with this, as Xcode makes it extremely difficult to handle it in any elegant way. For example, because Xcode absolutely insists on processing Info.plist before doing anything else in a build (why? why!?), you can’t just plug in a build setting replacement and be done. Various and sundry methods exist, most of which involve either writing the value to the generated Info.plist in the built product or modifying the original Info.plist on every build.
I don’t like those methods, so I came up with one that will work nicely and store the build number as a setting within the project file. Much nicer, IMO, than having to touch your Info.plist file every build and then committing the numbers to your version control system.
- Add a new Run Script build phase to your project. Place it before all other build phases if you want the number to increase for all builds, or after if you want it only to increase on successful builds.
- Make sure “Run script only when installing” is shut off.
- Enter “
/usr/bin/osascript” as the Shell for the script. - Enter this script:
tell application "Xcode" set prj to system attribute "PROJECT_NAME" repeat with config in build configurations of project prj try set x to build setting "CURRENT_PROJECT_VERSION" of config set value of x to ((((value of x) as number) + 1) as string) on error set value of build setting "CURRENT_PROJECT_VERSION" of config to "1" end try end repeat end tell set srcrt to system attribute "SRCROOT" set iplstf to system attribute "INFOPLIST_FILE" do shell script "touch " & srcrt & "/" & iplstf
- In your Info.plist file, add or set a key like this:
<key>CFBundleVersion</key> <string>${CURRENT_PROJECT_VERSION}</string>
Figuring out how to make build numbers on a per-target or per-configuration basis is left as an exercise to the reader :-).
P.S. I hate Applescript, but it’s the only way I know of to set build settings in an Xcode project.