Quickly integrate Huawei AGConnect remote configuration service – Android

Time:2022-8-16

Huawei AppGallery Connect provides a remote configuration service. Through remote configuration, the application can be flexibly modified in the cloud without upgrading, and the behavior and appearance of the application can be flexibly modified to quickly respond to user needs. Today I will teach you how to access the remote configuration service of AppGallery Connect.

1. Enable remote configuration on AGC and create configuration items:

existMy project Select your development project underincreaseBelow, findRemote configurationservice, click to activate:

If you don't have an Android project, you can create one yourself.

After opening, we can set the configuration items.

When creating a new configuration item, we can see two tabs, configuration item management and configuration condition management. In configuration item management, we can create application setting items that need to be modified through remote configuration.

1. First click "Add Configuration Item" to add a new configuration item. A configuration item can be configured with multiple condition values, that is, different values ​​can be set under different conditions, and this configuration condition value will be explained later in the configuration condition management.

2. Click Save Configuration Item to save the current configuration.

3. Click "Publish" to make the current configuration and conditions take effect. Clicking "Cancel" will discard the changes to the configuration and conditions.

4. On the "Configuration Item Management" tab, you can view a list of all configuration items.

5. Click "Operation" in the upper right corner of the configuration item: you can view, modify, delete configuration items, and quickly add configuration items by copying

In configuration condition management, we can perform condition management on configuration delivery objects, so that configurations are only delivered to specific user groups. There are a total of 9 different configuration conditions, the specific description of each condition can refer toofficial documentation

2. Integrate SDK in Android project

a) Integrated SDK

1. Add Huawei Maven to the project-level gradle file, that is, configure the content marked in red below

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
}

2, open the application levelbuild.gradle file, configure the SDK of cloud storage and the SDK of Huawei Authentication Service, and configure the content marked in red below. Be careful not to drop the agcp plugin above

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

b) Download the json file and configure the default storage instance

1. On the AGC interface, select My Project -> Project Settings -> General Below, download the agconnect-services.json file to the app path of your Android project.

3. Function development:

The function of remote configuration is actually very simple. The most important thing is to set the local default value and obtain the latest configuration items in the cloud through the SDK.

1. Set the local default settings

First, add a remote configuration default xml file to the res/xml of the project, where the key-value pair istestvalue, for example the file remote_config.xml.

After the file is created, call the applyDefault interface in the code to pass in the file to set the local default configuration.

config.applyDefault(R.xml.remote_config);

Or create a map type data directly in the code, and pass the map into applyDefault to successfully set the local default configuration.

2. It will take effect immediately after the data is obtained

After the application obtains the cloud configuration items through the SDK, it can choose to take effect immediately or after the next startup. The first is to take effect immediately.

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

First, call the fetch interface to obtain the configuration items in the cloud. In the callback of successful acquisition, we can directly call the apply interface to immediately set the configuration items to the application.

3. After the data is obtained, the next startup will take effect

In addition to taking effect immediately, we can choose to take effect next time

ConfigValues last = config.loadLastFetched();
config.apply(last);
config.fetch().addOnSuccessListener(new OnSuccessListener() {
    @Override
    public void onSuccess(ConfigValues configValues) {
       
    }
}).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(Exception e) {
    }
});

We only need to call the loadLastFetched interface to obtain the configuration item data downloaded through the fetch interface last time, and then set the data to the application through the apply interface.

4. Get parameter values

After we have obtained the configuration items, if we need to take out the configuration items one by one for operation, we need to call a series of interfaces provided by the sdk to obtain this value. Different interfaces can obtain different types of parameters.

Boolean

Boolean value = config.getValueAsBoolean("key");

Double

Double value = config.getValueAsDouble("key");

Long

Long value = config.getValueAsLong("key");

String

String value = config.getValueAsString("key");

Byte

byte[] value = config.getValueAsByteArray("key");

5. Get all

We can also get all parameter values ​​at once

Map map = config.getMergedAll();

Through the getMergedAll method, we can get all the local default values ​​and cloud parameter values ​​at one time.

6. Reset parameter values

Finally, the SDK also provides us with the function of resetting parameters

config.clearAll();

With the clearAll method, all default values ​​of settings and parameter values ​​in the cloud will be reset.

Cloud Storage Service Development Guide:

https://developer.huawei.com/consumer/cn/doc/development/AppGallery-connect-Guides/agc-remoteconfig-introduction-0000001055149778