HarmonyOS鸿蒙Next中引入第三方 so,import 之后 undefined

HarmonyOS鸿蒙Next中引入第三方 so,import 之后 undefined

jscrash:

```swift
08-14 05:09:32.384   51777-51777   C0391E/om.exam...on/   AceGesture  com.examp...egration  I     [click_recognizer.cpp(311)-(100000:100000:scope)] Click try accept
08-14 05:09:32.387   51777-51777   C01E00/om.exam...n/   GLOBAL_I18N  com.examp...egration  E     LocaleConfig::GetSystemLocale: Read system locale failed.
08-14 05:09:32.387   51777-51777   C03F00/om.exam...n/   ArkCompiler  com.examp...egration  W     [default] [GetNativeOrCjsExports:50] Load native module failed, so is [@normalized](/user/normalized):Y&&&libentry.so&
08-14 05:09:32.387   51777-51777   C03F00/om.exam...n/   ArkCompiler  com.examp...egration  E     [default] [Call:3600] occur exception need return
08-14 05:09:32.387   51777-51777   C03F00/om.exam...n/   ArkCompiler  com.examp...egration  E     [ecmascript] Pending exception before ExecutePendingJob called, in line:5559, exception details as follows:
08-14 05:09:32.387   51777-51777   C03F00/om.exam...n/   ArkCompiler  com.examp...egration  E     TypeError: Cannot read property nativeAdd of undefined
                                                                                                   at anonymous entry (entry/src/main/ets/pages/Index.ets:80:28)
08-14 05:09:32.387   51777-51777   C03900/om.exam...tegration/   Ace  com.examp...egration  W     [jsi_types.cpp(433)-(100000:100000:scope)] after call jsFunction hasError, empty: 0, caught: 1
08-14 05:09:32.388   51777-51777   C01311/om.exam...gration/   AppMS  com.examp...egration  I     [app_mgr_client.cpp:84]get AppMgrRemote object
08-14 05:09:32.388   51777-51777   C01311/om.exam...gration/   AppMS  com.examp...egration  I     [app_mgr_proxy.cpp:1333]called
08-14 05:09:32.389   51777-51777   C01317/om.exam...ration/   AppKit  com.examp...egration  E     [main_thread.cpp:1687]
                                                                                               com.example.nativesointegration is about to exit due to RuntimeError
                                                                                               Error type:TypeError
                                                                                               Error name:TypeError
                                                                                               Error message:Cannot read property nativeAdd of undefined
                                                                                               Stacktrace:
                                                                                                   at anonymous entry (entry/src/main/ets/pages/Index.ets:80:28)
08-14 05:09:32.389   51777-51777   C01317/om.exam...ration/   AppKit  com.examp...egration  W     [main_thread.cpp:1698]hisysevent write result=0, send event [FRAMEWORK,PROCESS_KILL], pid=51777, processName=com.example.nativesointegration, msg=Kill Reason:Js Error, foreground=1
# the minimum version of CMake.
cmake_minimum_required(VERSION 3.5.0)
project(NativeSoIntegration)

set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR})

if(DEFINED PACKAGE_FIND_FILE)
    include(${PACKAGE_FIND_FILE})
endif()

include_directories(${NATIVERENDER_ROOT_PATH}
                    ${NATIVERENDER_ROOT_PATH}/include)

add_library(entry SHARED napi_init.cpp)
target_link_libraries(entry PUBLIC libace_napi.z.so)
target_link_libraries(entry PUBLIC ${NATIVERENDER_ROOT_PATH}/lib/libInfer_Protocol.so)

# Compile and link third-party SO libraries
target_link_libraries(entry PUBLIC
            ${NATIVERENDER_ROOT_PATH}/../../../libs/${OHOS_ARCH}/libnativeAdd.so
)

# 5. 添加库搜索路径(确保运行时能找到.so)
target_link_directories(entry PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}/../../../libs/${OHOS_ARCH}
)
附件超过10MB上传不了,welink。

更多关于HarmonyOS鸿蒙Next中引入第三方 so,import 之后 undefined的实战教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复

用DevEco Studio编译打包库文件,简单省力。 CMake缺乏可行的参考案例。

更多关于HarmonyOS鸿蒙Next中引入第三方 so,import 之后 undefined的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS Next中引入第三方so库出现undefined问题,通常是so文件未正确打包或加载路径问题。确保so文件放在模块的src/main/resources/rawfile目录下,并在module.json5中配置:

"module": {
  "libs": [
    {
      "name": "libthird.so",
      "type": "rawfile"
    }
  ]
}

加载时使用import third from 'libthird.so'。检查so文件架构是否与设备匹配(armeabi-v7a/arm64-v8a)。若仍报错,需确认so文件是否完整。

从日志和代码来看,问题出在加载第三方so库时出现undefined错误。关键错误信息是"TypeError: Cannot read property nativeAdd of undefined",表明系统无法正确加载libnativeAdd.so

主要问题可能出在以下几个方面:

  1. so库架构不匹配:确保libnativeAdd.so的架构与设备匹配,检查OHOS_ARCH变量是否正确设置

  2. so库路径问题:虽然设置了target_link_directories,但运行时可能找不到so库。

    • 将so文件放在src/main/libs/${OHOS_ARCH}/目录下
    • 在build-profile.json中确认so文件被打包到hap中
  3. 导出符号问题:检查libnativeAdd.so是否正确定义了nativeAdd函数,可以使用nm工具验证

  4. NAPI初始化问题:确保napi_init.cpp中正确注册了nativeAdd函数

建议先确认so文件是否被正确打包到hap中,可以使用解压工具检查生成的hap文件是否包含该so库。另外检查设备架构与so库架构是否一致。

回到顶部