The loadNativeModule API is used to dynamically load native modules in synchronous mode. Using this API increases the time for loading the .so file. You need to evaluate the impact on the functionality.
loadNativeModule(moduleName: string): Object;
| Parameter | Description |
|---|---|
| moduleName | Name of the module to load. |
The name of the module loaded by loadNativeModule is the name provided in dependencies in the oh-package.json5 file of the dependency.
loadNativeModule must be called 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.
| Scenario | Example |
|---|---|
| System library module | Load @ohos. or @system.. |
| Native module in an application | Load libNativeLibrary.so. |
let hilog: ESObject = loadNativeModule("@ohos.hilog");
hilog.info(0, "testTag", "loadNativeModule ohos.hilog success"); The index.d.ts file of libentry.so is as follows:
//index.d.ts export const add: (a: number, b: number) => number;
{
"dependencies": {
"libentry.so": "file:./src/main/cpp/types/libentry"
}
} {
"buildOption": {
"arkOptions": {
"runtimeOnly": {
"packages": [
"libentry.so"
]
}
}
}
} let module: ESObject = loadNativeModule("libentry.so");
let sum: number = module.add(1, 2);