openharmony 鸿蒙 js-apis-common-load-native-module

2026-08-25 浏览 (1)

loadNativeModule (Synchronously and Dynamically Loading a System Library)

This module provides the capability of synchronously and dynamically loading a system library.

NOTE

The initial APIs of this module are supported since API version 12. Newly added APIs will be marked with a superscript to indicate their earliest API version.

The APIs of this module can be used only in the stage model.

loadNativeModule

loadNativeModule(moduleName: string): Object

The loadNativeModule API is used to synchronously and dynamically load a native module, that is, only load the required module at a time. Using this API increases the time for loading the .so file. You need to evaluate the impact on the functionality.

NOTE

The name of the module loaded by loadNativeModule is the name provided in dependencies in the oh-package.json5 file of the dependency.

loadNativeModule can be used only to load native modules in the UI main thread.

Dependencies must be configured for the API call regardless of whether the parameter is a constant string or variable expression.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
moduleNamestringYesName of the module to load.

Return value

TypeDescription
ObjectDefault export of the native module.

Error codes

For details about the error codes, see Universal Error Codes and Utils Error Codes.

IDError Message
401The parameter check failed.
10200301Loading native module failed.

Scenarios supported by loadNativeModule

ScenarioExample
System library moduleLoad @ohos. or @system..
Native module in an applicationLoad libNativeLibrary.so.

Example 1: Loading a System Library Module to a HAP

let hilog: ESObject = loadNativeModule("@ohos.hilog");
hilog.info(0, "testTag", "loadNativeModule ohos.hilog success");

Example 2: Loading a Native Library to a HAP

The index.d.ts file of libentry.so is as follows:

//index.d.ts
export const add: (a: number, b: number) => number;
  1. When loading a local .so library, configure dependencies in the oh-package.json5 file. For details, see Fields in the Module-level oh-package.json5 File.
{
  "dependencies": {
    "libentry.so": "file:./src/main/cpp/types/libentry"
  }
}
  1. Configure the module-level build-profile.json5 file. For details, see Module-level build-profile.json5 File.
{
  "buildOption": {
    "arkOptions": {
      "runtimeOnly": {
        "packages": [
          "libentry.so"
        ]
      }
    }
  }
}
  1. Use loadNativeModule to load libentry.so and call the add function.
let module: ESObject = loadNativeModule("libentry.so");
let sum: number = module.add(1, 2);

你可能感兴趣的鸿蒙文章

openharmony 鸿蒙 capi-jsvm-jsvm-vminfo

openharmony 鸿蒙 capi-memory

openharmony 鸿蒙 js-apis-syscap

openharmony 鸿蒙 capi-memory-purgmem

openharmony 鸿蒙 capi-jsvm-jsvm-callbackstruct8h

openharmony 鸿蒙 capi-jsvm-jsvm-propertydescriptor

openharmony 鸿蒙 capi-jsvm-jsvm-cpuprofiler--8h

openharmony 鸿蒙 capi-jsvm-jsvm-codecache

openharmony 鸿蒙 capi-jsvm-jsvm-data--8h

openharmony 鸿蒙 capi-jsvm-h

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/3G6WEPOy