Integrating Huawei's Remote Configuration into an Android App
Tags: huawei, android
Published: 2022-01-26 13:47:11

Remote Configuration allows you to change how your app works and looks on the cloud, without requiring any app upgrades. Here we'll take a look at a hands-on example of how to integrate Remote Configuration.

## Enabling and Configuring Remote Configuration
Sign in to AppGallery Connect and click **My projects**.
Click your project card and select the app for which you want to enable Remote Configuration from the app drop-down list on the top.
Go to **Grow > Remote Configuration**. If this is your first time using Remote Configuration, click **Use now** in the upper right corner.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uefslax0lk59y1g9234k.png)
Then you'll be able to configure the service in AppGallery Connect.
On the **Remote Configuration** page, set parameter and their conditions.
First, click New parameter to add a parameter. You can set multiple conditional values for a parameter. That is, the parameter value will vary depending on the condition.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4yq68ulnie4rvk0uxrak.png)
Click **Save**.
Click **Release** to make the configuration take effect. If you click **Cancel**, all changes will be discarded.
Click the **Parameters** tab, on which all parameters are listed.
Click in the upper right corner of the parameter card. You'll be able to view, modify, and delete parameters, and copy existing parameters as new ones.
On the **Conditions** tab, you can [set different conditions](https://developer.huawei.com/consumer/en/doc/development/AppGallery-connect-Guides/agc-remoteconfig-android-cloudconfig-0000001056587179) for the parameter. For example, if you set **Condition** to **Audience**, the configured parameter value will be updated only for a specific user group. 

## Integrating SDKs
Add Maven repository configuration to the **build.gradle** file of your project
{% gist https://gist.github.com/devwithzachary/b044d3c8eadd765146349a7b5b020335 %}

Open the app-level **build.gradle** file, and configure the Remote Configuration SDK, Analytics SDK, and AppGallery Connect Plugin

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

In AppGallery Connect, click **My projects**, and find your project from the list. Go to **Project settings** > **General information**, download the **agconnect-services.json** file, and save the file to the app directory of your Android project.

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

## Developing Functions
In this step, the Remote Configuration SDK will be used to set in-app default values and fetch parameter values from the cloud.
Add a default value configuration XML file, for example, **remote_config.xml**, to the **res/xml** directory of your Android Studio project. In the file, key-value pairs are in <value key="testkey">testvalue</value> format.

Call **applyDefault** to pass the default values.

{% gist https://gist.github.com/devwithzachary/7c1491f6259869dc0a3be03c73077339 %}
You can also create a **Map** object, and call applyDefault to read and pass the default values.

You can apply parameter values fetched from the cloud either immediately or upon the next launch of your app. 

### Fetch parameter values and apply them immediately

The following code applies parameter values immediately.

{% gist https://gist.github.com/devwithzachary/0750cf98ea58bfd905fcb4c4883d8f70 %}

In the code, call the fetch API to fetch parameter values from the cloud, and then call the apply API to apply the parameter values in the fetching callback immediately.

### Fetch parameter values and apply them upon the next launch of your app.

The following code applies parameter values upon the next launch of your app.

{% gist https://gist.github.com/devwithzachary/2b80d545e8717f8ef74dce75742dad50 %}

In the code, call loadLastFetched to obtain the parameter values fetched by calling the fetch API, and call the apply API to apply the parameter values.

### Obtain the value for a single parameter

You can call an API to obtain the value for a parameter of a specific type from parameter values fetched from the cloud. The APIs for different parameter types are listed below

{% gist https://gist.github.com/devwithzachary/7d9f0244424198916ac6e8367b72db4c %}

### Obtain all parameter values
You can obtain all in-app default parameter values and on-cloud parameter values by calling getMergedAll.

{% gist https://gist.github.com/devwithzachary/68162a8322bac052b268a52b02125db0 %}

For details, please refer to the development guide for [Remote Configuration](https://developer.huawei.com/consumer/en/doc/development/AppGallery-connect-Guides/agc-remoteconfig-introduction-0000001055149778).