Android studio 2.2版本以后对C/C++的支持可以说很方便了,当然官方推荐使用Cmake完成对C/C++的支持
2.2版本以上的同学新建一个项目就知道了,步骤如下:
File -> New -> New Project,如下图:
然后勾选Include C++ support,一直next ,最后Finish以后,项目就出现了,和一般的项目略有不同,其实只要多了几个文件,而已:
1:目录下多了个CmakeLists.txt文件
2:src目录下多了一个cpp目录,里面有个.cpp文件,C++文件都是以.cpp结尾的。
3:就是build.gradle内容中添加了几行配置
一一解读一下,先来看看CmakeLists.txt 文件里面的内容是什么:
# 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.

本文介绍了如何在Android Studio 2.2及以上版本中使用CMake支持C/C++开发,创建新项目时勾选C++支持,生成包含CmakeLists.txt和cpp源文件的项目。详细步骤包括添加cpp目录,创建C++源文件,链接C++项目到Gradle,修改CmakeLists.txt,以及在Java代码中调用C++方法。编译完成后,.so文件可在build目录下找到,可用于其他项目。

1598

被折叠的 条评论
为什么被折叠?



