Auto-increment build number in Xcode 11 / Xcode 12

Xcode 11 moved version handling to the .pbxproj file, in addition to Info.plist, which broke my script to automatically increase build number on each build. Solution for Xcode 11/12 is shown below.

Background – pre Xcode 11 / Xcode 12

Before Xcode 11 the “normal” way to auto increment build number was to read current build number from Info.plist using the MacOS PlistBuddy tool and then increment that number and write the updated build number back to Info.plist, again using PlistBuddy. Or you could use avgtool or some 3rd party solution.

However, with Xcode11 (and currently Xcode12) version number and buildnumber are set in the pbxproj file, and Info.plist only reference what is set in pbxproj.

This change got my old script to generate an error on build: Command PhaseScriptExecution failed with a nonzero exit code

$(CURRENT_PROJECT_VERSION) + 1: syntax error: operand expected (error token is "$(CURRENT_PROJECT_VERSION) + 1")
Command PhaseScriptExecution failed with a nonzero exit code

This is what Info.plist now looks like in Xcode11 / Xcode12, i.e. there is no longer an actual build number to read from Info.plist, only a reference to what is set in the pbxproj file.

<key>CFBundleShortVersionString</key> 
<string>$(MARKETING_VERSION)</string> 

<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>

Solution for Xcode 11 / Xcode 12

So, in Xcode 11/12 we now need to update the pbxproj file instead of Info.plist.

Use this script to auto increment build number:

buildNumber=$(xcodebuild -showBuildSettings | grep CURRENT_PROJECT_VERSION | tr -d 'CURRENT_PROJECT_VERSION =')
buildNumber=`echo $buildNumber +1|bc`
xcrun agvtool new-version $buildNumber

To add the script in Xcode 12, follow these steps:

  1. Select your Project Target (Target, not Project)
  2. Select Build Phases
  3. Add a new Build Phase by clicking the plus (+) icon on top left
  4. If you wish you can rename the new Build phase, e.g. Set Build Number
  5. Copy the script above and paste it
  6. You can leave all other Build Phase settings as default
  7. Done – Build your project and watch build number increment with 1 for each build

This was latest tested on Xcode 12.5 and macOS Big Sur 11.5.2 on a MacBook Air M1

Did this solution help you?

I hope this helped! Please comment if it did, or if you had any problem with it. Also let me know if you got it working (or not) using other versions or if you found a better solution – it will help me and others!

5 comments

  1. I will try to get this working on a new XCode 13 project.
    Question: is the unmatched parenthesis “$(“ in the script intentional?

  2. Sorry, yes it works under Xcode 13.
    And I hadn’t seen the horizontal scroll bar (on Safari on iPad) so I hand’t realised there was more text.
    The scroll bar is visible on Safari on macOS. Thank you.

  3. It seems with Xcode 13.4, if you used agvtool in a Build Phase, Xcode would cancel the build because the project file was touched.

  4. This worked great! Thank you!
    My only clarification would be in step 3 to suggest to use “New Run Script Phase”

Leave a comment

Your email address will not be published. Required fields are marked *