a4d7be80创建于 2025年11月5日历史提交

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

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 Yes Whether to query the sync result of the asset whose custom additional information is encrypted. The value true means to query the sync result; the value false means the opposite. The default value is false.
ASSET_TAG_GROUP_ID18+ Type: Uint8[]
Length: 7-127 bytes
Yes Group to which the asset to be queried belongs. By default, this parameter is not specified.

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.

  • For assets that are accessible only when a password is set, the sync will fail if no lock screen password is set on either the old or new device.

  • For assets that are accessible only when the screen is unlocked, the sync will fail if the screen of either the old or new device is locked.

  • For assets that are accessible only after user authentication, the sync will fail if no lock screen password is set on the old device.