Automatic build numbers in Xcode

This one is a relatively simple mechanism for automatic build numbers. This increments the build number every time a successful build is made in Xcode.

Step 1: Create a new Run Script

In the target setting, add a new Run Script after the Link Binary With Libraries. Change the shell to /usr/bin/perl -w and paste the following script:

# Auto increment version number from build common file

use strict;

die "$0: Must be run from Xcode" unless $ENV{"BUILT_PRODUCTS_DIR"};

# Get the current revision number and use it to set the CFBundleVersion value
open FH, "build.count" or die;
my $version = join("",);
close(FH);

++$version;
print "Build $versionn"; 

open FH, ">build.count" or die;
print FH $version;
close(FH);

$version = "1.0 ($version)";

# Update info.plist in build product. Use defaults command

my $INFO = "$ENV{BUILT_PRODUCTS_DIR}/$ENV{WRAPPER_NAME}/Info";
my @defaults = ( 'defaults', 'write', $INFO, 'CFBundleVersion', "'$version'" );
exec @defaults;

Note: If this is for a MacOS X application instead of an iPhone application, then the third to last line should be:

my $INFO = "$ENV{BUILT_PRODUCTS_DIR}/$ENV{WRAPPER_NAME}/Contents/Info";

Step 2: Create the Build Count file.

Note that this Perl script pulls the version number from a file “build.count” which is at the same directory level as the project file. You should create a file containing the single character “0”.

Step 3: Build

Now you can test to make sure that the CFBundleVersion setting in the Info file is updated to “1.0 (1)”, “1.0 (2)”, etc. (The “1.0 ” part is set by the build script; change there to update the major/minor build numbers, or prefix the build with anything you want.)

This works by incrementing the build number in the build.count file, then using the defaults command-line tool (‘man defaults’ for more) to update the info.plist file after it is copied into the executable bundle.

The original concept came from Red Sweater Blog: Automatic Build Sub-Versioning in Xcode. However, in my case I wanted to do the build version count between check-ins, and we are currently using GIT. (But that’s another horror story.)

6 thoughts on “Automatic build numbers in Xcode

  1. Pretty cool trick. One thing you might want to do to throttle the sheer size of the number, without (probably) impacting the usefulness of the build number, is to only increment on Release builds.

    Something like:

    if [[ ${CONFIGURATION} == “Release” ]]
    then
    # Your increment script here…
    fi

    Like

  2. Pingback: Development Chaos Theory » Blog Archive » Automatic build numbers … | IPhoneMate

  3. This is a good script and the use of defaults to modify the plist is inspired. Note that according to the man entry for defaults it is slated to loose its ability to operate on plist files directly.

    Unfortunately there is a typo which renders the script almost completely ineffective (it will increment from 0 to 1 and that’s it)!

    my $version = join(“”,);

    should read

    my $version = join(“”,);

    Another way to limit the build number would be to click the run script only when installing option in the script panel window. This would only run the script when the deployment build setting was enabled.

    Like

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s