Post

Incrementing the Build Number

As I get closer to releasing my application, I’m starting to run into all the small things that you have to do to release an application. One of those, I’ve recently found out, is the need to change the application’s version and/or build number every time you upload it to Apple. To manage them manually, you can navigate to the Project file, chose the target and select the General pane. You will find Version and Build under the Identity section.

While I was developing the app and deploying it to my phone with Xcode there was not issue with the app having the same version and build number. It wasn’t until I wanted to upload my application to the AppStore to distribute it on TestFlight that I ran into the issue. Each version uploaded to App Store Connect needs to have a unique version number. At first I just manually incremented the build number, but I quickly realized that this would be a step better automated. I know me, and I will forget this step enough times in the future that it will tick me off.

I did a lot of research on various methods and came across a rather simple solution. Apple comes with a built-in tool called agvtool that can be used to increment the build and version numbers, so I modified my build phases and added a new script phase that ran a simple script to run the tool.

1
2
cd "${PROJECT_DIR}";
agvtool bump

This seemed to work at first, but then I started to see a lot of issues with previews in my Xcode environment. It took me a little while to figure it out, but adding this as a build phase completely broke my previews by sending my build into an infinite loop. I would make a change and the system would try to update the build, which would increment the build number, which would restart the build, which would increment.. well, you get it.

I did some more digging and came across an interesting solution. Since I use Git to manage my source code, I can use it to check and see if the project file has been modified or not, and if it has not yet been modified then increment the build number.

1
2
3
4
5
6
7
8
9
cd "${PROJECT_DIR}"

status=$(git status --porcelain)
# Change to the name of your project file
proj_file="Project.xcodeproj/project.pbxproj"

if [[ "${#status}" != 0 ]] && [[ $status != *"M ${proj_file}"* ]]; then
    agvtool bump
fi

It still has the same problem of messing up the preview build the first time it runs, but afterwards it will not run until the change has been check in.

It may not be the best solution, but it will work for me until I figure out how to use Xcode Cloud to build my app.