How to set/get the App version number and build for cross-platform ?

Using VS 2017 and Xamarin Forms 2.3.4.237
Cross platform - iOS and Android

I'd like to accomplish two things:

1) Set an 'automatic build' number that increments each time I build my project (eg. 0.0.1 > 0.0.2....0.1.0 etc)
2) Get the build number so that I can add it to an About screen (or similar) so that I can track my version/builds.

How can I do these two things?

Tagged:

Answers

  • ShahidAli.7296ShahidAli.7296 USMember ✭✭

    Hi @weh ,

    Its a very loaded question and I don't think you will have any luck with automation unless proven otherwise. Essentially you got couple of options:

    1: You can use Settings plugin (https://github.com/jamesmontemagno/SettingsPlugin) to store the version number (Note: no automation, values must be set manually) in a key/value format and retrieve it whenever you need.

    2: Use Akavache plugin (which uses SQLite) to store the version info in a key/value format and display it whenever required.

    3: Static helper class with static properties holding version info.

    4: Store nothing on the client (a bit of an overkill) and get the back end api to return the version info.

    Hope that answer your question.

  • DirkWilhelmDirkWilhelm USMember ✭✭✭✭

    With a dependency service you can read the version and build number from the androidmanifest.xml and from the info.plist for ios.

    For android i set the versionName in the manifest to my version number and the versionCode to the build number.
    For ios i set the CFBundleShortVersionString to my version number and CFBundleVersion to the build number.

  • ShahidAli.7296ShahidAli.7296 USMember ✭✭

    @DirkWilhelm

    Presumably Build number has to be updated with each version of the app ?

  • DirkWilhelmDirkWilhelm USMember ✭✭✭✭

    @ShahidAli.7296 I'm using a gitlab ci build system, so i can use the buildid from that system to automatically update the manifest for android and info.plist for ios on each build.

    I guess something similar would be possible for manual builds in VS using pre-build events

  • wehweh USMember ✭✭

    (To all that responded) - First, thanks for the suggestions.
    Next - when I wrote code in VB6 you could do this without any added code:

    versionString = App.Major & "." & App.Minor & "." & App.Revision

    I'm guessing from your responses that something this straightforward is not possible...

  • voidstreamvoidstream FRMember ✭✭✭
    edited October 2017

    @DirkWilhelm said:
    With a dependency service you can read the version and build number from the androidmanifest.xml and from the info.plist for ios.

    For android i set the versionName in the manifest to my version number and the versionCode to the build number.
    For ios i set the CFBundleShortVersionString to my version number and CFBundleVersion to the build number.

    I don't have test, but i think the dependency service is the answer!
    Find how get infos for each projects and use an interface to access it with dependency service.

  • wehweh USMember ✭✭

    OK, I'll dig around and see if I can find the solution - thanks.

  • KingDavid3KingDavid3 USMember ✭✭
    edited January 2018

    @DirkWilhelm this is by far one of the simplest, thorough and informative descriptions of how to create and use a dependency service.
    Well done.

  • Does these version numbers count for the versions checkeed on iTunesConnect when uploading ?

    Thanx
    Eigil

  • Abhijeet_SuryaAbhijeet_Surya USMember ✭✭✭
    edited March 2018
    public class VersionInfo {
        public VersionInfo (string versionNumber, string buildNumber) {
            VersionNumber = versionNumber;
            BuildNumber = buildNumber;
        }
    
        public string VersionNumber {
            get;
        }
    
        public string BuildNumber {
            get;
        }
    }
    
    public interface IAppVersion {
        VersionInfo GetVersionAndBuildNumber ();
    }
    
    public class AppVersion : IAppVersion {
        public VersionInfo GetVersionAndBuildNumber () {
            Context context = global::Android.App.Application.Context;
            Android.Content.PM.PackageManager manager = context.PackageManager;
            Android.Content.PM.PackageInfo info = manager.GetPackageInfo (context.PackageName, 0);
    
            return new VersionInfo (info.VersionName, info.VersionCode.ToString ());
        }
    }
    
    public VersionInfo GetVersionAndBuildNumber () {
        return new VersionInfo (NSBundle.MainBundle.ObjectForInfoDictionary ("CFBundleShortVersionString").ToString (), NSBundle.MainBundle.ObjectForInfoDictionary ("CFBundleVersion").ToString ());
    }
    
  • manasaavmanasaav INMember ✭✭✭

    i tried dirk...... can you please help me out ..I am getting object reference not set to an instance of object.

  • JerryGonzalezJerryGonzalez USMember

    @DirkWilhelm

    Great solution! How would you go about doing this for a UWP project?

  • SgtJukkerSgtJukker USMember ✭✭
    edited July 2018

    @JerryGonzalez

    Implement the interface like this:

    [assembly: Xamarin.Forms.Dependency(typeof(Your.Namespace.UWP.Version_UWP))]
    namespace Your.Namespace.UWP
    {
        public class Version_UWP : IAppVersion
        {
            public string GetVersion()
            {
                return typeof(App).GetTypeInfo().Assembly.GetName().Version.ToString();
            }
    
            public string GetBuild()
            {
                return typeof(App).GetTypeInfo().Assembly.GetName().Version.Build.ToString();
            }
        }
    }
    
  • I joined Xamarin Forums moments ago JUST to say:

    @DirkWilhelm, @SgtJukker, and everyone else, thank you for your very helpful answers!

  • nick5454nick5454 USUniversity ✭✭✭

    @JerryGonzalez said:
    @DirkWilhelm

    Great solution! How would you go about doing this for a UWP project?

    @kaolick said:
    You could use Xamarin.Essentials package for getting app version and build number like this (see here).

    // Application Version (1.0.0)
    var version = AppInfo.VersionString;
    
    // Application Build Number (1)
    var build = AppInfo.BuildString;
    

    @kaolick Best answer!

Sign In or Register to comment.