简体   繁体   English

Android NDK包括第三方预建共享库,包含Gradle和CMake

[英]Android NDK including a 3rd party prebuilt shared library with Gradle and CMake

I'm struggling to include a prebuilt shared library in my android project 我正努力在我的android项目中包含一个预建的共享库

The library in question is libusb, which the NDK part of my android project requires. 有问题的库是libusb,我的android项目的NDK部分需要它。

Everything is compiling and linking OK, ie the project is building successfully, but on installing the apk on my device the app is crashing. 一切都在编译和连接好,即项目正在建设成功,但在我的设备上安装apk时,应用程序崩溃了。

The relevant error msg from monitor is: 监视器的相关错误消息是:

java.lang.UnsatisfiedLinkError: dlopen failed: library "libusb1.0.so" not found

what i've tried so far is adding the following to my app/build.gradle: 我到目前为止尝试的是将以下内容添加到我的app / build.gradle中:

sourceSets{
    main {
        // let gradle pack the shared library into apk
        jniLibs.srcDirs = '/home/me/third-party/libusb-1.0.21/android/libs/'
    }

in CMakeLists.txt i've added: 在CMakeLists.txt中,我添加了:

set(libusb_DIR $ENV{HOME}/third-party/libusb-1.0.21/android/libs)
set(libusb_LIB usb1.0)

link_directories( ${libusb_DIR}/${ANDROID_ABI}/ )
target_link_libraries( ${libusb_LIB} )

I've even tried creating a app/src/main/jniLibs dir and manually copying the armeabi-v7a version of the shared lib, libusb1.0.so , in there. 我甚至尝试创建app/src/main/jniLibs目录并手动复制共享库libusb1.0.so的armeabi-v7a版本。

Still getting same error message in Monitor after the apk has been installed.. 在安装apk之后,在Monitor中仍然会收到相同的错误消息。

Give a try to this one, instead of taking the path from env you should try ${CMAKE_SOURCE_DIR} 尝试一下这个,而不是从环境中获取路径你应该尝试$ {CMAKE_SOURCE_DIR}

set(usb_DIR ${CMAKE_SOURCE_DIR}/../../../../libs)
add_library(libusb SHARED IMPORTED)
set_target_properties(libusb PROPERTIES IMPORTED_LOCATION
    ${usb_DIR}/libs/${ANDROID_ABI}/libusb1.0.so)

target_link_libraries(${libusb})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM