如何使用cmake在android studio中添加外部库?

时间:2018-10-05 12:18:43

标签: android cmake android-ndk

我正在尝试使用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',   丢失,没有已知的规则

1 个答案:

答案 0 :(得分:1)

${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)