android studio jni cmake(1) 创建新项目

来源:互联网 发布:男生化妆知乎 编辑:程序博客网 时间:2024/05/22 11:54
在android studio 2.2 以后可以使用Cmake编译C和C++文件,打包APK的时候,会自动将编译好的动态链接库大包进去。首先介绍新建一个基于Cmake的jni 项目。1、安装cmake 和LLDBCMake:一款外部构建工具,可与 Gradle 搭配使用来构建原生库。如果您只计划使用 ndk-build,则不需要此组件。LLDB:一种调试程序,Android Studio 使用它来调试原生代码。使用SDK Manager下载

这里写图片描述

这里写图片描述
然后点击“apply”进行安装。

2、创建项目
新建项目,选中”include C++ support”,然后点击“next”,一直点击next
这里写图片描述
这里写图片描述

C++ standard :选择C++标准 可以选择默认的编译链或标准C++11支持。
Exceptions Support:表示当前项目支持C++异常处理,如果支持,在项目Module级别的build.gradle文件中会增加一个标识 -fexceptions到cppFlags属性中,并且在so库构建时,gradle会把该属性值传递给CMake进行构建。
Runtime Type Information Support:项目支持RTTI,属性cppFlags增加标识-frtti。
点击“finish”完成。
这里写图片描述
build.gradle关于对C++的支持
这里写图片描述
项目自动生成的CPP的源码目录和CMakeLists.txt构建脚本文件

3、认识CMakeLists.txt构建脚本文件

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

Sets the minimum version of CMake required to build the native # library. You should either keep the default value or only pass a # value of 3.4.0 or lower.

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 it 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).         # Associated headers in the same location as their source         # file are automatically included.         src/main/cpp/native-lib.cpp )

Searches for a specified prebuilt library and stores the path as a # variable. Because system libraries are included 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 the # 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-系列的函数动态链接。更详细的解释请参考这篇文章:C++静态库与动态库头文件也可以配置头文件路径,方法是(注意这里指定的是目录而非文件):include_directories([AFTER|BEFORE] [SYSTEM] dir1 [dir2 ...])下面的配置实际上与自定义的JNI项目(自定义的so库)没有太大关系。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    要被关联的库名称    ${log-lib}    要关联的库名称,要用大括号包裹,前面还要有$符号去引用。实际上,我们可以自己创建CMakeLists.txt文件,而且路径不受限制,只要在build.gradle中配置externalNativeBuild.cmake.path来指定该文件路径即可。
原创粉丝点击