ArkTS侧与Native侧分别如何动态加载SO库
=========================

**解决措施**

1.ArkTS 可以通过动态 import 加载 so 库。

2.Native侧可以使用dlopen动态加载so库。

参考代码如下：

1.ArkTS 通过动态 import 加载 so 库。添加异步函数，在异步函数中通过let testNapi = await import ("libentry.so")实现动态加载so库。

import { hilog } from '@kit.PerformanceAnalysisKit';
_// import testNapi from 'libentry.so';_

@Entry
@Component
struct LoadSoLibrary {
  @State message: string = 'Hello World';

  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
          .onClick(async() => {
            let testNapi = await import("libentry.so")          _  // Load so library_
            hilog.info(0x0000, 'testTag', 'Test NAPI 2 + 3 = %{public}d', testNapi.default.add(2, 3));  _ // Call library functions by default_
          _  // hilog.info(0x0000, 'testTag', 'Test NAPI 2 + 3 = %{public}d', testNapi.add(2, 3));_
          })
      }
      .width('100%')
    }
    .height('100%')
  }
}

2.Native侧使用dlopen动态加载so库。

需要调用liba.so中的add函数。

*   将liba.so文件放到libs/arm64-v8a/路径下。
    
    ![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/2b/v3/an0nSX8-TbapRZuYEaSPCg/zh-cn_image_0000002624475890.png?HW-CC-KV=V1&HW-CC-Date=20260709T133829Z&HW-CC-Expire=86400&HW-CC-Sign=2C829C1765BCE1BA9FC8474754DCCE34860FC6334861A2573F02BD239BDEBDF8 "点击放大")
    
*   需要在ArkTS侧传递so库路径信息到Native侧。
    
    import { hilog } from '@kit.PerformanceAnalysisKit';
    import testNapi from 'libentry.so';
    
    @Entry
    @Component
    struct Index {
      @State message: string = 'Hello World';
    
      build() {
        Row() {
          Column() {
            Text(this.message)
              .fontSize(50)
              .fontWeight(FontWeight.Bold)
              .onClick(() => {
                let path = this.getUIContext().getHostContext()!.bundleCodeDir;    _ // Get project path_
                hilog.info(0x0000, 'testTag', 'Test NAPI 2 + 3 = %{public}d', testNapi.addByLibPath(2, 3, path + '/libs/arm64/liba.so'));   _// Transfer parameter path information to the native side_
              })
          }
          .width('100%')
        }
        .height('100%')
      }
    }
    
*   在Native侧通过dlopen函数动态加载so库。
    
    #include "napi/native\_api.h" 
    #include <dlfcn.h> 
    typedef double (\*FUNC\_ADD)(int, int); 
    static napi\_value AddByLibPath(napi\_env env, napi\_callback\_info info) { 
        size\_t requireArgc = 3; 
        size\_t argc = 3; 
        napi\_value args\[3\] = {nullptr}; 
        napi\_get\_cb\_info(env, info, &argc, args, nullptr, nullptr); 
        double value0; 
        napi\_get\_value\_double(env, args\[0\], &value0); 
        double value1; 
        napi\_get\_value\_double(env, args\[1\], &value1); 
        char path\[255\]; 
        size\_t size = 255; 
        napi\_get\_value\_string\_utf8(env, args\[2\], path, 255, &size); _// Obtain dynamic library path information_ 
        void \*handle = dlopen(path, RTLD\_LAZY);                    _// Open a dynamic link library, The path is "path"._  
        dlerror(); 
        FUNC\_ADD add\_func = (FUNC\_ADD)dlsym(handle, "add"); _// Retrieve the function named "add"_ 
        if (dlerror()) { 
            return nullptr; 
        } 
        double res = add\_func(value0, value1);             _// Call add and pass the parameter information_ 
        dlclose(handle);                                   _// Close the dynamic library_ 
        napi\_value sum; 
        napi\_create\_double(env, res, &sum); 
        return sum; 
    } 
    _// ..._