HarmonyOS SDK upgrade guide

更新时间:
复制 MD 格式

Upgrade from version 1.0.0 to 1.0.1 or later

Important

After you upgrade the SDK to version 2.0.0, the connection type for version 1.0.0 is no longer supported. You must update your connection code to be compatible with the new version.

Version 1.0.1 improves the initialization interface from version 1.0.0, adding support for more application performance management (APM) products, such as App Performance Analytics.

Update the SDK

Run the following command to update the SDK package:

ohpm update @aliyun/apm
ohpm update @aliyun/apm_crash
ohpm update @aliyun/apm_perf

Adjust the connection code

The second step of the SDK connection process has been modified. Update your code to the latest version. For more information, see Use the SDK.

To use both crash and performance analysis, import two APIs during initialization. The core code is as follows:

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: 'A user nickname',  // Optional
      userId: 'A user ID',  // Optional
      channel: 'A user channel',  // Optional
      hiLog: true, // Optional. Enables or disables HiLog in the SDK.
      customLogger: new MyCustomLog(), // Optional. A custom logger interface.
    }
    // During init, select the crash and performance APIs based on the features that you use.
    APM.init(apm_config, [crashAnalysisApi, performanceApi])
    APM.start();

  }

  // Other code is omitted.
}