Android NDK 简单来说就是可以让你的应用使用natice-code语言, (比如C ,C++)的工具集
一:what is the ndk
Ndk 是允许你在你的工程中可以使用native-code (比如C or c++)的工具, 你可以利用ndk来构建你的natice-app
正如我们知道的那样, Java 是构建android app 的默认语言, 然而, java 代码很容易被反编译, 但是这对C++ code 来说这并不简单, 并且c++ 代码更加高效
二:why is ndk
C++ 代码反编译困难
对于计算复杂量大的代码, C or C++ code can increase the performance of your app
三:buliding native app
如果你使用android studio 开发, 你需要下载安装ndk包, More info: NDK
1, 首先在代码中声明要在C++ code中要实现的native方法
|
|
2, 对包含native
关键字方法的类, 生成.h
头文件
进入到本地Java文件夹下 ,在Terminal input the follow commond
|
|
first , jniproject.ndkproject
is your package name and MainActivity
is the class name that contains native code
and after that , you can see the .h
file of MainActivity
class , it looks like :
and we can looks what contents it contains
|
|
3, android.mk``application.mk
file
在你的jni directory, 新建android.mk
,
|
|
application.mk
|
|
if you want to know more information of syntax about them, here is the dox
http://android.mk/#intro
4, 编写你的natie-code 实现文件, (ig .cpp or .c)
|
|
as wo can see that in above c++ code
first , you need to include you .h
file
|
|
接下来你需要按照c++ 代码的规范编写实现方法, 请注意方法实现的命名规范 (JNIEXPORT 返回值类型 JNICALL 包名类名方法名)
5, ndk-build
under the jni
dictionary, generate .so
file
|
|
接下来你需要把生成的app/src/main/libs
下的平台的.so 文件复制到 app/libs
下
6, app/build.gradle
config seeting
|
|
the the last step is that you just need call you natice-code library
|
|
here is source code