How To Get App Version And Build Number In Xamarin.forms

Introduction

Version and the build numbers work together to uniquely identify a particular App Store submission for an app. The conventions for how these numbers work together are verified by automatic processes when you submit your app to the App Store or Play Store, so understanding how these numbers work and how they are intended to be used will help you save time when submitting your app. In this article, we can learn how to get App version and build numbers through programming in Xamarin.Forms.

Requirements

  1. This article's source code is prepared by using Visual Studio. And it is better to install the latest Visual Studio updates from here.
  2. This article is prepared on a MAC machine.
  3. This sample project is Xamarin.Forms PCL project.
  4. This sample app is targeted for Android, iOS. And, tested for Android & iOS.

Description

For each new version of your app, you will provide a version number to differentiate it from previous versions. The version number works like a name for each release of your app. For example, version 1.0.0 may name the first release, version 2.0.1 will name the second, and so on. When submitting a new release of your app to the App Store or Play Store, it is normal to have some false starts. You may forget an icon in one build, or perhaps there is a problem in another build. As a result, you may produce many builds during the process of submitting a new release of your app to the App Store or Play Store. Because these builds will be for the same release of your app, they will all have the same version number. But, each of these builds must have a unique build number associated with it so it can be differentiated from the other builds you have submitted for the release. The collection of all of the builds submitted for a particular version is referred to as the 'release train' for that version.

iOS

In iOS, the version number and the build number are also displayed in your app's Info.plist file.

Info.plist

Xamarin

Android 

In Android, we can see the version and build numbers in the Manifest file.

AndroidManifest.xml

Xamarin

Let's start to get app Version and Build numbers through programming.

1)Xamarin.Forms

Portable Class Library(PCL)

Step1

Create an interface, IAppVersionAndBuild.cs, with the methods declaration of GetVersionNumber and GetBuildNumber.

IAppVersionAndBuild.cs

  1. namespace VersionAndBuildNumber.DependencyServices {  
  2.     public interface IAppVersionAndBuild {  
  3.         string GetVersionNumber();  
  4.         string GetBuildNumber();  
  5.     }  
  6. }  

Step 2

Create your own XAML page named VersionAndBuildNumberPage.xaml.

VersionAndBuildNumberPage.xaml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
  3. BackgroundColor="#533F95" 
  4. xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
  5. xmlns:local="clr-namespace:VersionAndBuildNumber" 
  6. x:Class="VersionAndBuildNumber.VersionAndBuildNumberPage">  
  7.     <StackLayout VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand" Spacing="40">  
  8.         <StackLayout>  
  9.             <Label Text="Version Number:" TextColor="White" FontAttributes="Bold" FontSize="25" />  
  10.             <Label x:Name="lblVersionNumber" TextColor="White" FontAttributes="Bold" FontSize="25" /> </StackLayout>  
  11.         <StackLayout>  
  12.             <Label Text="Build Number:" FontSize="25" TextColor="White" FontAttributes="Bold" />  
  13.             <Label x:Name="lblBuildNumber" TextColor="White" FontAttributes="Bold" FontSize="25" /> </StackLayout>  
  14.     </StackLayout>  
  15. </ContentPage>  

Call to DependencyService

Now, call dependency service in the code behind for getting Version and Build numbers, also assign those values to the labels.

  1. DependencyService.Get<IAppVersionAndBuild>().GetVersionNumber();   
  2. DependencyService.Get<IAppVersionAndBuild>().GetBuildNumber();   

VersionAndBuildNumberPage.xaml.cs

  1. using VersionAndBuildNumber.DependencyServices;  
  2. using Xamarin.Forms;  
  3. namespace VersionAndBuildNumber {  
  4.     public partial class VersionAndBuildNumberPage: ContentPage {  
  5.         public VersionAndBuildNumberPage() {  
  6.             InitializeComponent();  
  7.             lblVersionNumber.Text = DependencyService.Get<IAppVersionAndBuild>().GetVersionNumber();  
  8.             lblBuildNumber.Text = DependencyService.Get<IAppVersionAndBuild>().GetBuildNumber();  
  9.         }  
  10.     }  
  11. }  

Xamarin.Android

Create a class, VersionAndBuild_Android, and implement the IAppVersionAndBuild methods like below.

VersionAndBuild_Android.cs

  1. using Android.Content.PM;  
  2. using VersionAndBuildNumber.DependencyServices;  
  3. using VersionAndBuildNumber.Droid.DependencyServices;  
  4. using Xamarin.Forms;  
  5. [assembly: Dependency(typeof(VersionAndBuild_Android))]  
  6. namespace VersionAndBuildNumber.Droid.DependencyServices {  
  7.     public class VersionAndBuild_Android: IAppVersionAndBuild {  
  8.         PackageInfo _appInfo;
  9.         public VersionAndBuild_Android() {  
  10.             var context = Android.App.Application.Context;  
  11.             _appInfo = context.PackageManager.GetPackageInfo(context.PackageName, 0);  
  12.         }  
  13.         public string GetVersionNumber() {  
  14.             return _appInfo.VersionName;  
  15.         }  
  16.         public string GetBuildNumber() {  
  17.             return _appInfo.VersionCode.ToString();
  18.         }  
  19.     }  
  20. }  

Note
PackageManager Class for retrieving various kinds of information related to the application packages that are currently installed on the device.

Xamarin.iOS

Create a class, VersionAndBuild_iOS, and implement the IAppVersionAndBuild methods like below.

VersionAndBuild_iOS.cs

  1. using Foundation;  
  2. using VersionAndBuildNumber.DependencyServices;  
  3. using VersionAndBuildNumber.iOS.DependencyServices;  
  4. using Xamarin.Forms;  
  5. [assembly: Dependency(typeof(VersionAndBuild_iOS))]  
  6. namespace VersionAndBuildNumber.iOS.DependencyServices {  
  7.     public class VersionAndBuild_iOS: IAppVersionAndBuild {  
  8.         public string GetVersionNumber() {  
  9.             //var VersionNumber = NSBundle.MainBundle.InfoDictionary.ValueForKey(new NSString("CFBundleShortVersionString")).ToString();   
  10.             return NSBundle.MainBundle.ObjectForInfoDictionary("CFBundleShortVersionString").ToString();  
  11.         }  
  12.         public string GetBuildNumber() {  
  13.             //var BuildNumber = NSBundle.MainBundle.InfoDictionary.ValueForKey(new NSString("CFBundleVersion")).ToString();   
  14.             return NSBundle.MainBundle.ObjectForInfoDictionary("CFBundleVersion").ToString();  
  15.         }  
  16.     }  

Note
CFBundleShortVersionString, CFBundleVersion these or the  Core Foundation Keys in iOS. The Core Foundation framework provides the underlying infrastructure for bundles, including the code used at runtime to load bundles and parse their structure. As a result, many of the keys recognized by this framework are fundamental to the definition of bundles themselves and are instrumental in determining the contents of a bundle. 

2) Xamarin.Android

  1. var context = Android.App.Application.Context;  
  2. var VersionNumber = context.PackageManager.GetPackageInfo(context.PackageName, PackageInfoFlags.MetaData).VersionName;  
  3. var BuildNumber = context.PackageManager.GetPackageInfo(context.PackageName, PackageInfoFlags.MetaData).VersionCode.ToString(); 

3) Xamarin.iOS

  1. var VersionNumber = NSBundle.MainBundle.InfoDictionary.ValueForKey(new NSString("CFBundleShortVersionString")).ToString();  
  2. var BuildNumber = NSBundle.MainBundle.InfoDictionary.ValueForKey(new NSString("CFBundleVersion")).ToString();  

Output

                         

Please download the source code from here 

 


Similar Articles