Tushar patel
Tushar patel

Reputation: 3407

Get current app's build version - Xamarin.Android

I need to display the app's build version in my settings page.

So how can i get the versionCode and versionName from AndroidManifest.xml file programmatically.

Or

Is there any way to get the build version programmatically in xamarin.forms.

Upvotes: 24

Views: 22609

Answers (4)

Gabriel Weidmann
Gabriel Weidmann

Reputation: 887

In Xamarin.Essentials you can use the following:

Version version = AppInfo.Version;

From there you can get the build version.

If you really just want the build number you can also use the shortcut:

string buildNumber = AppInfo.BuildString;

Upvotes: 19

Mario Galván
Mario Galván

Reputation: 4032

With a Dependency service:

For iOS:

using Foundation;

[assembly: Xamarin.Forms.Dependency(typeof(Screen_iOS))]

namespace XXX.iOS
{
    public class Screen_iOS : IScreen
    {

        public string Version
        {
            get
            {
                NSObject ver = NSBundle.MainBundle.InfoDictionary["CFBundleShortVersionString"];
                return ver.ToString();
            }
        }

    }
}

For Android:

using Xamarin.Forms;

[assembly: Dependency(typeof(ScreenAndroid))]

namespace XXX.Droid
{
    class ScreenAndroid : Java.Lang.Object, IScreen
    {

        public string Version
        {
            get
            {
                var context = Forms.Context;
                return context.PackageManager.GetPackageInfo(context.PackageName, 0).VersionName;
            }
        }

    }
}

And the interface:

interface IScreen
{
    string Version { get; }
}

Upvotes: 32

Parth Patel
Parth Patel

Reputation: 3997

For version name:

Application.Context.ApplicationContext.PackageManager.GetPackageInfo(Application.Context.ApplicationContext.PackageName, 0).VersionName

For version code:

Application.Context.ApplicationContext.PackageManager.GetPackageInfo(Application.Context.ApplicationContext.PackageName, 0).VersionCode

Upvotes: 33

JKennedy
JKennedy

Reputation: 18799

In Xamarin.Android you could always try:

public double GetAppVersion()
{
   var info = AppContext.PackageManager.GetPackageInfo(AppContext.PackageName, 0);
   return info.VersionCode;
}

and then you will need to use dependency injection to call this method from your Xamarin.Forms project.

Upvotes: 1

Related Questions