原文链接:http://developer.android.com/training/articles/perf-jni.html
JNI is the Java Native Interface.
JNI是java本地接口.
It defines a way for managed code (written in the Java programming language) to interact with native code (written in C/C++).
它定义一种方式去管理代码(以java语言编写的代码)与本地代码交互(用c/c++写的代码)
It's vendor-neutral,has support for loading code from dynamic shared libraries, and while cumbersome at times is reasonably efficient.
(vendor-neutral不是太明白什么意思,按照字面意思是"中立的供应商"),支持从动态分享库中读取代码.虽然有些时候麻烦,但是是合理有效的.
If you're not already familiar with it,
如果你还没有熟悉它,
read through the Java Native Interface Specification to get a sense for how JNI works and what features are available.
通过阅读java本地接口说明去了解jni如何工作和有什么可用的特性.
Some aspects of the interface aren't immediately obvious on first reading, so you may find the next few sections handy.
第一次阅读接口的一些方面不明显,不过你也许能在接下来的章节方便的获得(意思说下面章节会更加明显的表述jni接口的意思).
JavaVM and JNIEnv
JNI defines two key data structures, "JavaVM" and "JNIEnv".
JNI定义了2个关键的数据结构,javaVM和JNIEnv.
Both of these are essentially pointers to pointers to function tables.
这2种数据结构本质上是指向函数表的指针.
(In the C++ version, they're classes with a pointer to a function table and a member function for each JNI function that indirects through the table.)
(在C++版本中,它们是指向函数表的类然后一个成员函数间接地通过这张表对应每个JNI函数.)
The JavaVM provides the "invocation interface" functions,
JavaVM提供接口调用函数,
which allow you to create and destroy a JavaVM. In theory you can have multiple JavaVMs per process, but Android only allows one.
它允许你去创建和销毁一个javaVM,在理论上每个进程你可以有多个javaVM,但是Android只允许你有一个.
The JNIEnv provides most of the JNI functions. Your native functions all receive a JNIEnv as the first argument.
JNIEnv提供大多数的JNI函数,你的所有本地方法接收一个JNIEnv作为第一个参数.
The JNIEnv is used for thread-local storage. For this reason, you cannot share a JNIEnv between threads.
JNIEnv被用来给本地线程存储,因此,你不能在线程之间分享一个JNIEnv.
If a piece of code has no other way to get its JNIEnv,
如果一段代码没有其它方式获得它的JNIEnv,
you should share the JavaVM, and use GetEnv
to discover the thread's JNIEnv. (Assuming it has one; see AttachCurrentThread
below.)
你应该共享javaVM,然后使用GetEnv去发现这个线程JNIEnv(如果它有一个,看下面的AttachCurrentThread).
The C declarations of JNIEnv and JavaVM are different from the C++ declarations.
JNIEnv和JavaVM的C语言声明不同于C++的声明.
The "jni.h"
include file provides different typedefs depending on whether it's included into C or C++.
jni.h头文件提供不同的类型定义依靠它是否包含进了(c或c++),
For this reason it's a bad idea to include JNIEnv arguments in header files included by both languages.
由于这个原因,它是
(Put another way: if your header file requires #ifdef __cplusplus
, you may have to do some extra work if anything in that header refers to JNIEnv.)