16

Simple question, but seems very hard to find.

I am building an Android and iOS game. And I want to extract the version (i.e. "2.0.1") of the app (to display a popup if there is a newer version on App Store/Google Play).

Anyone know how to do this programmatically?

5
  • The bundle, where is it located: On your own server or is it hosted content of in-app purchases? What's the responsibility of the bundle? Real assets or just versioning information to trigger an update of the app?
    – Kay
    Jun 20, 2013 at 9:20
  • Have look at my udpated answer below if you still like to have a solution for getting the bundle version.
    – Kay
    Sep 12, 2013 at 22:12
  • I have already released the game, but will have a look at it for future projects! Thanks!
    – Sunkas
    Sep 13, 2013 at 8:08
  • @Sunkas accepted answer is outdated - could you change the accepted answer? Ta.
    – jhabbott
    Dec 22, 2016 at 14:22
  • I've changed the accepted answer. However, I have not verified it to be correct. Is this the correct way here on StackOverflow?
    – Sunkas
    Jan 19, 2017 at 10:20

3 Answers 3

17

OUTDATED: While this answer was perfectly valid at time of writing, the information it contains is outdated. There is a better way to do this now, see this answer instead. The answer has been preserved for historic reasons.

Update

I improved the solution described below massively and made an open source project (MIT license) hosted at github out of it. At a glance it does not only provides access to the bundle version of the currently running app but also tracks the history of previous bundle version in a pretty convienient way - at least since installation of the plugin or some manual adjustments are required.
So look at:

BundleVersionChecker at github
Usage and more details


I just found another and pretty convenient solution. 2 classes are needed:

The first one is an editor class checking the current PlayerSettings.bundleVersion. I called it BundleVersionChecker and it has to be placed in Assets/Editor. It functions as a code generator that simply generates a very simple static class containing just a version string, if the bundle version has changed:

using UnityEngine;
using UnityEditor;
using System.IO;

[InitializeOnLoad]
public class BundleVersionChecker
{
    /// <summary>
    /// Class name to use when referencing from code.
    /// </summary>
    const string ClassName = "CurrentBundleVersion";

    const string TargetCodeFile = "Assets/Scripts/Config/" + ClassName + ".cs";

    static BundleVersionChecker () {
        string bundleVersion = PlayerSettings.bundleVersion;
        string lastVersion = CurrentBundleVersion.version;
        if (lastVersion != bundleVersion) {
            Debug.Log ("Found new bundle version " + bundleVersion + " replacing code from previous version " + lastVersion +" in file \"" + TargetCodeFile + "\"");
            CreateNewBuildVersionClassFile (bundleVersion);
        }
    }

    static string CreateNewBuildVersionClassFile (string bundleVersion) {
        using (StreamWriter writer = new StreamWriter (TargetCodeFile, false)) {
            try {
                string code = GenerateCode (bundleVersion);
                writer.WriteLine ("{0}", code);
            } catch (System.Exception ex) {
                string msg = " threw:\n" + ex.ToString ();
                Debug.LogError (msg);
                EditorUtility.DisplayDialog ("Error when trying to regenerate class", msg, "OK");
            }
        }
        return TargetCodeFile;
    }

    /// <summary>
    /// Regenerates (and replaces) the code for ClassName with new bundle version id.
    /// </summary>
    /// <returns>
    /// Code to write to file.
    /// </returns>
    /// <param name='bundleVersion'>
    /// New bundle version.
    /// </param>
    static string GenerateCode (string bundleVersion) {
        string code = "public static class " + ClassName + "\n{\n";
        code += System.String.Format ("\tpublic static readonly string version = \"{0}\";", bundleVersion);
        code += "\n}\n";
        return code;
    }
}

The 2nd class is called CurrentBundleVersion. It's the above mentioned simple class generated by BundleVersionChecker and it is accessible from your code. It will be regenerated by BundleVersionChecker automatically whenever its version string is not equal to the one found in PlayerSettings.

public static class CurrentBundleVersion
{
    public static readonly string version = "0.8.5";
}

Because it is generated code you don't have to care about it, just commit it into your version control system.

So anywhere in your code you can just write:

if (CurrentBundleVersion != "0.8.4") {
    // do migration stuff
}

I am currently working on a more sophisticated version. This will contain some version tracking to do something like

if (CurrentBundleVersion.OlderThan (CurrentBundleVersion.Version_0_8_5) //...

5
  • Works great. Thanks!
    – Mic
    Jan 12, 2016 at 13:43
  • After upgrading Unity to 5.3.2f1, and MonoDevelop to 5.9.6, it stopped working. I am now getting a build error in BundleVersionChecker.cs: The name 'CurrentBundleVersion' does not exist in the current context. Did something change in Unity regarding the exposure of symbols between Editor and Game classes ? I tried to add [InitializeOnLoad] (in UnityEditor namespace) before defining the class CurrentBundleVersion, but it didn't help :(
    – Mic
    Feb 9, 2016 at 8:29
  • OK I managed to work around the build error by not checking the version change in the BundleVersionChecker() constructor, and just always calling directly CreateNewBuildVersionClassFile(PlayerSettings.bundleVersion). I don't see a terrible performance impact as it is just called everytime the Unity editor window gets focus.
    – Mic
    Feb 9, 2016 at 8:38
  • Sorry for the missing code formatting in the comment: static BundleVersionChecker () { string bundleVersion = PlayerSettings.bundleVersion; Debug.Log ("Replacing bundle version " + bundleVersion + " in file \"" + TargetCodeFile + "\""); CreateNewBuildVersionClassFile (bundleVersion); }
    – Mic
    Feb 9, 2016 at 8:40
  • For Unity 2018.x.x I just created two CurrentBundleVersion files, CurrentBundleVersionEditor and CurrentBundleVersion. I update both of them and from runtime or from the Editor I use one or another.
    – mayo
    Nov 5, 2018 at 19:29
14

The UnityEngine.Application.version is a static member that seems to be the runtime equivalent of UnityEditor.PlayerSettings.bundleVersion.

7

OUTDATED: While this answer was perfectly valid at time of writing, the information it contains is outdated. There is a better way to do this now, see this answer instead. The answer has been preserved for historic reasons, please consider this before down-voting.

In a word: No. You can not get the app bundle version directly from Unity.

In fact, there is a function called PlayerSettings.bundleVersion which can read what the number you set in the player setting, but unfortunately it is an editor class function so you are not able to use it in runtime. (In fact you can change this number in Xcode, so the number set in Unity's player setting might be wrong).

An easy way is writing your version number in the code, and update the number every time you submit and update your app. It is a little dangerous because you could forget to do it before release. So you may need a check list for it.

Another way is write a plugin. Xcode SDK has a method to get the app's version from info plist. You can just return this to Unity for your purpose.

[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]

0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.