基于 Android NDK 的学习之旅-----序言

做了个Android项目, 引擎层 用C的, 准备写这个系类的文章,借此跟朋友来分享下我NDK开放的经验以及自己知识的总结和备忘。希望能给需要这方面资料的朋友提供一定的帮助。

主要涉及到:

环境的搭建

Android.mk 文件的 配置

HelloWorld

NDK 打印信息

JNI 数据类型

Java 调用 C,C 调用 Java:

Java---C 之间的 数据的传输(基本数据类型的传输,String的传输,对象的传输,数组的传输等等)

在C中新建Java对象

Java方法在C中的映射(包括了签名的使用)

C中如何调用Java的方法(包括了静态的和非静态的,以及参数的传递和返回值的处理)

JNI中资源的释放 等等

先详细介绍下NDK, JNI:

What isthe NDK?

      Google Say:

The Android NDK is a toolset that lets youembed components that make use of native code in your Android applications.

Android applications run in the Dalvikvirtual machine. The NDK allows you to implement parts of your applicationsusing native-code languages such as C and C++. This can provide benefits to certain classes of applications, in the form of reuse ofexisting code and in some cases increased speed.

【Android NDK是一个工具集合,让你嵌入组件,利用原生代码在你的Android Application。

Android应用在虚拟机上执行.NDK允许你的应用通过原生代码如C和C++去实现.这将给某些应用带来好处,这种方式能代码重用,并且在某些情况下提高执行速度】(个人翻译,翻译不准确的请指出)

NDK全称:Native Development Kit。

Android NDK 是配合 Android SDK 的工具,Google 推出NDK的目的不是为了取代Android SDK,当然也不可能完全取代,它只是作为AndroidSDK  的一个补充。用来编译应用的原生代码。

 1、NDK是一系列工具的集合。

* NDK提供了一系列的工具,帮助开发者快速开发C(或C++)的动态库,并能自动将so和java应用一起打包成apk。这些工具对开发者的帮助是巨大的。

* NDK集成了交叉编译器,并提供了相应的mk文件隔离CPU、平台、ABI等差异,开发人员只需要简单修改mk文件(指出“哪些文件需要编译”、“编译特性要求”等),就可以创建出so。

* NDK可以自动地将so和Java应用一起打包,极大地减轻了开发人员的打包工作。

2、NDK提供了一份稳定、功能有限的API头文件声明。

Google明确声明该API是稳定的,在后续所有版本中都稳定支持当前发布的API。从该版本的NDK中看出,这些API支持的功能非常有限,包含有:C标准库(libc)、标准数学库(libm)、压缩库(libz)、Log库(liblog)。

What is the JNI?

      Sun say:

THE Java NativeInterface (JNI) is a powerful feature of the Java platform.

Applications that usethe JNI can incorporate native code written in programming languages such as Cand C++, as well as code written in the Java programming language. The JNIallows programmers to take advantage of the power of the Java platform, withouthaving to abandon their investments in legacy code. Because the JNI is a partof the Java platform, programmers can address interoperability issues once, andexpect their solution to work with all implementations of the Java platform.

JNI是Java Native Interface的缩写,中文为JAVA本地调用。从Java1.1开始,JavaNative Interface(JNI)标准成为java平台的一部分,它允许Java代码和其他语言写的代码进行交互。JNI一开始是为了本地已编译语言,尤其是C和C++而设计的,但是它并不妨碍你使用其他语言,只要调用约定受支持就可以了。

网络上相关的介绍资料太多了,我就不啰嗦了。

常用文档下载:

具体下载目录在 /2011年资料/Android入门教程/基于 Android NDK 的学习之旅-----序言/

相关推荐