android - 如何使用 CMake 依赖于 Android native 代码?

标签 android gradle android-ndk cmake

我目前正在为 Android 应用程序 编写 JNI 代码以使用一些遗留 native 代码。我选择使用 CMake 来尝试一下(这是我第一次使用它)。

我想使用 Jansson 将一个相当复杂的 C 结构序列化为 JSON 格式图书馆。这将使它更容易暴露给 Java。

这是我的问题:如何在我的项目中导入 Jansson 作为我自己代码的依赖项?

我尝试在我自己的源中导入 Jansson 并使用 add_subdirectory 子句来构建它。结果,我可以在输出中看到一些中间 CMake 文件,但没有实际编译的文件

这是我的 CMake 文件的样子:

cmake_minimum_required(VERSION 3.4.1)

include_directories(
             src/main/cpp/xxx/Include
             )

add_subdirectory("src/main/cpp/jansson")

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).
             # Associated headers in the same location as their source
             # file are automatically included.
             src/main/cpp/xxx/file1.c
             src/main/cpp/xxx/file2.c
             src/main/cpp/xxx/file3.c
             src/main/cpp/xxx/file4.c
             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

                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )

欢迎任何帮助!!

最佳答案

好的,所以我只是缺少声明我的库对 Jansson 的依赖性。这是通过将其项目名称添加到现有的 target_link_library 子句中实现的。

target_link_libraries( # Specifies the target library.
                       native-lib

                       # Links the target library to the log library
                       # included in the NDK.
                       jansson
                       ${log-lib} )

如果其他人(也可能是我 future 的自己)遇到同样的问题,以下是在 Android CMake 文件中添加 native 依赖项的所有必要步骤:

  • 查找已经支持 CMake 的库
  • 在你的项目中导入库代码(我使用的是 git submodule)
  • 添加 add_subdirectory 子句,指向包含库 CMake 文件的文件夹
  • 将包含文件添加到 include_directories(希望导入的库为此设置一个变量,例如我的 JANSSON_INCLUDE_DIRS
  • 通过将库项目名称添加到target_link_libraries 子句来添加依赖

完成后:噗!魔法 !一切正常!

关于android - 如何使用 CMake 依赖于 Android native 代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40264438/

相关文章:

android - 如何拦截ActionBarSherlock中 "Up"(主页按钮)按钮的点击?

unit-testing - Android Studio与Gradle测试中的Robolectric不同线程

android - 如何在我的 gradle 项目中添加 JAR?

android - 未找到 JNI_load..应用程序关闭并在循环中运行

Android.mk : How to check if a module already exists

android - Here Maps Android SDK位置指示器旋转

java - 获取 Android ProgressBar 中 secondaryProgress 的宽度

android - 如何将 SwitchPreference 样式设置为 "normal"开关?

安卓工作室 : how to generate signed APK using Gradle?

android - 来自 git 的 Linphone Android 缺少 R.raw.rootca(ubuntu 和 win7)