一.环境搭建
1.ndk下载
比较稳定的版本是r9d,android官网只有最新版的下载链接。百度网盘资源
2.配置ndk到环境变量,进入cmd,执行ndk-build命令

如上图,则配置成功
3.android程序要调用so文件成功,模拟器架构必须是arm
如果使用的Genymotion模拟器,默认情况下是x86,下载Genymotion-ARM-Translation.zip,拖拽到Genymotion就行了
二.ndk常用命令
1.ndk-build:将C/C++文件编译成so文件
2.ndk-build clean:删除之前编译的so文件
3.javah:

三.开发第一个ndk项目
1.新建android项目
2.在android项目中,添加jni目录,用于放置C/C++文件和头文件
3.编写java层的native方法
4.生成jni头文件
使用javah classpath bin/classes -d jni com.danielhan.hellondk.MainActivity,报错如下:

解决办法:javah classpath bin/classes;D:\sdk\platforms\android-21\android.jar -d jni com.danielhan.hellondk.MainActivity
5.编写C/C++文件
6.在jni目录下添加mk文件


注:LOCAL_MODULE:生成的so文件名,如设置问hello则生成的为libhello.so
LOCAL_SRC_FILES:C/C++文件名
7.生成so文件
四.eclipse相关配置
1.如果每次修改C文件代码,都要进入cmd,执行ndk-build编译的话,会比较麻烦,可配置
①eclipse的ndk路径

②配置builder




网友评论