首页 > 解决方案 > 如何使用 cmake 在 android studio 中添加外部库?

问题描述

我正在尝试使用 cMake 将 live555.so 和 .h 文件与 Android 项目链接。如果我不使用绝对路径,我会出错。

我的cMake文件:

cmake_minimum_required(VERSION 3.4.1)

include_directories( ${PROJECT_SOURCE_DIR}/src/main/jniLibs/live555/include )

add_library( live555 SHARED IMPORTED )

set_target_properties( live555 PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/src/main/jniLibs/live555/lib/${ANDROID_ABI}/live555.so)

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 )

target_link_libraries( # Specifies the target library.
                   native-lib
                   android
                   live555
                   ${log-lib} )

错误

错误:错误:'../../../../src/main/jniLibs/live555/lib/armeabi-v7a/live555.so',需要'../../../.. /build/intermediates/cmake/debug/obj/armeabi-v7a/libnative-lib.so',缺少并且没有已知的规则来制作它

标签: androidcmakeandroid-ndk

解决方案


${PROJECT_SOURCE_DIR}是主 CMakeList.txt 文件所在的目录。这通常是app/src/main/cpp。您现在可以计算出正确的相对路径。

这意味着你可以简单地写

include_directories( ${PROJECT_SOURCE_DIR}/../jniLibs/live555/include )

add_library( live555 SHARED IMPORTED )

set_target_properties( live555 PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/../jniLibs/live555/lib/${ANDROID_ABI}/live555.so)

推荐阅读