Rapid integration of Huawei AGConnect remote configuration service - Android

Posted by storyboo on Thu, 10 Mar 2022 05:37:29 +0100

Huawei AppGallery Connec t provides a remote configuration service. Through remote configuration, the application does not need to be upgraded, and the behavior and appearance of the application can be flexibly modified in the cloud, so as to quickly respond to the needs of users. Today, let's teach you how to access the remote configuration service of AppGallery Connect.

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

Select your development project under my project, find the remote configuration service under growth, and click Open:

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

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.

a) First click Add configuration item to add new configuration item content. Configuration items 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 configuration condition management.

b) Click save configuration item to save the current configuration.

c) Click publish to make the current configuration and conditions take effect. Click Cancel to discard the changes to the configuration and conditions.

d) Under the configuration item management tab, you can view the list of all configuration items.

e) Click "operation" in the upper right corner of configuration items: you can view, modify and delete configuration items, and quickly add configuration items by copying

In configuration condition management, we can manage the condition of the configuration distribution object, so that the configuration can only be distributed to specific user groups. There are 9 different configuration conditions in total. Please refer to the official document for the description of each condition:

https://developer.huawei.com/consumer/cn/doc/development/AppGallery-connect-Guides/agc-remoteconfig-android-cloudconfig-0000001056587179#ZH-CN_TOPIC_0000001155033761__section11391717983

2. Integrate SDK in Android project

a) Integrated SDK

Add Huawei Maven to the gradle file at the project level, that is, configure the contents 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}

Open the application level build Gradle file, configure the SDK of cloud storage and the SDK of Huawei authentication service, and configure the contents marked in red below. Be careful not to leave the agcp plug-in 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

On the AGC interface, select my project - > project settings - > general, and then download 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 local default values and obtain the latest configuration items in the cloud through SDK.

a) Set local defaults

Firstly, add an XML file of remote configuration default value in res/xml of the project, where the key value pair is < value key = "testkey" > testvalue < / value >, such as file remote_config.xml.

After the file is created, the applyDefault interface is invoked in the code to import the files to set the local default configuration.

config.applyDefault(R.xml.remote_config);

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

b) Effective immediately after data acquisition

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

config.fetch().addOnSuccessListener(new OnSuccessListener<ConfigValues>() {    @Override    public void onSuccess(ConfigValues configValues) {        config.apply(configValues);        //Use the configuration value}) addOnFailureListener(new OnFailureListener() {    @Override    public void onFailure(Exception e) {    }});

First, call the fetch interface to get the configuration items of the cloud. In the callback for success, we can directly call the apply interface to set the configuration items to the application immediately.

c) After obtaining the data, the next startup will take effect

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

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) {    }});

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

d) Get parameter value

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 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");

e) Get all

We can also get all the parameter values at one time

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

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

f) Reset parameter value

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

config.clearAll();

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

Cloud Storage Service Development Guide:

https://developer.huawei.com/consumer/cn/doc/development/AppGalle Gansu red education and training www.gzganxun.com cn ry-connect-Guides/agc-remoteconfig-introduction-0000001055149778

 

Topics: IT