"Android system development" JNIEnv analysis of Android JNI

Source: Internet
Author: User

.


jni.h file : Understanding JNI needs to cooperate with Jni.h file, 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 Java in the execution environment of 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, and the JNI global only has one;

-- jnienv : JAVAVM in-line, each thread has one, there may be very many jnienv in JNI;


jnienv effect :

-- Call Java functions : jnienv represents the Java execution environment and is able to invoke code in Java using JNIEnv;

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


2. Creation and release of JNIEnv


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

Source in C : Jniinvokeinterface is a JAVAVM struct in the C locale, invoking (*attachcurrentthread) (javavm*, jnienv**, void*) methods to Acquiring JNIENV structure;

-- C + + source : _JAVAVM is a JAVAVM struct in C + +, calling Jint Attachcurrentthread (jnienv** p_env, void* Thr_args) method to 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->detachcurrentth in the JAVAVM struct (_javavm) Read (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 call to the C language structure of the proposed method, C and C + + method same */Jint Attachcurrentthread (jnienv** p_env, void* Thr_args) {return Functions->attachcurrentthread (this, p_env, Thr_args);} /* Release the jnienv of this thread, call the C-language structure of the method, C and C + + method same */Jint Detachcurrentthread () {return functions->detachcurrentthr EAD (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 can not 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, thus accepting different jnienv;


jnienv Structure : 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 detailed 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 struct : This struct defines a large number of function pointers that point to functions related to Java-related variables, assuming that in the 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) : After the omission, the definition of Java-related methods, are pointers to the 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 a jninativeinterface struct based on a level two pointer env1, first obtain a pointer to the struct body, The method to be taken is (*ENV1), so the function pointer in the call points to this way: (*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* : The definition is this typedef _jnienv jnienv, jnienv* env is equivalent to _jnienv* env1, so call _jni When a function pointer is defined in ENV, 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*/};


"Android system development" JNIEnv analysis of Android JNI

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.