Android NDK 开发

来源:互联网 发布:java解析多层嵌套json 编辑:程序博客网 时间:2024/06/16 04:11

NDK 介绍

NDK 官方网页 http://developer.android.com/sdk/ndk/index.html

The Android NDK is a companion tool to the Android SDK that lets you build performance-critical portions of your apps in native code. It provides headers and libraries that allow you to build activities, handle user input, use hardware sensors, access application resources, and more, when programming in C or C++. If you write native code, your applications are still packaged into an .apk file and they still run inside of a virtual machine on the device. The fundamental Android application model does not change.

NDK 就是用native code (c or c++) 开发 android 程序 ,他提供头文件和c 库,使你可以象SDK java code 那样 处理输入,构建activity 。不过他还是编译成apk 在虚拟机中跑。

Using native code does not result in an automatic performance increase, but always increases application complexity. If you have not run into any limitations using the Android framework APIs, you probably do not need the NDK. Read What is the NDK? for more information about what the NDK offers and whether it will be useful to you.

NDK不一定增加性能,但是却增加程序复杂性,没有特别的理由,官方不建议使用NDK 。

而我们用NDK 的原因是我们为了跨平台比较方便,用 cocos2dx和c++ 语言编写游戏,所以必须要用NDK 。

开发NDK程序 除了SDK 包,还需要 下载NDK 包。

因为android 是基于linux 平台的 ,所以必须在linux 平台下编译程序。 你可以选择在linux 平台下开发 ,或者 用 cygwin 在windows 下开发 。

[编辑]编译NDK 程序

安装cygwin 和NDK 后编译NDK 程序

NDK 有个sample 目录 里面有 不少sample 。

拿最简单的hello-jni 为例。

在这个android 项目中新加了 jni 目录 ,用来存放 c 文件 和 make 文件。

在 cygwin 控制台里面 ,进了这个目录,( cygwin 里面 f盘路径一般是 /cygdrive/f/ ) 运行NDK 根目录下 的 ndk-build


cd <ndk-root>/samples/hello-jni<ndk_root>/ndk-build

系统就会编译文件 ,你会发现hello-jni 项目 生成了一个lib 目录 和相关的so 文件。

本地代码编译后,你就可以象一般android 程序一样,打包运行此程序。



编译 cocos-2dx helloword 的是时候 ,报  local/armeabi/libgnustl_static.a: No such file: Permission denied 


按照网上说法 ,不直接用ndk-builder ,而是设置 

HelloWorld\android下build_native.修改设置 两个参数

NDK_ROOT_LOCAL=/cygdrive/d/android-ndk-r7b

COCOS2DX_ROOT_LOCAL=/cygdrive/g/cocos2d-1.0.1-x-0.11.0

运行 build_native ,但还是报错 ,

再按照网上说法


需要在helloworld中找到Application.mk文件添加如下内容:STLPORT_FORCE_REBUILD := true


也不行 ,

最后还是用 window 修改文件的方法,先copy libgnustl_static.a ,再修改文件权限 ,everyone can  control it 。解决这个问题。


参考 http://www.cnblogs.com/halzhang/archive/2010/08/03/1791405.html

http://blog.csdn.net/andy_quan/article/details/7310632

原创粉丝点击