---
title: "JNI系统程序开发"
author: "mmdev"
date: "2011-12-10T09:26:00Z"
tags: []
slug: "jni"
draft: false
lastmod: "2011-12-10T16:18:45Z"
id: 1301530
url: "https://mmdev.iteye.com/blog/1301530"
---

JNI是Java Native Interface的缩写。从Java 1.1开始，JNI标准成为java平台的一部分，它允许Java和其他语言进行交互。

JNI一开始为C和C++而设计的，但是它并不妨碍你使用其他语言，只要调用约定受支持就可以了。使用java与本地已编译的代码交互，通常会丧失平台可移植性。但是，它可以直接与硬件、操作系统进行交互，也能提高程序的性能。

Android中JNI系统程序和应用程序的开发有些细微的差别。本文章主要是针对Android中JNI系统程序的开发，如果你想进步了解应用程序请阅读《[**JNI应用程序开发**](http://hubingforever.blog.163.com/blog/static/171040579201072743235305/ "阅读全文")》

Jni程序开发的一般操作步骤如下：

(1) 编写带有native声明的方法的java类

(2) 编译java类成class文件，使用javah -jni java类名生成扩展名为h的头文件

(3)使用C++ 实现本地方法

(4)编辑Android.mk

(5)编译JNI的C/C++源码生成.so文件

(6) 在Java中把.so文件载入到java.library.path

(7)把JNI函数注册到系统

(8) 在java中调用native方法.

一 Java中声明JNI函数

在Java声明一个JNI函数很简单，就像在接口中声明函数一样，只是在前面多加个前缀Native.

比如：native String getStringFromJni();

注：对于Java中声明JNI函数，JNI系统程序和应用程序开发都一样

二 编译java类成class文件，使用javah -jni java类名生成扩展名为h的头文件&lt;wbr style="line-height:25px"&gt;&lt;div style="line-height:25px"&gt;&lt;span style="color:#003366; line-height:25px"&gt;如果你需要不同的JNI的C\\C++实现文件的函数能互相调用，那么你就需要为你的JNI的C\\C++端编写扩展名为h的头文件。&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#003366; line-height:25px"&gt;你可以手写头文件，也可以使用javah命令自动生成&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt; &lt;span style="color:#003366; line-height:25px"&gt;关于javah命令的详细内容请参考《&lt;/span&gt;&lt;strong&gt;&lt;a title="阅读全文" target="\_blank" href="http://hubingforever.blog.163.com/blog/static/17104057920115218559186/" style="color:rgb(207,121,28); line-height:25px; text-decoration:none"&gt;javah命令详解&lt;/a&gt;&lt;/strong&gt;&lt;span style="line-height:25px; color:rgb(0,51,102)"&gt;》&lt;/span&gt; &lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#000080; line-height:25px"&gt;对于JNI应用程序，一般不需要不同的JNI的C\\C++实现文件的函数能互相调用。&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#000080; line-height:25px"&gt;但是对JNI系统程序，最好还是让不同的JNI的C\\C++实现文件的函数能互相调用。&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt; &lt;span style="color:#000080; line-height:25px"&gt;如果我们是通过registerNativeMethods()手动把的JNI本地和相应java函数的对应关系注册到系统的,那么本地方法的命名似乎并没有严格的规定，否则&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(0,0,128)"&gt;本地方法的命名就必须严格按照规范来，必须为java\_java类的包名\_java类名\_Java的函数名的形式，这里java类的包名用"\_"代替"."。JNI系统程序一般都是采用前面一种方式，JNI应用程序则通常不用把JNI的C/C++函数和相应java函数的对应关系注册到系统。&lt;/span&gt; &lt;/div&gt; &lt;div style="line-height:25px"&gt; &lt;span style="line-height:25px"&gt;比如&lt;/span&gt;&lt;span style="color:#000080; line-height:25px"&gt;：&lt;/span&gt;Hello.java文件&lt;/div&gt; &lt;div style="line-height:25px"&gt; &lt;div style="line-height:25px"&gt;&lt;span style="font-size:13px; color:#3366ff; line-height:23px"&gt;package com.robin;&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="font-size:13px; color:#3366ff; line-height:23px"&gt;public class Hello{&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="font-size:13px; color:#3366ff; line-height:23px"&gt;&lt;span style="line-height:23px; white-space:pre"&gt;&lt;/span&gt;void sayHello()&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="font-size:13px; color:#3366ff; line-height:23px"&gt;&lt;span style="line-height:23px; white-space:pre"&gt;&lt;/span&gt;{&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="font-size:13px; color:#3366ff; line-height:23px"&gt;&lt;span style="line-height:23px; white-space:pre"&gt;&lt;/span&gt;System.out.println(getHelloStringFromJni());&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="font-size:13px; color:#3366ff; line-height:23px"&gt;&lt;span style="line-height:23px; white-space:pre"&gt;&lt;/span&gt;}&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="font-size:13px; color:#3366ff; line-height:23px"&gt;&lt;span style="line-height:23px; white-space:pre"&gt;&lt;/span&gt;native String getHelloStringFromJni();&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="font-size:13px; color:#3366ff; line-height:23px"&gt;}&lt;/span&gt;&lt;/div&gt; &lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="font-size:13px; line-height:23px"&gt;JNI的本地方法就应该为：&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt; &lt;span style="font-size:13px; color:#0000ff; line-height:23px"&gt;JNIEXPORT jstring JNICALL&lt;/span&gt;&lt;span style="font-size:13px; color:#ff6600; line-height:23px"&gt;Java&lt;/span&gt;&lt;span style="font-size:13px; color:#0000ff; line-height:23px"&gt;\_com\_robin\_Hello\_&lt;/span&gt;&lt;span style="font-size:13px; color:#ff6600; line-height:23px"&gt;getHelloStringFromJni(JNIEnv \*, jobject);&lt;/span&gt; &lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="line-height:25px"&gt;三 使用C/C++ 实现本地方法&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#003366; line-height:25px"&gt;接着可以开始在.c或.cpp文件中实现头文件声明的JNI函数。&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#000080; line-height:25px"&gt;JNI的所有的本地方法的第一个参数都是指向JNIEnv结构的。这个结构是用来调用JNI函数的。第二个参数jclass/jobject的意义，要看方法是不是静态的（static）或者实例（Instance）的。前者，jclass代表一个类对象的引用，而后者是被调用的方法所属对象的引用。 从第三个参数开始的才是java函数本身传递的参数。函数返回值和传入参数的类型都是根据约定映射java类型到本地C/C++类型。我们可以在JNI的C\\C++本地方法对传入的java参数进行操作处理，处理完成可以返回空，也可以返回一个映射到C/C++的java类型。&lt;/span&gt;&lt;/div&gt; &lt;/div&gt; &lt;div style="line-height:25px"&gt;关于JNI中参数的传递与操作的更多内容请参考《&lt;strong&gt;&lt;a title="阅读全文" target="\_blank" href="http://hubingforever.blog.163.com/blog/static/17104057920115712234774/" style="color:rgb(207,121,28); line-height:25px; text-decoration:none"&gt;JNI中参数的传递与操作&lt;/a&gt;&lt;/strong&gt;》&lt;/div&gt; &lt;div style="line-height:25px"&gt; &lt;span style="line-height:25px"&gt;注&lt;/span&gt;：在C/C++中，如果一个函数没有进行声明，那么在定义它时，就同时进行声明了。&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="line-height:25px"&gt;四编辑Android.mk&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt; &lt;div style="line-height:25px; font-weight:bold"&gt;示例1&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#3366ff; line-height:25px"&gt;LOCAL\_PATH := $(call my-dir)&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#3366ff; line-height:25px"&gt;include $(CLEAR\_VARS)&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#3366ff; line-height:25px"&gt;LOCAL\_MODULE := hello-jni&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#3366ff; line-height:25px"&gt;LOCAL\_SRC\_FILES := hello-jni.c&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#3366ff; line-height:25px"&gt;include $(BUILD\_SHARED\_LIBRARY)。&lt;/span&gt;&lt;/div&gt; &lt;/div&gt; &lt;div style="line-height:25px"&gt;关于&lt;span style="line-height:22px"&gt;Android.mk的详细内容请参照《&lt;/span&gt;&lt;strong&gt;&lt;a title="阅读全文" target="\_blank" href="http://hubingforever.blog.163.com/blog/static/171040579201152185542166/" style="color:rgb(207,121,28); line-height:25px; text-decoration:none"&gt;Android.mk&lt;/a&gt;&lt;/strong&gt;》&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="line-height:25px"&gt;五编译JNI的C\\C++源码生成.so文件&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt; &lt;span style="color:#003366; line-height:25px"&gt;对于JNI应用程序，编译JNI的C\\C++源码是使用NDK来进行编译，具体请参照《&lt;/span&gt;&lt;strong&gt;&lt;a title="阅读全文" target="\_blank" href="http://hubingforever.blog.163.com/blog/static/171040579201072743235305/" style="color:rgb(207,121,28); line-height:25px; text-decoration:none"&gt;JNI应用程序开发&lt;/a&gt;&lt;/strong&gt;&lt;span style="line-height:25px; color:rgb(0,51,102)"&gt;》&lt;/span&gt; &lt;/div&gt; &lt;div style="line-height:25px"&gt; &lt;span style="color:#003366; line-height:25px"&gt;对于JNI系统程序，你只要提供了&lt;/span&gt;&lt;span style="color:#0000ff; line-height:25px"&gt;Android.mk&lt;/span&gt;&lt;span style="color:#003366; line-height:25px"&gt;文件，在编译Android系统的时候，该Anroid.mk就会被执行，当然就进行编译并生成.so文件。&lt;/span&gt; &lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#003366; line-height:25px"&gt;一个.so文件就相当于一个JNI组件。&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt; &lt;span style="line-height:25px; color:rgb(0,51,102)"&gt;另外注意，&lt;/span&gt;&lt;span style="color:#0000ff; line-height:25px"&gt;Android.mk&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(0,51,102)"&gt;中通过“&lt;/span&gt;&lt;span style="color:#0000ff; line-height:25px"&gt;LOCAL\_MODULE:= hello-jni&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(0,51,102)"&gt;”指定包名，此处包名为"hello-jni"，生成的.so文件名为lib+包名+.so形成，这里将生成l&lt;/span&gt;&lt;span style="color:#0000ff; line-height:25px"&gt;ibhello-jni.so&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(0,51,102)"&gt;文件&lt;/span&gt; &lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="line-height:25px"&gt;六在Java中把.so文件载入到java.library.path&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt; &lt;div style="line-height:25px"&gt; &lt;span style="color:#003366; line-height:25px"&gt;在java代码中可以通过&lt;/span&gt;&lt;span style="color:#0000ff; line-height:25px"&gt;System.loadLibrary&lt;/span&gt;&lt;span style="color:#003366; line-height:25px"&gt;方法把.so文件载入java.library.path，以便我们使用。&lt;/span&gt; &lt;/div&gt; &lt;div style="line-height:25px"&gt;比如&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#3366ff; line-height:25px"&gt;&lt;span style="line-height:25px; white-space:pre"&gt;&lt;/span&gt;static {&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#3366ff; line-height:25px"&gt; System.loadLibrary("hello-jni");&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#3366ff; line-height:25px"&gt; }&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt; &lt;span style="line-height:25px"&gt;注意&lt;/span&gt;：System.loadLibrary的参数是包名hello-jni，而不文件名libhello-jni.so&lt;/div&gt; &lt;/div&gt; &lt;div style="line-height:25px"&gt; &lt;div style="line-height:25px"&gt; &lt;span style="line-height:25px"&gt;&lt;span style="color:#003366; line-height:25px"&gt;JNI\_OnLoad()&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000080; line-height:25px"&gt;与&lt;/span&gt;&lt;span style="line-height:25px"&gt;&lt;span style="color:#003366; line-height:25px"&gt;JNI\_OnUnload()&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000080; line-height:25px"&gt;函数&lt;/span&gt; &lt;/div&gt; &lt;div style="line-height:25px"&gt; &lt;span style="line-height:25px; color:rgb(0,0,128)"&gt; &lt;/span&gt;&lt;span style="color:#003366; line-height:25px"&gt;当Android的VM(Virtual Machine)执行到&lt;/span&gt;&lt;span style="color:#ff6600; line-height:25px"&gt;System.loadLibrary()&lt;/span&gt;&lt;span style="color:#003366; line-height:25px"&gt;函数时，首先会去执行JNI组件(\*.so)里的&lt;/span&gt;&lt;span style="color:#ff6600; line-height:25px"&gt;JNI\_OnLoad()&lt;/span&gt;&lt;span style="color:#003366; line-height:25px"&gt;函数。&lt;/span&gt; &lt;/div&gt; &lt;div style="line-height:25px"&gt; &lt;span style="color:#003366; line-height:25px"&gt;它的用途有二&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(0,0,128)"&gt;：&lt;/span&gt; &lt;/div&gt; &lt;div style="line-height:25px"&gt; &lt;span style="line-height:25px; color:rgb(0,0,128)"&gt; &lt;/span&gt;&lt;span style="color:#000080; line-height:25px"&gt;1)告诉VM此C组件使用那一个JNI版本。如果你的在\*.so没有提供&lt;/span&gt;&lt;span style="color:#ff6600; line-height:25px"&gt;JNI\_OnLoad()&lt;/span&gt;&lt;span style="color:#000080; line-height:25px"&gt;函数，VM会默认该\*.so档是使用最老的JNI 1.1版本。&lt;/span&gt; &lt;/div&gt; &lt;div style="line-height:25px"&gt; &lt;span style="color:#000080; line-height:25px"&gt; 由于新版的JNI做了许多扩充，如果你需要使用JNI的新版功能（例如JNI 1.4的java.nio.ByteBuffer）,那么JNI\_OnLoad()函数返回JNI\_VERSION\_1\_4或以上。如果你返回&lt;/span&gt;&lt;span style="color:#ff6600; line-height:25px"&gt;JNI\_VERSION\_1\_4&lt;/span&gt;&lt;span style="color:#000080; line-height:25px"&gt;的话，就是告知VM你使用的是&lt;/span&gt;&lt;span style="color:#ff6600; line-height:25px"&gt;JNI\_VERSION\_1\_4&lt;/span&gt;&lt;span style="color:#000080; line-height:25px"&gt;版本的JNI。&lt;/span&gt; &lt;/div&gt; &lt;div style="line-height:25px; color:rgb(0,0,128)"&gt; 2)由于VM执行到System.loadLibrary()函数时，就会立即先呼叫JNI\_OnLoad()， 所以C组件的开发者可以藉由JNI\_OnLoad()来进行组件内的初期值之设定(Initialization) 。&lt;/div&gt; &lt;div style="line-height:25px; color:rgb(0,0,128)"&gt; 例如，在Android的/system/lib/libmedia\_jni.so档案里，就提供了JNI\_OnLoad()函数，其程式码片段为：&lt;/div&gt; &lt;div style="line-height:25px; color:rgb(0,0,128)"&gt;&lt;span style="line-height:25px"&gt;示例1&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#3366ff; line-height:25px"&gt;//#define LOG\_NDEBUG 0&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#3366ff; line-height:25px"&gt;#define LOG\_TAG "MediaPlayer-JNI"&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#0000ff; line-height:25px"&gt;jint JNI\_OnLoad(JavaVM* vm, void* reserved)&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#3366ff; line-height:25px"&gt;{&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#3366ff; line-height:25px"&gt;JNIEnv* env = NULL;&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#3366ff; line-height:25px"&gt;jint result = -1;&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt; &lt;span style="color:#0000ff; line-height:25px"&gt;if (vm-&gt;GetEnv((void\*\*) &amp;env, JNI\_VERSION\_1\_4) != JNI\_OK)&lt;/span&gt;&lt;span style="color:#3366ff; line-height:25px"&gt;{&lt;/span&gt; &lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#3366ff; line-height:25px"&gt;LOGE("ERROR: GetEnv failed\\n");&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#3366ff; line-height:25px"&gt;goto bail;&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#3366ff; line-height:25px"&gt;}&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#3366ff; line-height:25px"&gt;assert(env != NULL);&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt; &lt;span style="color:#3366ff; line-height:25px"&gt;if (register\_android\_media\_MediaPlayer(&lt;/span&gt;&lt;span style="color:#0000ff; line-height:25px"&gt;env&lt;/span&gt;&lt;span style="color:#3366ff; line-height:25px"&gt;) &lt; 0) {&lt;/span&gt; &lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#3366ff; line-height:25px"&gt;LOGE("ERROR: MediaPlayer native registration failed\\n");&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#3366ff; line-height:25px"&gt;goto bail;&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#3366ff; line-height:25px"&gt;}&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt; &lt;span style="color:#3366ff; line-height:25px"&gt;if (register\_android\_media\_MediaRecorder(&lt;/span&gt;&lt;span style="color:#0000ff; line-height:25px"&gt;env)&lt;/span&gt;&lt;span style="color:#3366ff; line-height:25px"&gt;&lt; 0) {&lt;/span&gt; &lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#3366ff; line-height:25px"&gt;LOGE("ERROR: MediaRecorder native registration failed\\n");&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#3366ff; line-height:25px"&gt;goto bail;&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#3366ff; line-height:25px"&gt;}&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt; &lt;span style="color:#3366ff; line-height:25px"&gt;if (register\_android\_media\_MediaScanner(&lt;/span&gt;&lt;span style="color:#0000ff; line-height:25px"&gt;env&lt;/span&gt;&lt;span style="color:#3366ff; line-height:25px"&gt;) &lt; 0) {&lt;/span&gt; &lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#3366ff; line-height:25px"&gt;LOGE("ERROR: MediaScanner native registration failed\\n");&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#3366ff; line-height:25px"&gt;goto bail;&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#3366ff; line-height:25px"&gt;}&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt; &lt;span style="color:#3366ff; line-height:25px"&gt;if (register\_android\_media\_MediaMetadataRetriever(&lt;/span&gt;&lt;span style="color:#0000ff; line-height:25px"&gt;env&lt;/span&gt;&lt;span style="color:#3366ff; line-height:25px"&gt;) &lt; 0) {&lt;/span&gt; &lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#3366ff; line-height:25px"&gt;LOGE("ERROR: MediaMetadataRetriever native registration failed\\n");&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#3366ff; line-height:25px"&gt;goto bail;&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#3366ff; line-height:25px"&gt;}&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#3366ff; line-height:25px"&gt;/* success -- return valid version number \*/&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt; &lt;span style="color:#0000ff; line-height:25px"&gt;result = JNI\_VERSION\_1\_4&lt;/span&gt;&lt;span style="color:#3366ff; line-height:25px"&gt;;&lt;/span&gt; &lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#3366ff; line-height:25px"&gt;bail:&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#3366ff; line-height:25px"&gt;return result;&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#3366ff; line-height:25px"&gt;}&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#000080; line-height:25px"&gt;此函数回传JNI\_VERSION\_1\_4值给VM，于是VM知道了其所使用的JNI版本了。此外，它也做了一些初期的动作(该函数可呼叫任何本地函数)，例如指令：&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#0000ff; line-height:25px"&gt;if (register\_android\_media\_MediaPlayer(env) &lt; 0) {&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#0000ff; line-height:25px"&gt;LOGE("ERROR: MediaPlayer native registration failed\\n");&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#0000ff; line-height:25px"&gt;goto bail;&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#0000ff; line-height:25px"&gt;}&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;这里&lt;span style="line-height:25px; color:rgb(0,0,255)"&gt;register\_android\_media\_MediaPlayer(env)&lt;/span&gt;将此组件提供的各个本地函数(Native Function)登记到VM里，以便能加快后续呼叫本地函数的效率。关于JNI本地函数的登记的详细内容请参考后文。&lt;/div&gt; &lt;div style="line-height:25px"&gt; &lt;span style="color:#003366; line-height:25px"&gt;当VM释放JNI组件时，则会调用&lt;/span&gt;&lt;span style="color:#ff6600; line-height:25px"&gt;JNI\_OnUnload()&lt;/span&gt;&lt;span style="color:#003366; line-height:25px"&gt;函数，我们可以在此添加一些善后清除的代码。&lt;/span&gt; &lt;/div&gt; &lt;div style="line-height:25px"&gt; &lt;span style="color:#000080; line-height:25px"&gt;当VM呼叫&lt;/span&gt;&lt;span style="color:#ff6600; line-height:25px"&gt;JNI\_OnLoad()&lt;/span&gt;&lt;span style="color:#000080; line-height:25px"&gt;或&lt;/span&gt;&lt;span style="color:#ff6600; line-height:25px"&gt;JNI\_Unload()&lt;/span&gt;&lt;span style="color:#000080; line-height:25px"&gt;函数时，都会将VM的指针(Pointer)传递给它们，其参数如下：&lt;/span&gt; &lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#0000ff; line-height:25px"&gt;jint JNI\_OnLoad(JavaVM* vm, void* reserved) { }&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#0000ff; line-height:25px"&gt;jint JNI\_OnUnload(JavaVM* vm, void* reserved){ }&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#000080; line-height:25px"&gt;在JNI\_OnLoad()函数里，就必须透过VM来取得JNIEnv，并存入env变量里，如下述指令：&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#3366ff; line-height:25px"&gt;jint JNI\_OnLoad(JavaVM* vm, void* reserved){&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#3366ff; line-height:25px"&gt;JNIEnv* env = NULL;&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#3366ff; line-height:25px"&gt;jint result = -1;&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#3366ff; line-height:25px"&gt;if (vm-&gt;GetEnv((void\*\*) &amp;env, JNI\_VERSION\_1\_4) != JNI\_OK) {&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#3366ff; line-height:25px"&gt;LOGE("ERROR: GetEnv failed\\n");&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#3366ff; line-height:25px"&gt;goto bail;&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#3366ff; line-height:25px"&gt;}&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#3366ff; line-height:25px"&gt;}&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#000080; line-height:25px"&gt;由于VM通常是多线程(Multi-threading)的执行环境。每一个线程在呼叫JNI\_OnLoad()时，所传递进来的JNIEnv指标值都是不同的。&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#000080; line-height:25px"&gt;为了配合这种多执行绪的环境，JNI组件开发者在撰写本地函数时，也就可因JNIEnv不同而避免线程冲突问题，&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt; &lt;span style="color:#000080; line-height:25px"&gt;这样就能确保所写的本地函数能安全地在Android的多线程的VM里安全地执行。基于这个理由，当在呼叫JNI组件的本地函数时，都会将JNIEnv指标值传递给它。如&lt;/span&gt;&lt;span style="line-height:25px"&gt;示例1,&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(0,0,128)"&gt;调用register\_android\_media\_MediaPlayer(env)函数时，就将env指标值传递过去。如此，在register\_android\_media\_MediaPlayer()函数就能根据JNIEnv的不同而避免线程的冲突。&lt;/span&gt; &lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#000080; line-height:25px"&gt;可以撰写下述指令：&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#3366ff; line-height:25px"&gt;if ((\*env)-&gt;MonitorEnter(env, obj) == JNI\_OK) {&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#3366ff; line-height:25px"&gt;//代码1&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt; &lt;span style="color:#3366ff; line-height:25px"&gt;}&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(51,102,255)"&gt;else {&lt;/span&gt; &lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#3366ff; line-height:25px"&gt;//代码2&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#3366ff; line-height:25px"&gt;}&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt; &lt;span style="color:#000080; line-height:25px"&gt;该段代码用于查看是否已经有JNIEnv进入此obj，如果没有，此JNIEnv就进入该obj并返回JNI\_OK，然后执行&lt;/span&gt;&lt;span style="color:#0000ff; line-height:25px"&gt;代码1&lt;/span&gt;&lt;span style="color:#000080; line-height:25px"&gt;；否则执行&lt;/span&gt;&lt;span style="color:#0000ff; line-height:25px"&gt;代码2.&lt;/span&gt; &lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#000080; line-height:25px"&gt;还有，也可撰写下述指令：&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#3366ff; line-height:25px"&gt;if ((\*env)-&gt;MonitorExit(env, obj) == JNI\_OK) {&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#3366ff; line-height:25px"&gt;//代码1&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt; &lt;span style="color:#3366ff; line-height:25px"&gt;}&lt;/span&gt;&lt;span style="line-height:25px; color:rgb(51,102,255)"&gt;else&lt;/span&gt; &lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#3366ff; line-height:25px"&gt;{&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#3366ff; line-height:25px"&gt;//代码2&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#3366ff; line-height:25px"&gt;}&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt; &lt;span style="color:#000080; line-height:25px"&gt;该段代码用于查看该JNIEnv是否正在此obj，如果是该JNIEnv就离开此obj并返回JNI\_OK，执行&lt;/span&gt;&lt;span style="color:#0000ff; line-height:25px"&gt;代码1&lt;/span&gt;&lt;span style="color:#000080; line-height:25px"&gt;，否则执行&lt;/span&gt;&lt;span style="color:#0000ff; line-height:25px"&gt;代码2&lt;/span&gt;&lt;span style="color:#000080; line-height:25px"&gt;。&lt;/span&gt; &lt;/div&gt; &lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#000080; line-height:25px"&gt;另外注意对于本步骤，JNI应用程序开发和JNI系统程序开发没有什么不同。&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="line-height:25px"&gt;七 把JNI函数注册到系统&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt; &lt;span style="color:#003366; line-height:25px"&gt; 应用层级的Java类别通过VM而调用到本地函数，VM默认是按照JNI的java函数和C\\C++本地函数的命名规范，去寻找\*.so里的本地函数。查找的过程还是有点耗时&lt;/span&gt; &lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#003366; line-height:25px"&gt; 如果调用次数很多，而每次都寻找一遍的话，会花掉许多时间。&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#003366; line-height:25px"&gt;如果你想节约VM查找JNI本地函数的时间，你可以通过registerNativeMethods()函数把JNI本地函数向VM进行登记，以节约VM查找JNI本地函数的时间。&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#003366; line-height:25px"&gt;如果你的JNI本地函数的命名没严格遵守java函数和C\\C++本地函数对应的命名规范，你必须通过registerNativeMethods()函数把JNI本地函数向VM进行登记，以便VM能找到你的JNI本地函数&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#003366; line-height:25px"&gt;例如，在Android的/system/lib/libmedia\_jni.so档案里的代码段如下：&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="font-size:13px; color:#3366ff; line-height:23px"&gt;//#define LOG\_NDEBUG 0&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="font-size:13px; color:#3366ff; line-height:23px"&gt;#define LOG\_TAG "MediaPlayer-JNI"&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt; &lt;span style="font-size:13px; color:#0000ff; line-height:23px"&gt;static JNINativeMethod gMethods\[]&lt;/span&gt;&lt;span style="font-size:13px; color:#3366ff; line-height:23px"&gt;= {&lt;/span&gt; &lt;/div&gt; &lt;div style="line-height:25px"&gt; &lt;span style="font-size:13px; color:#3366ff; line-height:23px"&gt;{"&lt;/span&gt;&lt;span style="font-size:13px; color:#99cc00; line-height:23px"&gt;setDataSource&lt;/span&gt;&lt;span style="font-size:13px; color:#3366ff; line-height:23px"&gt;", "&lt;/span&gt;&lt;span style="font-size:13px; color:#ff6600; line-height:23px"&gt;(Ljava/lang/String;)V&lt;/span&gt;&lt;span style="font-size:13px; color:#3366ff; line-height:23px"&gt;",&lt;/span&gt; &lt;/div&gt; &lt;div style="line-height:25px"&gt; &lt;span style="font-size:13px; color:#99cc00; line-height:23px"&gt;(void \*)android\_media\_MediaPlayer\_setDataSource&lt;/span&gt;&lt;span style="font-size:13px; color:#3366ff; line-height:23px"&gt;},&lt;/span&gt; &lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="font-size:13px; color:#3366ff; line-height:23px"&gt;{"setDataSource", "(Ljava/io/FileDescriptor;JJ)V",&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="font-size:13px; color:#3366ff; line-height:23px"&gt;(void \*)android\_media\_MediaPlayer\_setDataSourceFD},&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="font-size:13px; color:#3366ff; line-height:23px"&gt;{"prepare", "()V", (void \*)android\_media\_MediaPlayer\_prepare},&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="font-size:13px; color:#3366ff; line-height:23px"&gt;{"prepareAsync", "()V", (void \*)android\_media\_MediaPlayer\_prepareAsync},&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="font-size:13px; color:#3366ff; line-height:23px"&gt;{"\_start", "()V", (void \*)android\_media\_MediaPlayer\_start},&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="font-size:13px; color:#3366ff; line-height:23px"&gt;{"\_stop", "()V", (void \*)android\_media\_MediaPlayer\_stop},&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="font-size:13px; color:#3366ff; line-height:23px"&gt;{"getVideoWidth", "()I", (void \*)android\_media\_MediaPlayer\_getVideoWidth},&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="font-size:13px; color:#3366ff; line-height:23px"&gt;{"getVideoHeight", "()I", (void \*)android\_media\_MediaPlayer\_getVideoHeight},&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="font-size:13px; color:#3366ff; line-height:23px"&gt;{"seekTo", "(I)V", (void \*)android\_media\_MediaPlayer\_seekTo},&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="font-size:13px; color:#3366ff; line-height:23px"&gt;{"\_pause", "()V", (void \*)android\_media\_MediaPlayer\_pause},&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="font-size:13px; color:#3366ff; line-height:23px"&gt;{"isPlaying", "()Z", (void \*)android\_media\_MediaPlayer\_isPlaying},&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="font-size:13px; color:#3366ff; line-height:23px"&gt;{"getCurrentPosition", "()I", (void \*)android\_media\_MediaPlayer\_getCurrentPosition},&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="font-size:13px; color:#3366ff; line-height:23px"&gt;{"getDuration", "()I", (void \*)android\_media\_MediaPlayer\_getDuration},&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="font-size:13px; color:#3366ff; line-height:23px"&gt;{"\_release", "()V", (void \*)android\_media\_MediaPlayer\_release},&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="font-size:13px; color:#3366ff; line-height:23px"&gt;{"\_reset", "()V", (void \*)android\_media\_MediaPlayer\_reset},&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="font-size:13px; color:#3366ff; line-height:23px"&gt;{"setAudioStreamType","(I)V", (void \*)android\_media\_MediaPlayer\_setAudioStreamType},&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="font-size:13px; color:#3366ff; line-height:23px"&gt;{"setLooping", "(Z)V", (void \*)android\_media\_MediaPlayer\_setLooping},&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="font-size:13px; color:#3366ff; line-height:23px"&gt;{"setVolume", "(FF)V", (void \*)android\_media\_MediaPlayer\_setVolume},&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="font-size:13px; color:#3366ff; line-height:23px"&gt;{"getFrameAt", "(I)Landroid/graphics/Bitmap;",&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="font-size:13px; color:#3366ff; line-height:23px"&gt;(void \*)android\_media\_MediaPlayer\_getFrameAt},&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="font-size:13px; color:#3366ff; line-height:23px"&gt;{"native\_setup", "(Ljava/lang/Object;)V",&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="font-size:13px; color:#3366ff; line-height:23px"&gt;(void \*)android\_media\_MediaPlayer\_native\_setup},&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="font-size:13px; color:#3366ff; line-height:23px"&gt;{"native\_finalize", "()V", (void \*)android\_media\_MediaPlayer\_native\_finalize},&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="font-size:13px; color:#3366ff; line-height:23px"&gt;};&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt; &lt;span style="font-size:13px; color:#3366ff; line-height:23px"&gt;&lt;/span&gt;&lt;span style="font-size:13px; color:#ff6600; line-height:23px"&gt;static int&lt;/span&gt;&lt;span style="font-size:13px; color:#ff00ff; line-height:23px"&gt;register\_android\_media\_MediaPlayer&lt;/span&gt;&lt;span style="font-size:13px; color:#ff6600; line-height:23px"&gt;(JNIEnv \*env)&lt;/span&gt;&lt;span style="font-size:13px; color:#3366ff; line-height:23px"&gt;{&lt;/span&gt; &lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="font-size:13px; color:#0000ff; line-height:23px"&gt;return AndroidRuntime::registerNativeMethods(env,&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="font-size:13px; color:#0000ff; line-height:23px"&gt;"android/media/MediaPlayer", gMethods, NELEM(gMethods));&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="font-size:13px; color:#3366ff; line-height:23px"&gt;}&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt; &lt;span style="font-size:13px; color:#3366ff; line-height:23px"&gt;&lt;/span&gt;&lt;span style="font-size:13px; color:#ff6600; line-height:23px"&gt;jint&lt;/span&gt;&lt;span style="font-size:13px; color:#ff00ff; line-height:23px"&gt;JNI\_OnLoad&lt;/span&gt;&lt;span style="font-size:13px; color:#ff6600; line-height:23px"&gt;(JavaVM* vm, void* reserved){&lt;/span&gt; &lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="font-size:13px; color:#3366ff; line-height:23px"&gt;//省略&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt; &lt;span style="font-size:13px; color:#3366ff; line-height:23px"&gt;if (&lt;/span&gt;&lt;span style="font-size:13px; color:#0000ff; line-height:23px"&gt;register\_android\_media\_MediaPlayer(env) &lt; 0&lt;/span&gt;&lt;span style="font-size:13px; color:#3366ff; line-height:23px"&gt;) {&lt;/span&gt; &lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="font-size:13px; color:#3366ff; line-height:23px"&gt;LOGE("ERROR: MediaPlayer native registration failed\\n");&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="font-size:13px; color:#3366ff; line-height:23px"&gt;goto bail;&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="font-size:13px; color:#3366ff; line-height:23px"&gt;}&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="font-size:13px; color:#3366ff; line-height:23px"&gt;//省略&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="font-size:13px; color:#3366ff; line-height:23px"&gt;}&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt; &lt;span style="color:#000080; line-height:25px"&gt;当VM载入libmedia\_jni.so档案时，就调用&lt;/span&gt;&lt;span style="color:#0000ff; line-height:25px"&gt;JNI\_OnLoad()&lt;/span&gt;&lt;span style="color:#000080; line-height:25px"&gt;函数。接着，在&lt;/span&gt;&lt;span style="color:#0000ff; line-height:25px"&gt;JNI\_OnLoad()&lt;/span&gt;&lt;span style="color:#000080; line-height:25px"&gt;中调用&lt;/span&gt;&lt;span style="color:#0000ff; line-height:25px"&gt;register\_android\_media\_MediaPlayer()&lt;/span&gt;&lt;span style="color:#000080; line-height:25px"&gt;函数&lt;/span&gt;。&lt;span style="color:#000080; line-height:25px"&gt;然后，在&lt;/span&gt;&lt;span style="color:#0000ff; line-height:25px"&gt;register\_android\_media\_MediaPlayer()&lt;/span&gt;&lt;span style="color:#000080; line-height:25px"&gt;函数中调用&lt;/span&gt;&lt;span style="color:#0000ff; line-height:25px"&gt;AndroidRuntime::registerNativeMethods()&lt;/span&gt;函数，&lt;/div&gt; &lt;div style="line-height:25px"&gt; &lt;span style="color:#000080; line-height:25px"&gt;向VM(即AndroidRuntime)登记&lt;/span&gt;&lt;span style="color:#0000ff; line-height:25px"&gt;gMethods\[]&lt;/span&gt;&lt;span style="color:#000080; line-height:25px"&gt;数组所含的本地函数&lt;/span&gt;。&lt;/div&gt; &lt;div style="line-height:25px"&gt; &lt;span style="color:#003366; line-height:25px"&gt;简而言之，registerNativeMethods()函数的用途有二&lt;/span&gt;：&lt;/div&gt; &lt;div style="line-height:25px"&gt; &lt;span style="line-height:25px"&gt;1)&lt;/span&gt;&lt;span style="color:#000080; line-height:25px"&gt;更有效率去找到函数&lt;/span&gt;。&lt;/div&gt; &lt;div style="line-height:25px"&gt; &lt;span style="line-height:25px"&gt;2)&lt;span style="color:#000080; line-height:25px"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#000080; line-height:25px"&gt;可在执行期间进行抽换。由于&lt;/span&gt;&lt;span style="color:#0000ff; line-height:25px"&gt;gMethods\[]&lt;/span&gt;&lt;span style="color:#000080; line-height:25px"&gt;是一个关于JNI的&lt;java函数，本地函数指针&gt;对照表，在程序执行时，可多次呼叫&lt;/span&gt;&lt;span style="color:#0000ff; line-height:25px"&gt;registerNativeMethods()&lt;/span&gt;&lt;span style="color:#000080; line-height:25px"&gt;函数来更换本地函数之指针，而达到弹性抽换本地函数之目的。&lt;/span&gt; &lt;/div&gt; &lt;div style="line-height:25px"&gt; &lt;span style="color:#003366; line-height:25px"&gt;Andoird 中使用了一种不同传统Java JNI的方式来定义其native的函数。其中很重要的区别是Andorid使用了一种Java 和 C 函数的映射表数组，并在其中描述了函数的参数和返回值。这个数组的类型是&lt;/span&gt;&lt;span style="color:#ff6600; line-height:25px"&gt;JNINativeMethod&lt;/span&gt;&lt;span style="color:#003366; line-height:25px"&gt;，定义如下：&lt;/span&gt; &lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#0000ff; line-height:25px"&gt;typedef struct {&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt; &lt;span style="color:#0000ff; line-height:25px"&gt;const char* name;&lt;/span&gt;&lt;span style="color:#ff6600; line-height:25px"&gt;/\*Java中函数的名字\*/&lt;/span&gt; &lt;/div&gt; &lt;div style="line-height:25px"&gt; &lt;span style="color:#0000ff; line-height:25px"&gt;const char* signature;&lt;/span&gt;&lt;span style="color:#ff6600; line-height:25px"&gt;/\*用于描述函数的参数和返回值的字符\*/&lt;/span&gt; &lt;/div&gt; &lt;div style="line-height:25px"&gt; &lt;span style="color:#0000ff; line-height:25px"&gt;void* fnPtr;&lt;/span&gt;&lt;span style="color:#ff6600; line-height:25px"&gt;/\*本地函数指针，指向C函数\*/&lt;/span&gt; &lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#0000ff; line-height:25px"&gt;} JNINativeMethod;&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="color:#000080; line-height:25px"&gt;其中比较难以理解的是第二个参数，例如 "()V" ,"(II)V", "(Ljava/lang/String;Ljava/lang/String;)V". 实际上这些字符是与函数的参数类型一一对应的。"()" 中的字符表示参数，后面的则代表返回值。例如"()V" 就表示void Func(); "(II)V" 表示 void Func(int, int);&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt;关于此的详细内容请参照《&lt;strong&gt;&lt;a title="阅读全文" target="\_blank" href="http://hubingforever.blog.163.com/blog/static/171040579201151722833782/" style="color:rgb(207,121,28); line-height:25px; text-decoration:none"&gt;JNI中java类型的简写&lt;/a&gt;&lt;/strong&gt;》&lt;/div&gt; &lt;div style="line-height:25px"&gt;&lt;span style="line-height:25px; color:rgb(0,0,128); font-family:Arial,Helvetica,simsun,u5b8bu4f53"&gt;另外还有需要指出的是对于本步骤，JNI应用程序开发和JNI系统程序开发没有什么不同。&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px"&gt; &lt;span style="line-height:25px; font-family:Arial,Helvetica,simsun,u5b8bu4f53"&gt;&lt;/span&gt; &lt;div style="line-height:25px"&gt;&lt;span style="line-height:25px"&gt;八、在java中调用native方法.&lt;/span&gt;&lt;/div&gt; &lt;div style="line-height:25px; color:rgb(0,0,128)"&gt;无论是对于JNI应用程序开发，还是JNI系统程序开发，你可以在java中像调用普遍的java方法一样调用native方法&lt;/div&gt; &lt;/div&gt; &lt;/wbr&gt;