JNIEnv Analysis of Android JNI

Source: Internet
Author: User
Tags truncated

.


jni.h file : Understanding JNI needs to work with jni.h files, jni.h is a file in the Google NDK, the location is $/android-ndk-r9d/platforms/android-19/ar Ch-arm/usr/include/jni.h ;


1. JNIEnv effect


jnienv Concept : is a thread-related structure, which represents the Java operating environment in this thread;


jnienv and JAVAVM : Pay attention to distinguish these two concepts;

-- JAVAVM : JAVAVM is the representative of the Java Virtual machine in the JNI layer, there is only one jni globally;

-- jnienv : JAVAVM in-line, each thread has one, there may be a lot of jnienv in JNI;


jnienv effect :

-- call Java function : jnienv represents the Java Runtime environment, you can use JNIEnv to invoke code in Java;

-- manipulating Java objects: the Java object passed into the JNI layer is the Jobject object and needs to use JNIENV to manipulate the Java object;


2. Creation and release of JNIEnv


jnienv Create and release : obtained from JAVAVM: Here is the code for the JAVAVM struct,

Source in C : Jniinvokeinterface is a JAVAVM struct in the C locale, called (*attachcurrentthread) (javavm*, jnienv**, void*) method, Can obtain the JNIENV structure body;

-- C + + source : _JAVAVM is the JAVAVM struct in C + +, call Jint attachcurrentthread (jnienv** p_env, void* Thr_args) method, you can get JN Ienv structural body;

-- release in C : Call the (*detachcurrentthread) (javavm*) method in the JAVAVM struct (jniinvokeinterface) to release the jnienv in this thread;

-- release in C + + : Call the Jint Detachcurrentthread () {return functions->detachcurrent in the JAVAVM struct (_javavm) Thread (this); } method, you can release the jnienv in this thread;

/* * JNI invocation interface.    */struct jniinvokeinterface {void* reserved0;    void* reserved1;    void* Reserved2; Jint (*DESTROYJAVAVM) (javavm*);/* Create JNIENV, each thread creates a */Jint (*attachcurrentthread) (javavm*, jnienv**,    void*);/* Release jnienv */Jint (*detachcurrentthread) (javavm*) for this thread;    Jint (*getenv) (javavm*, void**, Jint); Jint (*attachcurrentthreadasdaemon) (javavm*, jnienv**, void*);};/ * * C + + version. */struct _JAVAVM {const struct jniinvokeinterface* functions; #if defined (__cplusplus) jint DESTROYJAVAVM () {RET Urn FUNCTIONS->DESTROYJAVAVM (this);  }/* create JNIENV, each thread creates a method that calls the C language structure, C is the same as the C + + method */Jint Attachcurrentthread (jnienv** p_env, void* Thr_args) { Return Functions->attachcurrentthread (this, p_env, Thr_args); }/* releases the jnienv of this thread, calls the C-language structure of the method, C is the same as the C + + method */Jint Detachcurrentthread () {return FUNCTIONS->DETACHCURRENTTH Read (this); } jint GetEnv (void** env, Jint verSion) {return functions->getenv (this, env, version);} Jint Attachcurrentthreadasdaemon (jnienv** p_env, void* Thr_args) {return Functions->attachcurrentthreadasdaemon ( This, p_env, Thr_args); } #endif/*__cplusplus*/};


3. JNIEnv Architecture


Thread-related : jnienv is thread-dependent, that is, there is a jnienv pointer in each thread, each jnienv is thread-specific, other threads cannot use jnienv in this thread, and thread A cannot invoke thread B's jnienv;


jnienv cannot span threads :

--The current thread is valid : JNIEnv only in the current thread is valid, jnienv cannot be passed between threads, in the same thread, multiple calls to the JNI layer method, the incoming jnienv is the same;

- local methods match multiple jnienv : Local methods defined in the Java layer can be called on different threads, so different jnienv can be accepted;


jnienv structure : From the above code can be drawn, jnienv is a pointer to a thread-related structure, thread-related structure to the JNI function pointer array, the array contains a large number of JNI function pointers, which point to the specific JNI function





4. Analyze JNIEnv related code


jnienv definition of the relevant code :

/* Declare the struct so that you can use the */struct _jnienv;struct _javavm;/* to declare the jnienv in the C locale as the c_jnienv pointer, pointing to the jninativeinterface struct */typed EF const struct jninativeinterface* c_jnienv; #if defined (__cplusplus)/* C + + environment, jnienv is struct */typedef _jnienv jnienv;type def _JAVAVM JAVAVM; #else/* C language Environment, JNIENV is the pointer */typedef const struct jninativeinterface* jnienv;typedef const struct JNIINV okeinterface* JAVAVM; #endif

-- Jninativeinterface structure : This struct defines a number of function pointers that point to functions related to Java-related variables, and in the case of a C locale, jnienv is a pointer to the struct;

-- _JNIENV structure : The jnienv in the C + + environment is the struct, which encapsulates a jninativeinterface struct pointer, that is, the jnienv in C + + is more than the C language, and and fully compatible with the jnienv in C language;

-- _JAVAVM structure : The structure is the representative of the Java virtual machine in JNI, there is only one virtual machine mapping in the whole JNI layer;


jninativeinterface Source code (truncated) : omitted, wherein the definition of Java-related methods, are pointers to corresponding functions of the function pointer;

-- analytic jnienv* : typedef const struct JNINATIVEINTERFACE* jnienv in c locale, jnienv* env is equivalent to c8>jninativeinterface** env1 (pointer to struct address), to obtain a function pointer defined in the JNINATIVEINTERFACE structure based on a level two pointer env1, first obtain a pointer to the struct body, The method is (*ENV1), so call the function pointer in the method to point to this: (*ENV1)->findclass (jnienv*, const char*);

/* Table of interface function pointers. */struct jninativeinterface {    void*       reserved0;    void*       reserved1;    void*       Reserved2;    void*       reserved3;    Jint        (*getversion) (jnienv *);    ... ...    Jobject     (*newdirectbytebuffer) (jnienv*, void*, Jlong);    void*       (*getdirectbufferaddress) (jnienv*, jobject);    Jlong       (*getdirectbuffercapacity) (jnienv*, jobject);    /* Added in JNI 1.6 *    /Jobjectreftype (*getobjectreftype) (jnienv*, jobject);};


_jnienv Source code (truncated) : There is a JNINATIVEINTERFACE structure pointer in the source code , which shows that the jnienv of C + + environment is extended under the jnienv of C language environment;

-- parse jnienv* : definition is such a typedef _jnienv jnienv, jnienv* env is equivalent to _jnienv* env1, so call _jn When a function pointer is defined in IENV, it is only necessary to use env1->findclass (jnienv*, const char*).

/* * C + + object wrapper. * * This is usually overlaid in a C struct whose first element is a * jninativeinterface*.  We rely somewhat on compiler behavior. */struct _jnienv {/    * don't rename this; it does not seem to be entirely opaque *    const struct JNINATIVEINTERFAC e* functions; #if defined (__cplusplus)    jint getversion ()    {return functions->getversion (this);}    ... ...     Jlong getdirectbuffercapacity (jobject buf)    {return functions->getdirectbuffercapacity (this, buf);}    /* Added in JNI 1.6 *    /jobjectreftype getobjectreftype (Jobject obj)    {return Functions->getobjectreftype ( this, obj); } #endif/*__cplusplus*/};


JNIEnv Analysis of Android JNI

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.