.so
库链接到Android Studio项目中。我已经查阅了Android开发者网站上的文档和其他一些文章,但是在连接.so
文件与项目时没有成功。每当我尝试运行代码时,就会出现以下错误:
这是我的CMake文件。CMake错误:此项目中使用了以下变量,但它们被设置为NOTFOUND。请设置它们或确保在CMake文件中正确地设置和测试它们:testlib
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”文件正确链接,但实际上存在于“.so”文件中的函数的定义并未链接。如果回答问题1和2,我相信这个问题将得到解决。所有本地Android项目都需要一个.m文件吗?我没有将此文件添加到我的项目中,并认为这可能是我遇到的错误之一。