Blog

the writings of

Automatically Update Build Number in XCode

August 29, 2014

How to automatically update a build number in XCode based on the project's version number

This is a post expanding on the process(es) described in the accepted answer on this Stack Overflow question.

The goal here is to automate a "Build Number" in XCode where it is in the format

1.2.3.4

Where:

  • 1 is the major version number
  • 2 is the minor version number
  • 3 is the revision/maintenance release number
  • 4 is the number of builds that have been required to get to this point

The reason my script (below) differs from the proposed answer is that I wanted to be responsible for updating the "Version", which would then automatically generate/feed the "Build number".

The build number will be generated based on the version number. You then only need to update the version number.
The build number will be generated based on the version number. You then only need to update the version number.

Steps to automate the build number

1. Follow the steps from the Stack Overflow thread

Namely:

In Xcode 4.2 - 5.0:

  1. Load your Xcode project.
  2. In the left hand pane, click on your project at the very top of the hierarchy. This will load the project settings editor.
  3. On the left-hand side of the center window pane, click on your app under the TARGETS heading. You will need to configure this setup for each project target.
  4. Select the Build Phases tab.
    • In Xcode 4, at the bottom right, click the Add Build Phase button and select Add Run Script.
    • In Xcode 5, select Editor menu > Add Build Phase > Add Run Script Build Phase.
  5. Drag-and-drop the new Run Script phase to move it to just before the Copy Bundle Resources phase (when the app-info.plist file will be bundled with your app).
  6. In the new Run Script phase, set Shell: /bin/bash.
  7. Copy and paste the following into the script area for integer build numbers:

2. Replace the suggested script

Instead of using the script suggested by the Stack Overflow answer, use this script:


echo "Updating Build"


buildNumber=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "$INFOPLIST_FILE")
version=$(/usr/libexec/PlistBuddy -c "Print CFBundleShortVersionString" "$INFOPLIST_FILE")

echo "Current Version: $version"
echo "Latest Build: $buildNumber"

major=$(echo $version | cut -d'.' -f 1)
minor=$(echo $version | cut -d'.' -f 2)
revision=$(echo $version | cut -d'.' -f 3)
build=$(echo $buildNumber | cut -d'.' -f 4)

echo "Major: $major"
echo "Minor: $minor"
echo "Revision: $revision"
echo "build: $build"

build=$(($build + 1))

echo "This build: $build"

/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $major.$minor.$revision.$build" "$INFOPLIST_FILE"

That should be it! Enjoy.

blog comments powered by Disqus