链接 .so 库在安卓工作室项目中



正如标题所示,我正在尝试将原生.so链接到Android工作室项目。我已经浏览了 android 开发人员网站上的文档和更多文章,但未能成功将.so文件与项目连接起来。

每当我尝试运行代码时,我都会收到以下错误

CMake 错误:此项目中使用以下变量,但 它们设置为"未找到"。请设置它们或确保它们已设置 并在 CMake 文件中正确测试:testlib

这是我的CMake文件

cmake_minimum_required(VERSION 3.4.1)
add_library( # Sets the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
src/main/cpp/native-lib.cpp )
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log )
add_library(testlib SHARED IMPORTED)
set_property(TARGET testlib PROPERTY IMPORTED_LOCATION "E:/project/Remote_Native/remote_attempt_1/app/libs/armeabi-v7a/libremotedesktop_client.so")
#find_path(testlib E:/project/Remote_Native/remote_attempt_1/app/libs/armeabi-v7a/RemoteDesktop.h)
find_library(testlib E:/project/Remote_Native/remote_attempt_1/app/libs/armeabi-v7a/libremotedesktop_client.so)
#add_library(remote SHARED IMPORTED)
#set_target_properties(remote PROPERTIES IMPORTED_LOCATION libs/${ANDROID_ABI}/libremotedesktop_client.so )
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library.
native-lib
# Links the target library to the log library
# included in the NDK.
${log-lib}
${testlib})
target_include_directories()

我有四个.so文件,分别用于arm64,armeabi,armeabi-v7a,x86。我已经在路径中对 armeabi-v7a lib 进行了编码,android 工作室在我这样做时会抛出上述错误。我的实际目标是根据手机中的芯片动态加载库。我很确定我当前的代码没有实现这一目标。

这是我的查询

  1. 如何解决我遇到的错误?我尝试提供相对路径和绝对路径,但无济于事,我遇到了同样的错误。

  2. 如何将.so.h文件添加到原生安卓工作室项目中?这是否基于运行代码的芯片而有所不同?

  3. 当我直接将.h文件添加到本机文件夹时,我可以在 C 代码中引用该标头中的类和函数,但我无法运行代码。我在.h文件中有一个getInstance()方法。每当我打电话给getInstance()它说undefined refernce to getInstance()的功能。我从中了解到的是".h"文件链接正确,但.so文件中实际存在的.h文件的功能定义没有链接。我相信,如果问题1和2得到回答,这个问题将得到解决。

  4. 所有原生安卓项目都需要有一个 .mk 文件吗?我没有在我的项目中添加一个,并认为这可能是我收到错误的原因之一。

在你的情况下,你不需要find_library。对于日志,库由 NDK 为您解析;对于libremotedesktop_client.so,您知道确切的路径。

以下是适合您的CMakeList.txt

cmake_minimum_required(VERSION 3.4.1)
add_library( # Sets the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
src/main/cpp/native-lib.cpp )
add_library(remote SHARED IMPORTED)
set_property(TARGET remote PROPERTY IMPORTED_LOCATION "E:/project/Remote_Native/remote_attempt_1/app/libs/${ANDROID_ABI}/libremotedesktop_client.so")
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library.
native-lib
# Links the target library to the log library
# included in the NDK.
log
remote)

请注意,在 CMake 脚本中使用完整路径 (E:/project...) 不是最佳做法;您可能可以相对于CMakeLists.txt的路径以某种方式表示此库的路径,这是${CMAKE_CURRENT_SOURCE_DIR}

1-2)。 首先,在 CMakeList 的开头添加set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY BOTH).txt(紧随cmake_minimum_required(...)之后)以允许库的其他搜索路径。在此之后,您只需使用标准find_library即可找到 lib:

find_library(FOO_LIBRARY
foo # Lib's full name in this case - libfoo.so
PATHS path/to/lib/)
if (NOT FOO_LIBRARY)
message(FATAL_ERROR "Foo lib not found!")
endif()

您可以使用变量ANDROID_ABI来获取特定的库版本,如果目录包含以这种方式组织的库:

- path/to/lib
- armeabi-v7a
- libfoo.so
- arm64-v8a
- libfoo.so
- x86
- libfoo.so
- x86_64
- libfoo.so

因此,在这种情况下搜索路径:path/to/lib/${ANDROID_ABI}/

要在项目中包含标题,只需使用include_directories

include_directories(path/to/headers)

4). 仅当您使用ndk-build时才需要 .mk 文件(因此您不需要任何文件)

最新更新