【问题标题】:Unable to load native library which is present in the app无法加载应用程序中存在的本机库
【发布时间】:2021-07-14 04:03:45
【问题描述】:

我正在使用 Android NDK 构建本机应用程序。它依赖于各种共享对象 (.so)。我已将它们添加到 CMakeLists 中的应用程序中,如下所示:

add_library(bluetooth SHARED IMPORTED)
/home/stoic/fluoride/bt/output_dir/out/Default/lib/libbluetooth.so)
set_target_properties(bluetooth PROPERTIES IMPORTED_LOCATION /home/stoic/lib-bluetooth-x86-64/libbluetooth.so)
add_library(chrome SHARED IMPORTED)
set_target_properties(chrome PROPERTIES IMPORTED_LOCATION /home/stoic/lib-bluetooth-x86-64/libchrome.so)
add_library(grpc++ SHARED IMPORTED)
set_target_properties(grpc++ PROPERTIES IMPORTED_LOCATION /home/stoic/lib-bluetooth-x86-64/libgrpc++.so)
add_library(grpc_wrap SHARED IMPORTED)
set_target_properties(grpc_wrap PROPERTIES IMPORTED_LOCATION /home/stoic/lib-bluetooth-x86-64/libgrpc_wrap.so)


target_link_libraries(native-activity
    android
    native_app_glue
    EGL
    GLESv1_CM
    log
        bluetooth
        chrome
        grpc++
        grpc_wrap
        /home/stoic/lib-bluetooth-x86-64/libstatslog.so
        /home/stoic/lib-bluetooth-x86-64/android.hardware.bluetooth.a2dp@1.0.so
)

我没有写整个 CMakeLists.txt,因为应用程序编译良好并且 apk 构建成功。

所有 .sos 加载正常,但 Android 运行时为 android.hardware.bluetooth.a2dp@1.0.so 提供此错误:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.native_activity, PID: 4230
    java.lang.UnsatisfiedLinkError: Unable to load native library "/data/app/~~J6dVW4LGncsbvMxzwtgq7w==/com.example.native_activity-Je33LNoodZDV37TTvXSpEw==/lib/x86_64/libnative-activity.so": dlopen failed: library "android.hardware.bluetooth.a2dp@1.0.so" not found: needed by /data/app/~~J6dVW4LGncsbvMxzwtgq7w==/com.example.native_activity-Je33LNoodZDV37TTvXSpEw==/lib/x86_64/libnative-activity.so in namespace classloader-namespace

我检查了已构建应用程序的解压后的 apk,我看到 lib/x86_64 目录中存在 android.hardware.bluetooth.a2dp@1.0.so 以及其他 .sos。

ls lib/x86_64/
android.hardware.bluetooth.a2dp@1.0.so  libbluetooth.so  libchrome.so  libgrpc++.so  libgrpc_wrap.so  libnative-activity.so  libstatslog.so

有人知道我在做什么有什么问题吗?

【问题讨论】:

标签: android bluetooth android-ndk runtime-error


【解决方案1】:

我能够在 cmets 中使用 @Michael 指出的 answer 解决问题。我发现的另一件事是,Android 也不支持库名称中的字符 @,所以我也必须将其替换为 _。 Android 没有关于此的文档,我是通过反复试验发现的。

我使用 sed 而不是原始答案中使用的 rpl。

sed -i "s/@\([0-9]\).\([0-9]\).so/_\1_\2.so/g" libbluetooth.so

我对我的应用程序中使用的所有库都做了同样的事情。为了解决所有库的问题 - 我将所有库复制到一个目录中并使用了这个脚本:

echo "" > change.log
for var in `ls -1`;do
    echo $var
    sed -i "s/@\([0-9]\).\([0-9]\).so/_\1_\2.so/g" $var
    new_file=`echo $var|sed "s/@\([0-9]\).\([0-9]\).so$/_\1_\2.so/g"`
    mv $var $new_file
    objdump -p $new_file | grep so >> change.log
done

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-13
    • 2013-07-14
    • 2016-05-08
    • 1970-01-01
    • 2012-11-17
    • 1970-01-01
    • 2014-05-06
    • 1970-01-01
    相关资源
    最近更新 更多