Implement Huawei AppGallery Remote Configuration in your Xamarin Android app
Tags: xamarin, android, huawei, appgallery
Published: 2022-02-08 10:32:53

Xamarin (Microsoft) is a multi-system development platform for mobile services that many developers use. Many AppGallery Connect services now support Xamarin, including Remote Configuration.

Remote Config allows you to make changes to your app remotely by making use of variables that you can define in the AppGallery Console. Different values can be set for different audiences, locations or sections of users. This is a great way to personalise your application, carry out testing of new features or just make updates without having to deploy a new app version.

## Install the Xamarin environment and project setup.

You'll need to first download and install Visual Studio 2019.
Open Visual Studio and select Mobile development with .NET to install the Xamarin environment.
 
![Visual Studios install screen](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c9oknn2l1tkpohgllnje.png)

Next make sure you have enabled the [Auth Service](https://developer.huawei.com/consumer/en/doc/development/AppGallery-connect-Guides/agc-auth-android-getstarted-0000001053053922) in AppGallery Connect.

Open Visual Studio, click Create a new project in the start window, select Mobile App (Xamarin.Forms), and set the app name and other required information.

![Visual Studio create new project screen](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qoffdg2w5i54hpru8bmy.png)

Right-click your project and choose **Manage NuGet Packages**.
![Right click menu with Manage NuGet Packages highlighted](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5tx9wcmxwx5s1jzca0vc.png)

Search for the **Huawei.Agconnect.RemoteConfiguration** package on the displayed page and install it.

Download the JSON service file from your AppGallery project and add it into the **Assets* directory in your project.
![Xamarin file structure showing agconnect-service.json file in Asserts folder](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gpbvccmjn60i3u9usrpy.png)
 
Create a new class named **HmsLazyInputStreams.cs**, and implement the following code to read the JSON file.

{% gist https://gist.github.com/devwithzachary/893d7f4ccf3d7bbe091bb076eb15276e %}

Create a ContentProvider class and add the following code for your file to be read once your app is launched. Ensure that the package name set in ContentProvider, and the one in your project and the one set in AppGallery Connect are the same as the app package name.

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

Right-click your project and choose **Properties**. Click **Android Manifest** on the displayed page and set a package name
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gg9isq4chiow0qt3t7qp.png)

## Set in app default parameter values 

You can configure a default parameter value in either of the following ways:
Using an XML resource file. Add a default parameter value XML file to the res/xml directory of your project. Call the **ApplyDefault** method to read the file.

Or you can use a Map object. Set a default parameter value dynamically through coding.

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

## Fetch the updated parameter value

Call the fetch API to fetch the updated parameter value from Remote Configuration. The default update interval used by the fetch API is 12 hours and can be set as required.

{% gist https://gist.github.com/devwithzachary/1ecca003e410821ad047a14f56334a35 %}

## Obtain all parameter values

For Xamarin.Android apps, the MergedAll API is called to obtain all parameter values including the in-app default parameter value and the on-cloud parameter value. For Android apps, the getMergedAll API is used

And that's all for integrating Remote Configuration into Xamarin.Android apps. As more and more AppGallery Connect services support Xamarin, I will keep you posted about further integration tutorials.