在 Android Studio (cmake) 中使用预构建的共享库 [英] Using a prebuilt shared Library in Android Studio (cmake)

查看:56
本文介绍了在 Android Studio (cmake) 中使用预构建的共享库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的 Android 应用程序中使用 C++ 共享库.我试图按照 Google 的 NDK 示例中的 hello-libs 示例进行操作,但不知何故它不起作用.

I want to use a C++ shared library inside my Android App. I tried to follow along the hello-libs example from Google's NDK samples, but somehow it doesn't work out.

看来,我的库没有打包到 APK 中.我发现的所有教程都使用 *.mk-files,但我想使用 cmake.

It seems, that my library isn't packed into the APK. All tutorials i found are using *.mk-files, but I want to use cmake.

这是我的 CMakeLists.txt:

This is my CMakeLists.txt:

# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.4.1)

# native lib
add_library(native-lib SHARED
            native-lib.cpp)

set(IMPORT_DIR ${CMAKE_SOURCE_DIR}/../jniLibs)

# shared lib
add_library(shared-lib SHARED IMPORTED)
set_target_properties(shared-lib PROPERTIES IMPORTED_LOCATION
                      ${IMPORT_DIR}/dynamic/lib/${ANDROID_ABI}/libLibrary.so)
target_include_directories(native-lib PRIVATE
                           ${IMPORT_DIR}/dynamic/include)

# linking libs
target_link_libraries(native-lib
                      android
                      shared-lib
                      log)

一旦我将 shared-lib 与其他链接在一起,运行应用程序时就会出现以下错误:

As soon as I link the shared-lib along with the others, I get the following error, when running the app:

E/art: dlopen("/data/app/my.package.name-1/lib/arm/libnative-lib.so", RTLD_LAZY) failed: dlopen failed: library "C:/projects/HelloLibs/app/src/main/cpp/../jniLibs/dynamic/lib/armeabi-v7a/libLibrary.so" not foun
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: my.package.name, PID: 29266
                  java.lang.UnsatisfiedLinkError: dlopen failed: library "C:/projects/HelloLibs/app/src/main/cpp/../jniLibs/dynamic/lib/armeabi-v7a/libLibrary.so" not found
                      at java.lang.Runtime.loadLibrary(Runtime.java:371)
                      at java.lang.System.loadLibrary(System.java:988)
                      at my.package.name.MainActivity.<clinit>(MainActivity.java:11)
                      at java.lang.reflect.Constructor.newInstance(Native Method)
                      at java.lang.Class.newInstance(Class.java:1690)
                      at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2543)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2771)
                      at android.app.ActivityThread.access$900(ActivityThread.java:177)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1432)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:135)
                      at android.app.ActivityThread.main(ActivityThread.java:5912)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:372)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)

似乎在设备上找不到该库.否则看起来库的路径仍然是我本地机器上的路径.

It looks like the library is not found on the device. Otherwise it looks like the librarie's path is still the one on my local maschine.

推荐答案

预构建的 libLibrary.so 没有 SONAME.如果由于某种原因您无法使用最新的 NDK 重建此库,您可以尝试使用 patchelf 实用程序来将 SONAME 添加到现有二进制文件中.

The prebuilt libLibrary.so does not have SONAME. If for whatever reason you cannot rebuild this library with latest NDK, you may try the patchelf utility to add SONAME to existing binary.

这篇关于在 Android Studio (cmake) 中使用预构建的共享库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆