How to auto increment Xcode project build version numbers without plist

28th Sep, 2022 | xcode

One thing I do with all my XCode projects is setup the build to automatically increment the build number. By doing this I always have a reliable unique version so I can reference builds in the archive against builds in other places like the App Store, version control or builds that have been manually distributed.

I have previously blogged about a bash script to do this. It's a bit hacky but it has been working perfectly...until recently. Now, in the latest versions of XCode, the plist that was modified before no longer holds the source of truth so my script cannot mutilate the plist to increase the build number.

So, I needed to find a new way to do this. I certainly did not want to go back to the very old days of manually updating it whenever I remembered!

First attempts

At first I started digging around the project files and found project.pbxproj contains CURRENT_PROJECT_VERSION which holds the build version. Directly hacking this file within a build script felt even hackier than my previous method so I started looking at xcodebuild -showBuildSettings to get the current value.

This returned the value but it was very slow and for some reason did not seem to work with grep. It also was not clear how to inject the new value.

After some more digging it turns out the process to do everything was under my nose the whole time!

Enter agvtool

Fortunately there is already a utility to handle project versioning exactly how I wanted:

  • First build version is 1
  • After each build it is incremented by 1
  • I manage the release version manually as before

Setup

The setup is easier than before, and it no longer feels like a hack!

  1. Enable the agvtool
    1. Select your project in the file explorer and select the "build settings" tab
    2. Filter by the phrase "versioning"
    3. Change "Versioning system" to "Apple Generic"
  2. Switch off user script sandboxing, so we can update the project files
    1. Search inside the "build settings" tab for "User script sandboxing"
    2. Set this parameter to "No"
  3. Create a build phase script
    1. Select the "build phases" tab
    2. Click the "+" button at the top left and select "New run script phase"
    3. Open the "run script" section and paste this command to increment the build number using the avgtool
      1. xcrun agvtool next-version -all
    4. Important - uncheck the "Based on dependency analysis" checkbox

After that every build will increase the build number. Magic!