目录

Syncing Assets (Backup and Restore) (C/C++)

Adding Dependencies

Link the dynamic libraries in the CMake script.

target_link_libraries(entry PUBLIC libasset_ndk.z.so)

Include header files.

#include "napi/native_api.h"
#include <string.h>
#include "asset/asset_api.h"

Adding Assets That Support Sync

Add an asset with the password demo_pwd, alias demo_alias, and additional information demo_label.

static napi_value AddSyncAsset(napi_env env, napi_callback_info info)
{
    char *secretStr = "demo_pwd";
    char *aliasStr = "demo_alias";
    char *labelStr = "demo_label";

    Asset_Blob secret = {(uint32_t)(strlen(secretStr)), (uint8_t *)secretStr};
    Asset_Blob alias = {(uint32_t)(strlen(aliasStr)), (uint8_t *)aliasStr};
    Asset_Blob label = {(uint32_t)(strlen(labelStr)), (uint8_t *)labelStr};
    Asset_Attr attr[] = {
        {.tag = ASSET_TAG_SECRET, .value.blob = secret},
        {.tag = ASSET_TAG_ALIAS, .value.blob = alias},
        {.tag = ASSET_TAG_DATA_LABEL_NORMAL_1, .value.blob = label},
        {.tag = ASSET_TAG_SYNC_TYPE, .value.u32 = ASSET_SYNC_TYPE_TRUSTED_DEVICE}, // You need to specify the sync type between trusted devices (for example, clone between old and new devices).
    };

    int32_t addResult = OH_Asset_Add(attr, sizeof(attr) / sizeof(attr[0]));
    napi_value ret;
    napi_create_int32(env, addResult, &ret);
    return ret;
}

Accessing the Backup and Restore Extension Capability

To trigger the backup and restore of application data, see Accessing Backup and Restore.

Querying the Sync Result of Assets

[h2]Available APIs

You can call OH_Asset_QuerySyncResult to query the sync result of assets. For more information, see the API reference.

The following table describes the attributes for querying an asset.

Attribute Name (Asset_Tag) Attribute Content (Asset_Value) Mandatory or Not Description
ASSET_TAG_REQUIRE_ATTR_ENCRYPTED14+ Type: bool. Optional Whether to query the asset synchronization result for assets whose service-defined additional information is encrypted. true indicates to query the synchronization result of assets for which the service-defined additional information is stored in encrypted form. false indicates to query the synchronization result of assets for which the service-defined additional information is stored in unencrypted form. The default value is false.
ASSET_TAG_GROUP_ID18+ Type: uint8[], with a length of 7 to 127 bytes. Optional Group to which the assets to be queried belong. By default, the synchronization result of assets that do not belong to any group is queried.

[h2]Sample Code

static napi_value QuerySyncResult(napi_env env, napi_callback_info info)
{
    Asset_SyncResult syncResult = {0};
    int32_t queryResult = OH_Asset_QuerySyncResult(NULL, 0, &syncResult);
    napi_value ret;
    napi_create_int32(env, queryResult, &ret);
    return ret;
}

Notes and Constraints

For a successful sync between trusted devices, the assets of both old and new devices must be accessible. Otherwise, the sync might fail.