Is there anyway to increase the version and bundle numbers of an Android project automatically with each build run?
I seem to spend a lot of time tweaking the build number and really I’m only ever +1’ing it. I’m a sole dev so there isn’t any real scheme behind the version numbering.
I’ve found some old scripts online but most seem to require Unity cloud build or just no-longer work.
I did something similar recently (for cloud build but should work locally too). It calls a function in ‘pre-export’ that will set your build number before the build begins. It gets the build number from a free web service called https://increment.build. Just put your game code below (in the part marked INSERT_ID_HERE). The web service just gives you back an ever increasing number each time you call it.
#if UNITY_EDITOR
using UnityEditor;
using UnityEngine;
using UnityEditor.Callbacks;
using UnityEditor.Build;
using System;
using System.IO;
using UnityEngine.Networking;
/* This class will increment the iOS build number on each build - this is useful for uploading builds
to TestFlight (iOS) as it requires a more recent build number on each iteration */
public class IncrementBuildNumber : IPreprocessBuild
{
public int callbackOrder { get { return 0; } }
public void OnPreprocessBuild(BuildTarget target, string path)
{
string build = PlayerSettings.Android.bundleVersionCode.ToString();
// Retrieve auto-incrementing build number from server service
UnityWebRequest www = UnityWebRequest.Get( "https://increment.build/***INSERT_ID_HERE***" );
www.SendWebRequest();
while( www.isDone == false )
{
}
if( (www.isNetworkError) || (string.IsNullOrEmpty( www.downloadHandler.text )) )
{
Debug.LogErrorFormat( "Error retrieving auto-increment build number:" + www.error );
}
else
{
Debug.LogFormat( "Retrieved auto-increment build number:" + www.downloadHandler.text );
//Debug.Log( www.downloadHandler.text );
build = www.downloadHandler.text;
}
PlayerSettings.bundleVersion = "1.0";
PlayerSettings.iOS.buildNumber = build;
try
{
PlayerSettings.Android.bundleVersionCode = Convert.ToInt32( build );
}
catch
{
Debug.LogErrorFormat( "Error converting auto-increment build number to an integer: {0}" , build );
}
}
}
#endif
I’m getting an error that CloudBuild doesn’t exist in the current namespace. Any ideas how I can resolve that or adjust for local building on a sole PC?
I’ve updated my code in the post above to use that system so hopefully it should work now if you copy it again - I’m not at my PC just now so can’t really test it. Let me know if it doesn’t work for you and I’ll have a look next week.
p.s. I’ve just realised that since you’re doing it locally you could probably simplify the script a lot. You can just retrieve the current android build number from PlayerSettings and increment it by one (and then set it back again). The advantage of getting it from that online service is that you can have multiple people doing builds and it’ll still work - you also don’t need to check in the changes to any source control.
using UnityEditor;
using UnityEngine;
using UnityEditor.Callbacks;
using System;
using System.IO;
using UnityEngine.Networking;
/* This class will increment the iOS build number on each build - this is useful for uploading builds
to TestFlight (iOS) as it requires a more recent build number on each iteration */
public class IncrementBuildNumber : MonoBehaviour
{
#if UNITY_CLOUD_BUILD
public static void PreExport( UnityEngine.CloudBuild.BuildManifestObject manifest )
{
string build = manifest.GetValue( "buildNumber" , null );
// Retrieve auto-incrementing build number from server service
UnityWebRequest www = UnityWebRequest.Get( "https://increment.build/NAMEOFAPPGOESHERE" );
www.SendWebRequest();
while( www.isDone == false )
{
}
if( (www.isNetworkError) || (string.IsNullOrEmpty( www.downloadHandler.text )) )
{
Debug.LogErrorFormat( "Error retrieving auto-increment build number:" + www.error );
}
else
{
Debug.LogFormat( "Retrieved auto-increment build number:" + www.downloadHandler.text );
//Debug.Log( www.downloadHandler.text );
build = www.downloadHandler.text;
// Update manifest with the build number so the 'auto update app' still works
// when checking the current app build number
manifest.SetValue( "buildNumber" , build );
}
PlayerSettings.bundleVersion = "1.0";
PlayerSettings.iOS.buildNumber = build;
try
{
string androidBuild = Convert.ToInt32(build).ToString("D3");
PlayerSettings.Android.bundleVersionCode = Convert.ToInt32( androidBuild );
}
catch
{
Debug.LogErrorFormat( "Error converting auto-increment build number to an integer: {0}" , build );
}
}
#endif
}
Thanks tone for the script, but im getting an error when the cloud build finished it says "
The type or namespace name ‘Callbacks’ does not exist in the namespace ‘UnityEditor’ (are you missing an assembly reference?)" any idea on how to solve it ?
Hi, it might happen if you haven’t put that script in an ‘Editor’ folder. If it’s definitely in an Editor folder then I’m not certain, I’ll give it a go when I’m working after the weekend.
You don’t really need to do that, if you use the auto incrementing build number then local builds will also run that script too.
I guess you could setup a shell script to run when the build finishes but it might be difficult to commit the changes to source control because the build machine would need to have your credentials setup.