Creating And Distributing Android App Using Visual Studio 2017

Introduction

Here, I will explain how to create an Android app, using C# and Xamarin forms templates, using Visual Studio 2017 and distribute it locally. I know only XAML and C# code, using which I have created an app, which is used to calculate an age by the giving date of birth.

Explanation

Let’s start creating a project in Visual Studio by going to Start->New->Project. Now, you can see a pop up asking to select a template to create a project. In it, I have selected Cross Platform->Cross Platform app(Xamain.Forms or Native ) and I gave the name as AgeCalaculate, as shown below.

Visual studio 2017

After clicking OK button, you can see a one more popup, as shown below.

Visual studio 2017

In this popup, I have selected Blank App, under UI Technology Xamarin.Forms and PCL as Code sharing Strategy. After clicking OK button; you can see three projects are created, i.e., portable, Android and iOS and you can see in the left side App.xaml.cs file opens in the first page is mentioned as MainPage i.e. MainPage.xaml in the Portable project. This is the startup page, which is shown below.

Visual studio 2017

Let’s do some modification in the MainPage. For this, open the page and remove that default label. Add one stack layout with an orientation vertical. Inside this stack layout, I have added 2 labels,1 datepicker and one button and XAML code is shown below. 

  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"  
  3.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
  4.              xmlns:local="clr-namespace:AgeCalculate"  
  5.              x:Class="AgeCalculate.MainPage">  
  6.   
  7.     <StackLayout Orientation="Vertical" >  
  8.         <Label Margin="10,10,10,10"   Text="Enter date of birth"/>  
  9.         <DatePicker x:Name="dtdob"   
  10.           Margin="10,10,10,10"  />  
  11.         <Button Text="Convert to Age"   
  12.              HorizontalOptions="Center" Clicked="bt_click" TextColor="White" BackgroundColor="Blue" />  
  13.         
  14.         <Label Margin="10,20,10,10"  x:Name="lblAge" Text="Age :"/>  
  15.   
  16.     </StackLayout>  
  17.   
  18. </ContentPage>   

I had given a click event for the button and date picker name as dtdob. The label name given below is lblAge in order to access these controls from C# code.

In the code backend file, I have created a static function, which accepts a date and returns a string; I have added a logic to get an age from date into this function. In button click event, I am calling this static function by passing the datepicker date value and storing to a string variable. This string variable is assigned to the last added label lblAge. The code is shown below. 

  1. public static string CalculateYourAge(DateTime Dob)  
  2.        {  
  3.            DateTime Now = DateTime.Now;  
  4.            int _Years = new DateTime(DateTime.Now.Subtract(Dob).Ticks).Year - 1;  
  5.            DateTime _DOBDateNow = Dob.AddYears(_Years);  
  6.            int _Months = 0;  
  7.            for (int i = 1; i <= 12; i++)  
  8.            {  
  9.                if (_DOBDateNow.AddMonths(i) == Now)  
  10.                {  
  11.                    _Months = i;  
  12.                    break;  
  13.                }  
  14.                else if (_DOBDateNow.AddMonths(i) >= Now)  
  15.                {  
  16.                    _Months = i - 1;  
  17.                    break;  
  18.                }  
  19.            }  
  20.            int Days = Now.Subtract(_DOBDateNow.AddMonths(_Months)).Days;    
  21.              
  22.            return $"Age is {_Years} Years {_Months} Months {Days} Days";  
  23.        }  
  24.   
  25.        private void bt_click(object sender, EventArgs e)  
  26.        {  
  27.            string _Age = CalculateYourAge(dtdob.Date);  
  28.            lblAge.Text = _Age;  
  29.        }   

The output is shown below.

Visual studio 2017

Now, as you can see here; the Android app has been created successfully. Next, we will see how will we distribute this app.

For this, let's reduce app size by taking only SDK assemblies by right clicking on an Android project and selecting the properties->Androidoptions->Linker, as shown below.

Visual studio 2017

Subsequently, you can create an Android bundle by right clicking an Android project and selecting archive, as shown below.

Visual studio 2017

After clicking Archive screen, it will display, as shown below.

Visual studio 2017

Wait to finish creating an Android bundle. After finishing screen, it will display, as shown below.

Visual studio 2017

Below this screen, you can see two buttons, where one is an open folder. When you click this folder, then you can see .apk bundle file is created but this is not signed. In order to distribute an app properly, you need to sign this app before distribution. For this, we have to click second button that is Distribute screen, as shown below.

Visual studio 2017

When you click this button, you can see a screen asking how you wanted to distribute by Google play or Ad Hoc. If you go for Google play , then you need some registration process and so on. Here, I will explain Ad Hoc, which is used to distribute locally. Let’s see how is that click on Ad Hoc, then you can see a pop up in this click on + symbol to create a new certificate, as I don’t have any of the signed certificates, and add all the details in the following pop up and click Create button to create, as shown below.

Visual studio 2017

After creating the pop up, it will look, as shown below.

Visual studio 2017

From this pop up, select the certificate from the grid, which you wanted to apply. As of now, I have only one, which I have created just now that is rntp. Select this and click Save As button and locate, where you wanted to save this signed app. After clicking Save as button, the screen shown below.

Visual studio 2017

Select your desired place and subsequently click Save. After clicking Save, you will be prompting to put a password, which you had given in the certificate and click OK. The screen is shown below.

Visual studio 2017

After clicking OK, the signed Application will be generated at your desired place. You can locate this by clicking Open distribution button from the screen shown below.

Visual studio 2017

Your app is ready to distribute. Now, you can share this app anywhere Android OS is supported. It can be intsalled like any other app.

Next Recommended Readings