目录

在HarmonyOS工程中如何引用外部构建的so库

问题现象

在工程中如何引用已经适配HarmonyOS的so库?

背景知识

参考官网ArkTS侧引用三方so库的文档,通过配置模块动态依赖即可在工程中引用已经适配HarmonyOS的so库。

解决方案

常见FAQ

Q:har包依赖so库时编译报错:
Only the following .so dependencies are allowed: external .so files located in libs/arm64-v8a or libs/x86_64 and internal .so files listed in CMakeLists.txt.

A:CMakeLists.txt中声明的so库名称与引用的so库名称不一致导致,需要检查CMakeLists.txt声明的so库名称。

Q:将已有的几个so库编译合并成一个so库时发生报错:

ERROR: Duplicated files found in module editsdk. This may cause unexpected errors at runtime. 
- D:\friendDemo\cantfindso\ohosApp\depend\editsdk\build\default\intermediates\cmake\default\obj\arm64-v8a\libeditsdk.so
- D:\friendDemo\cantfindso\ohosApp\depend\editsdk\libs\arm64-v8a\libeditsdk.so


* Try the following:
> Set .so file priorities with pickFirsts, pickLasts, or select option.
> Make sure each .so file name is unique.
> More info: https://developer.huawei.com/consumer/cn/doc/harmonyos-faqs-V5/faqs-compiling-and-building-109-V5


> hvigor ERROR: Failed :editsdk:default@ProcessLibs... 
> hvigor ERROR: BUILD FAILED in 3 s 795 ms

A:根据编译报错,结合CMakeLists.txt代码可知,目标生成的so库和已有so库重名导致编译错误。将目标so库的名字改成和已有so库不同的即可解决。

Q:工程中两个so内有同名同入参的函数,是否会导致调用关系错乱?

A:工程中两个so内有同名同入参的函数确实可能会导致调用关系错乱,建议添加命名空间区分来自不同库的内容,或者使用dlopen来加载指定的so,参考:多so相互依赖场景下如何解耦。

Q:使用dlopen动态加载自定义沙箱路径下的so失败是什么原因?

A:为保障用户隐私安全,dlopen具有命名空间隔离能力,应用可以加载的动态库受到命名空间的限制。一般应用只能够加载应用安装包目录/data/storage/el1/bundle下的动态库,以及系统内置对外开放的动态库。

若加载自定义路径动态库会报错:MUSL-LDSO bundlename E Open absolute_path library: check ns accessible failed, pathname libxxx.so namespace moduleNs_default。

参考:通过调用dlopen的方式引用。