【NDK Lab】CMAKE 创建新的C或C++原生库(基础篇)

1、创建新项目(Create New Project)

点击File — New — New Project,把Include C++ Support前面的CheckBook勾上,然后依次进行接下来的操作

create new project

2、配置C++支持功能(Customize C++ Support)

在Customize C++ Support界面默认即可

customize C++ Support

  • C++ Standard
    指定编译库的环境,其中Toolchain Default使用的是默认的CMake环境;C++ 11也就是C++环境。
  • Exceptions Support
    如果选中复选框,则表示当前项目支持C++异常处理,如果支持,在项目Module级别的build.gradle文件中会增加一个标识 -fexceptions到cppFlags属性中,并且在so库构建时,gradle会把该属性值传递给CMake进行构建。
  • Runtime Type Information Support
    同理,选中复选框,项目支持RTTI,属性cppFlags增加标识-frtti

JniCmake项目主体结构

3、CMakeLists.txt构建

CMakeLists.txt文件用于配置JNI项目属性,主要用于声明CMake使用版本、so库名称、C/CPP文件路径等信息,下面是该文件内容:

# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.4.1)

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.

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 )

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

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 )

# 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} )
  • cmake_minimum_required(VERSION 3.4.1)
    CMake最小版本使用的是3.4.1。

  • add_library()
    配置so库信息(为当前脚本文件添加库)

    • native-lib这个是声明引用so库的名称,在项目中,如果需要使用这个so文件,引用的名称就是这个。值得注意的是,实际上生成的so文件名称是libnative-lib。当Run项目或者build项目是,在Module级别的build文件下的intermediates\transforms\mergeJniLibs\debug\folders\2000\1f\main
      下会生成相应的so库文件。
    • SHARED这个参数表示共享so库文件,也就是在Run项目或者build项目时会在目录intermediates\transforms\mergeJniLibs\debug\folders\2000\1f\main
      下生成so库文。此外,so库文件都会在打包到*.apk里面,可以通过选择菜单栏的Build->Analyze Apk…查看apk中是否存在so库文件,一般它会存放在lib目录下。
    • src/main/cpp/native-lib.cpp 构建so库的源文件。
  • STATIC:静态库,是目标文件的归档文件,在链接其它目标的时候使用。
  • SHARED:动态库,会被动态链接,在运行时被加载。
  • MODULE:模块库,是不会被链接到其它目标中的插件,但是可能会在运行时使用dlopen-系列的函数动态链接
  • include_directories
    配置头文件路径目录

  • find_library()
    这个方法与我们要创建的so库无关而是使用NDK的Apis或者库,默认情况下Android平台集成了很多NDK库文件,所以这些文件是没有必要打包到apk里面去的。直接声明想要使用的库名称即可(猜测:貌似是在Sytem/libs目录下)。在这里不需要指定库的路径,因为这个路径已经是CMake路径搜索的一部分。如示例中使用的是log相关的so库。

    • log-lib这个指定的是在NDK库中每个类型的库会存放一个特定的位置,而log库存放在log-lib中
    • log指定使用log库
  • target_link_libraries()
    如果你本地的库(native-lib)想要调用log库的方法,那么就需要配置这个属性,意思是把NDK库关联到本地库。

    • native-lib要被关联的库名称
    • loglib 符号去引用。

实际上,我们可以自己创建CMakeLists.txt文件,而且路径不受限制,只要在build.gradle中配置externalNativeBuild.cmake.path来指定该文件路径即可

4、MainActivity调用和运行结果

MainActivity

JniCmake运行效果图

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值