Integrating App Linking in a Xamarin app for Android
Tags: huawei, android, xamarin
Published: 2021-11-22 21:33:32

Xamarin is a popular cross platform framework to build mobile applications using .net 

A number of AppGallery Connect services support many cross platform frameworks including Xamarin. Today we are going to take a look at how you can use one of these services, App Linking within your Xamarin project.

## Enabling App Linking in AppGallery Connect

1. Create an app or use an existing app in [AppGallery Connect](https://developer.huawei.com/consumer/en/service/josp/agc/index.html#/). Click My projects, go to Grow > App Linking, and click Use now on the displayed page.

2. On the displayed App Linking page, click the URL prefixes tab and then click New URL prefix to create a unique URL prefix.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rc2xkuebxca83e08ina3.png)

## Preparing the Xamarin Android Development Environment 

### Completing Android Setup

Download the JSON file from AppGallery Connect and copy the file to your project's Assets directory.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tag8stkuozhvpxispxfh.png)

Set a package name. Right-click your project and choose Properties. Click Android Manifest on the displayed page and set the same package name to that in the JSON file.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dm6g73p4t0voeo3ilxw4.png)
Implement LazyInputStream to read the agconnect-services.json file.
Right-click your project, and choose Add > New Item. In the displayed window, select Class and name the new class HmsLazyInputStream.cs.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tmoc9tcm8447utr6j8ec.png)
The HmsLazyInputStream.cs class extends the LazyInputStream class and needs to be implemented. The sample code is as follows:

{% gist https://gist.github.com/devwithzachary/893d7f4ccf3d7bbe091bb076eb15276e %}
 
Create another new class as described in the preceding steps and read the agconnect-services.json file before your app is launched. You can name the new class CustomContentProvider.cs, which extends the ContentProvider class, and set the authorities and InitOrder attributes for the new class.

{% gist https://gist.github.com/devwithzachary/88c3b2b8f49abfbd3fed291b48932120 %}

### Installing the Service SDK for Android

Right-click your project and choose Manage NuGet Packages.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zu1hwr4wagdi6pjghuiw.png)

Search for AppLinking on the Browse tab. Click Xamarin.Android bindings for AGC - Applinking in the search results and install it.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vd7r4yswognta3ekfqwh.png)

Agree to the service agreement as prompted.

## Developing Your App
###Creating an App Linking Link
To specify the layout of your app, open the activity_main file under Resources > layout. Sample code:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cdev38ths0p8r5gddevk.png)

{% gist https://gist.github.com/devwithzachary/eb753bec4bf178e50c88474d8df9adbc %}

Open the MainActivity.cs file and import the following packages

{% gist https://gist.github.com/devwithzachary/7989113b4375db3fbf899eb90b034d6a %}

Configure button actions in the onCreate method

{% gist https://gist.github.com/devwithzachary/73c4af57bc0891451519830106c88080 %}

Create an App Linking link and implement the method for sharing the link.

{% gist https://gist.github.com/devwithzachary/21b55c0ba0276a3fba0dc913033417b4 %}

### Receiving an App Linking Link
Configure the code of the activity for receiving an App Linking link.
Right-click the project, choose add > New Item, select Activity, and name it DetailActivity.
The sample code is as follows:
{% gist https://gist.github.com/devwithzachary/03c60b4f676ab813b72cbafacc1fe1a6 %}

Configure the layout for the page of receiving an App Linking link.
Right-click the project, choose add > New Item, select Android Layout, and name it activity_detail. The sample code is as follows:

{% gist https://gist.github.com/devwithzachary/29dd7afb78c65712bd8811d593af765d %}

Configure the Manifest file. Find the Properties directory and open the AndroidManifest file in the directory. Configure the following content in the <application> element.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qcd7s5pomuki9cygpds7.png)

{% gist https://gist.github.com/devwithzachary/f09a483896314015496cc6ce54d9cd6d %}

##Testing Your App
Click Run to test your app.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9cxopo34rdj7pgar6h0m.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fxgxxpfr68r8xds22cf8.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jn4nqceo8uiozd5yv5x3.png)

References
[Getting started with Xamarin](https://developer.huawei.com/consumer/en/doc/development/AppGallery-connect-Guides/agc-get-started-xamarin-0000001081055220)
[App Linking (Android)](https://developer.huawei.com/consumer/en/doc/development/AppGallery-connect-Guides/agc-applinking-xamarin-android-usage-0000001077120642)