NDK development notes --- CMake to build JNI

  Starting from AndroidStudio2.2, CMake is used by default to build NDK projects, or ndk-build can also be used. Here we mainly introduce the CMake method.

  First, introduce the download and installation of NDK. On the SDK settings page, select the SDK Tools panel to download the NDK.


As shown in the figure, you need to download CMake, LLDB, NDK components:

  • NDK

  This tool set allows you to use C and C++ code for Android, and provides numerous platform libraries that allow you to manage native Activity and access physical device components, such as sensors and touch input.

  • CMake

An external build tool that can be used with Gradle to build native libraries. If you only plan to use ndk-build, you don't need this component.
  • LLDB
A debugging program that Android Studio uses to debug native code.

Create projects with C/C++ support by default

When we create the project, in the Configure your new project section of the wizard, select the Include C++ Support check box as shown in the following figure:


In the Customize C++ Support section of the wizard, you can customize the project with the following options:


  • C++ Standard
Use the drop-down list to select which C++ standard you want to use. Choosing Toolchain Default will use the default CMake settings
  • Exceptions Support
If you want to enable support for C++ exception handling, please select this check box. If this check box is enabled, Android Studio will add the -fexceptions flag to the cppFlags of the module-level build.gradle file, and Gradle will pass it to CMake.
  • Runtime Type Information Support
如果您希望支持 RTTI,请选中此复选框。如果启用此复选框,Android Studio 会将 -frtti 标志添加到模块级 build.gradle 文件的 cppFlags 中,Gradle 会将其传递到 CMake。

可以根据自己项目需求进行选择。在这里,因为暂时用不到,直接点击完成即可。创建好的项目如下图所示:

其中:
  • cpp目录存放C/C++的头文件或者源文件
  • External Build Files存放我们的CMake脚本文件,这是通过Gradle来进行配置的。

项目自动生成的部分c++代码:



自动生成的部分Java代码:


自动生成的build.gradle文件:



android里多了:
externalNativeBuild {
    cmake {
        path "CMakeLists.txt"
    }
}

defaultConfig 里多了:
externalNativeBuild {
    cmake {
        cppFlags ""
    }
}

在主Mudule的跟目录下多了个CMakeLists.txt,我们定制自己的原生代码的时候主要就是修改CMakeLists.txt里面的配置:

我们可以看下CMakeLists.txt里面的配置:


  •  add_library 

里面有三个参数配置:
    native-lib:设置库的名字为native-lib,名字可以任意,但是要和System.loadLibrary("native-lib");保持一致 。
   SHARED:可以分享的,动态库。
   src/main/cpp/native-lib.cpp:配置源文件或者头文件的路径

  • find_library

    将find_library()命令添加到CMake构建脚本中以定位NDK库,并将其路径存储为一个变量。可以使用此变量在构建脚本的其他部分引用NDK库。
    log:找到log模块

  • target_link_libraries 

     指定要关联到原生库的库,第一个自然是我们add_library里面指定的库名字native-lib库,然后可以看到${log-lib},也就是引用了find_library里面定义的日志库。
     经过上面一系列的配置,项目就可以正常运行起来了。

实例:
  我们可以稍微修改代码,然后传一个字符串,在界面显示出来



在native-lib里,以log库为例,log库是android下的,如果我们新建项目的时候勾选上了 Incude C++ Support,那么自动生成的CMakeLists.txt里面默认会为我们添加log库。这里宏定义了个 LOG_TAG ,并宏定义打印函数 __android_log_print ,我们传入 ANDROID_LOG_ERROR ,所以是E级别。


输出的结果为:





为已有项目添加C/C++支持

上面介绍的是用 Android Studio 创建带C/C++支持的默认项目,下面我们介绍如何为已经有的项目添加C/C++支持。为了给出例子,我们先创建一个cmake-jni的项目。
首先,可以手动创建一个JNI目录,如下图所示:

然后在这个目录可以手动创建我们的C/C++源文件:fileCrypt.c

创建了一个CMake类:

在MainActivity里面代码调用:

在cmake-jni的目录下面创建一个File,名字为CMakeLists.txt,推荐使用这个名字和文件路径。
注意:必须先创建源文件,否则下面创建CMake脚本同步的时候不会通过。



CMakeLists.txt文件的内容为:

注意:

   1. 路径一定要注意跟我们所创建的目录名字一致,注意你创建的是“jni”还是“cpp”目录,否则同步不了。例如我们刚刚通过 Android Studio 创建的目录实质上是“jni”目录,因此这里写jni。你也可以手动创建 cpp目录,然后这里写 cpp目录,与标准的项目一样。

   2.我们创建的有可能是 C 也有可能是 C++,所以这里要注意写 .c 还是 .cpp 后缀,否则同步会失败。


然后选中 app,右击,选择下图中的选项:


找到我们创建的脚本文件,确认:


Android Studio 就会自动同步,然后在 cmake-jni模块的 build.gradle文件自动添加配置:


输出的结果为:


参考文章:

http://mp.weixin.qq.com/s/5DHJa5E-lZzy21-9edxoIg

http://mp.weixin.qq.com/s/_qwK5VNI40TO44eonDLo2

http://blog.csdn.net/qq_35071078/article/details/70544766



Guess you like

Origin blog.csdn.net/xufei5789651/article/details/73694634