Android NDK including a 3rd party prebuilt shared

2019-08-08 10:29发布

问题:

I'm struggling to include a prebuilt shared library in my android project

The library in question is libusb, which the NDK part of my android project requires.

Everything is compiling and linking OK, i.e. the project is building successfully, but on installing the apk on my device the app is crashing.

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:

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:

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.

Still getting same error message in Monitor after the apk has been installed..

回答1:

Give a try to this one, instead of taking the path from env you should try ${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})