安卓NDK开发之开发环境搭建以及使用的注意事项

前言:在AS之前我们使用Eclipse开发,稍有麻烦,今天我们就使用谷歌推荐的AS下的默认开发方式--Cmake开发方式,废话少说直接开始!

----------分割线--------

AndroidStudio下载:Android Studio 中文社区(官网) -Android App开发工具集-Android SDK/JDK/ADT/Gradle-2.0/2.2下载/安装/教程  

AS和NDK下载:AndroidDevTools Android SDK下载 Android Studio下载 Gradle下载 SDK Tools下载  

---------分割线-------

创建NDK项目:

1.勾选 Include C++ Support 选项

.

2.不解释


3.不解释


4.不解释


5.C++ Standard :点击下拉框,可以选择标准 C++,或者选择默认 CMake 设置的 Toolchain Default 选项(我们这里就是使用Cmake方式)。

Exceptions Support :如果你想使用有关 C++ 异常处理的支持,就勾选它。勾选之后,Android Studio 会在 module 层的 build.gradle 文件中的 cppFlags 中添加 -fexcetions 标志。

Runtime Type Information Support :如果你想支持 RTTI,那么就勾选它。勾选之后,Android Studio 会在 module 层的 build.gradle 文件中的 cppFlags 中添加 -frtti 标志。


6.点击完成之后,我们还需要设置一下NDK的路径(如果路径已经自动设置好请忽略)。


7.然后静等几十秒AS自动刷新会生成一下几个文件:

cpp 文件夹存放你所有 native code 的地方,包括源码,头文件,预编译项目等。对于新项目,Android Studio 创建了一个 C++ 模板文件: native-lib.cpp ,并且将该文件放到了你的 app 模块的 src/main/cpp/ 目录下。这份模板代码提供了一个简答的 C++ 函数: stringFromJNI() ,该函数返回一个字符串:”Hello from C++”。

External Build Files 文件夹是存放 CMake 或 ndk-build 构建脚本的地方。有点类似于 build.gradle 文件告诉 Gradle 如何编译你的 APP 一样,CMake 和 ndk-build 也需要一个脚本来告知如何编译你的 native library。对于一个新的项目,Android Studio 创建了一个 CMake 脚本: CMakeLists.txt ,并且将其放到了你的 module 的根目录下。


8.运行:


-------注意事项--------

1.关于文件夹以及c文件的名字问题我们是可以更改的:


2.只要项目引入NDK路径,我们也可以自己建一个支持NDK开发的moudle:

创建c文件(名字和CmakeLists.txt里面的名字保持一致):(注意:刚创建的时候文件夹颜色不是蓝色的)


先把上一个项目的CmakeLists.txt复制到你现在的moudle里面,然后创建:


最后在build.gradle文件里添加:



最后等刷新然后成!

3.如何创建多个cpp文件支持:

CmakeLists.txt文件:

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 )
改为:
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.

file(GLOB native_srcs "src/main/cpp/*.cpp")
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.
             ${native_srcs})
ok!

----------完----------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

等待着冬天的风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值