Integrate the HarmonyOS SDK

更新时间:
复制 MD 格式

This topic describes how to integrate the HarmonyOS SDK.

Prerequisites

  1. Prepare your HarmonyOS development environment. For more information, see the HarmonyOS Application Development Guide. This SDK is developed based on HarmonyOS API 12, with a compatibleSdkVersion of 5.0.0(12).

  2. Create a HarmonyOS application. For more information, see Integrate an SDK. Obtain the AppKey and AppSecret from the application settings.

  3. If you have already integrated SDK version 1.0.0, see the HarmonyOS SDK Upgrade Guide.

Add the SDK to your application

Run the following commands in the root directory of your HarmonyOS application to install the SDK. The `apm` package is required. Install the `apm_crash` and `apm_perf` packages as needed for crash and performance analysis.

ohpm install @aliyun/apm
ohpm install @aliyun/apm_crash
ohpm install @aliyun/apm_perf

For more information about the ohpm tool and installing third-party SDKs for OpenHarmony, see the OpenHarmony Third-Party Library Repository Guide.

Use the SDK

1. Initialization

Run the following code in the `Ability` `onCreate` lifecycle callback to initialize and configure the SDK:

Note

Place the SDK initialization code before all your business logic. This ensures that the App Monitor service loads first when the application starts. This allows crash and performance data to be collected and uploaded to the console immediately.

import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit';
import { window } from '@kit.ArkUI';
import { APM, APMConfig, Logger } from '@aliyun/apm';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { crashAnalysisApi } from '@aliyun/apm_crash';
import { performanceApi } from '@aliyun/apm_perf';

class MyCustomLog implements Logger {
  print(domain: number, tag: string, level: hilog.LogLevel, msg: string): void {
    switch (level) {
      case hilog.LogLevel.DEBUG:
        console.debug(`Custom log msg:${msg}`);
        break;
      case hilog.LogLevel.INFO:
        console.info(`Custom log msg:${msg}`);
        break;
      case hilog.LogLevel.WARN:
        console.warn(`Custom log msg:${msg}`);
        break;
      case hilog.LogLevel.ERROR:
      case hilog.LogLevel.FATAL:
        console.error(`Custom log msg:${msg}`);
        break;
    }
  }
}

export default class EntryAbility extends UIAbility {
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');

    const apm_config: APMConfig = {
      context: this.context,  // Required
      appKey: 'Your AppKey',    // Required
      appSecret: 'Your AppSecret',  // Required
      nick: 'User nickname',  // Optional
      userId: 'User ID',  // Optional
      channel: 'User channel',  // Optional
      hiLog: true, // Optional. Toggles hilog within the SDK.
      customLogger: new MyCustomLog(), // Optional. Custom logger interface.
    }
    // Select the crash and performance APIs during init based on the features you need.
    APM.init(apm_config, [crashAnalysisApi, performanceApi])
    APM.start();

  }

  // Other code is omitted.
}

Set `appKey` and `appSecret` to the values you obtained in Preparations.

Integration validation

After the integration is complete, start the application. To validate the integration, set the log level to Debug and check the SDK log output.

1. Validate Crash Analysis integration

The following key logs appear in the log output:

I  Custom log msg:Crash SDK init succeeded
I  Custom log msg:Crash SDK start success

2. Validate App Performance Analytics integration

The following key logs appear in the log output:

I  Custom log msg:Perf SDK init succeeded
I  Custom log msg:Perf SDK start success

Additional resources