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

2017-08-24  本文已影响77人  soulrelay

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} )
  • 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-系列的函数动态链接
  • log-lib这个指定的是在NDK库中每个类型的库会存放一个特定的位置,而log库存放在log-lib中
  • log指定使用log库
  • native-lib要被关联的库名称
  • ${log-lib}要关联的库名称,要用大括号包裹,前面还要有$符号去引用。

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

4、MainActivity调用和运行结果

MainActivity JniCmake运行效果图

其它

上一篇 下一篇

猜你喜欢

热点阅读