[Huawei Developers] devwithzachary:
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.

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.

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 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

buildscript {
repositories {
google()
jcenter()
maven {url 'https://developer.huawei.com/repo/'}
}
dependencies {
classpath 'com.android.tools.build:gradle:4.0.1'
classpath 'com.huawei.agconnect:agcp:1.5.2.300'
}
}

allprojects {
repositories {
google()
jcenter()
maven {url 'https://developer.huawei.com/repo/'}
}
}

task clean(type: Delete) {
delete rootProject.buildDir
}

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

apply plugin: 'com.android.application'
apply plugin: 'com.huawei.agconnect'
android {...}

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

implementation 'com.huawei.agconnect:agconnect-remoteconfig:1.5.2.300'
}

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.

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 testvalue format.

Call applyDefault to pass the default values.

config.applyDefault(R.xml.remote_config);

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.

config.fetch().addOnSuccessListener(new OnSuccessListener<ConfigValues>() {
@Override
public void onSuccess(ConfigValues configValues) {
config.apply(configValues);
// Apply parameter values.
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
}
});

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.

ConfigValues last = config.loadLastFetched();
config.apply(last);
config.fetch().addOnSuccessListener(new OnSuccessListener<ConfigValues>() {
@Override
public void onSuccess(ConfigValues configValues) {

}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
}

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

Boolean value = config.getValueAsBoolean("key");
Double value = config.getValueAsDouble("key");
Long value = config.getValueAsLong("key");
String value = config.getValueAsString("key");
byte[] value = config.getValueAsByteArray("key");

Obtain all parameter values​

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

Map<String,Object> map = config.getMergedAll();

For details, please refer to the development guide for Remote Configuration.