【发布时间】:2018-07-14 15:45:29
【问题描述】:
正如标题所示,我正在尝试将原生 .so 链接到 android studio 项目。我浏览了 android 开发者网站中的文档和更多文章,但未能成功将 .so 文件与项目连接起来。
每当我尝试运行代码时,我都会收到以下错误
CMake 错误:此项目中使用了以下变量,但是 它们被设置为 NOTFOUND。请设置它们或确保它们已设置 并在 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 库进行了编码,当我这样做时,android studio 会抛出上述错误。我的实际目标是根据手机中的芯片动态加载库。我很确定我当前的代码没有实现这一点。
这是我的疑问
如何解决我遇到的错误?我试过同时给出相对路径和绝对路径,但无济于事,我得到了同样的错误。
如何将
.so和.h文件添加到本机android studio 项目中?这会根据运行代码的芯片而有所不同吗?当我直接将
.h文件添加到本机文件夹时,我可以在我的 C 代码中引用该标头中的类和函数,但我无法运行该代码。我在.h文件中有一个getInstance()方法。每当我打电话给getInstance()函数它说undefined refernce to getInstance()。我从中了解到的是“.h”文件已正确链接,但.h文件中实际存在的.so文件的功能定义未链接。我相信只要回答了问题 1 和 2,这个问题就会得到解决。是否所有原生安卓项目都必须有一个.mk 文件?我没有在我的项目中添加一个,并认为这可能是我遇到错误的原因之一。
【问题讨论】:
-
“这是我的疑问” - 所以问题应该是每个帖子一个问题,而不是多个问题。
-
@JesperJuhl 。您可能是对的,如果我的每个查询都有不同的答案,则该规则适用。我相信我在某些方面犯了一个菜鸟错误,这将清除我所有的 4 个疑问,这就是为什么我决定将它们全部放在一个问题中。如果这些查询的答案似乎不同,我稍后会将它们分成不同的问题。干杯:)
-
看看这个示例:github.com/googlesamples/android-ndk/tree/master/hello-libs "这个示例使用带有外部库支持的 Android Studio CMake 插件。它演示了如何:(*) 包含一个预构建的静态库(gmath) 在您的应用中;(*) 在您的应用中包含一个预构建的共享库 (gperf)"
标签: android c++ android-ndk shared-libraries