Android NDK包括第3方使用Gradle和CMake预先构建的共享库 [英] Android NDK including a 3rd party prebuilt shared library with Gradle and CMake

查看:85
本文介绍了Android NDK包括第3方使用Gradle和CMake预先构建的共享库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力在我的android项目中包含一个预构建的共享库

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

有问题的库是libusb,这是我的android项目的NDK部分所需要的.

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

一切都在编译并链接好,即项目正在成功构建,但是在我的设备上安装apk时,应用崩溃了.

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

到目前为止,我一直在尝试将以下内容添加到我的app/build.gradle中:

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/'
    }

在我添加的CMakeLists.txt中:

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} )

我什至尝试创建app/src/main/jniLibs目录,并在其中手动复制共享库libusb1.0.so的armeabi-v7a版本.

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.

安装apk后,仍然在Monitor中收到相同的错误消息.

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

推荐答案

尝试一下,而不是从env尝试,您应该尝试$ {CMAKE_SOURCE_DIR}

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})

这篇关于Android NDK包括第3方使用Gradle和CMake预先构建的共享库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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