gpt4 book ai didi

android - 在android studio项目中链接.so库

转载 作者:行者123 更新时间:2023-11-30 03:26:02 26 4
gpt4 key购买 nike

如标题所示,我正在尝试将原生 .so 链接到 android studio 项目。我浏览了 android 开发者网站上的文档和更多文章,但未能将 .so 文件与项目连接起来。

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

CMake Error: The following variables are used in this project, but they are set to NOTFOUND. Please set them or make sure they are set and tested correctly in the CMake files: 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 studio 抛出了上述错误。我的实际目标是根据手机中的芯片动态加载库。我很确定我当前的代码没有实现这一目标。

这是我的问题

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

  2. 如何将 .so.h 文件添加到原生 android studio 项目中?那有基于运行代码的芯片的变化吗?

  3. 当我直接将 .h 文件添加到 native 文件夹时,我可以在我的 C 代码中引用该 header 中的类和函数,但我无法运行该代码。我在 .h 文件中有一个 getInstance() 方法。每当我调用getInstance() 函数表示 undefined reference to getInstance()。据我了解,“.h”文件已正确链接,但 .so 文件中实际存在的 .h 文件的功能定义是没有联系。我相信如果问题 1 和 2 得到回答,这个问题就会得到解决。

  4. 是不是所有的原生android项目都需要一个.mk文件?我没有将它添加到我的项目中,我认为这可能是我遇到错误的原因之一。

最佳答案

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

这是适合您的 CMakeLists.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}

关于android - 在android studio项目中链接.so库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48611514/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com