Learning about JNI in Android (a) some understanding of jnienv

Source: Internet
Author: User

A simple example gives us an initial understanding of the role of JNI, but some of the concepts in JNI need to be understood so that we can better use it to achieve what we want to do.

So how does C + + and Java use JNI to invoke each other?

We know. In Android, when a Java file is compiled into a Dex file, it is loaded by the class loader into the Dalvik VM (DVM), which is interpreted by the DVM and translated into machine language, which is performed by the machine.

For C + +, the NDK toolkit that the source code provides via Android can be compiled into a running dynamic library (i.e.. so file). After. Communication is possible between Java and C + +.

Well, here, it is conceivable that the Dex bytecode of Java and the so library of C + + will certainly be executed in a DVM at the same time. They use a process space together, otherwise, how do they communicate with each other?

so here, a key middle area is the Dalvik VM. and for C + +, when they are also loaded into the DVM, the function methods implemented by C + + will be loaded into the function table in the DVM.

Suppose that you want to tune in C + + with functions, they must have something to access the function table in the virtual machine.

And this thing is jnienv *.

When we take advantage of the Javah generated header file for C + +. For example, the following:

Jniexport jstring jnicall Java_com_lms_jni_hwdemo_printhello  (jnienv *e, Jobject j) {        return (**e). Newstringutf (E, "Hello from T");}

We can see that this method has two parameters. The first of them is jnienv *. And when we define this method on the Java side. There are no parameters, such as the following:

Public native String Printhello ();

So what's this jnienv for?in fact, from the name of this parameter can be seen, refers to the implementation of the JNI environment, I think it is a reference to the Java Virtual environment. In Android. Refers to Dalvik VMs. Refer to the definition of jnienv in the jni.h file, such as the following (for C and C + +. Its definition is a little different):
struct _jnienv;struct _javavm;typedef const struct jninativeinterface* c_jnienv; #if defined (__cplusplus) typedef _ JNIEnv jnienv; The type typedef of JNIENV in C + + _JAVAVM JAVAVM; #elsetypedef const struct jninativeinterface* jnienv; C in jnienv type typedef const struct JNIINVOKEINTERFACE* JAVAVM; #endif

In C, we can see that the type of jnienv is jninativeinterface*. is a pointer type, what is _jnienv like in C + +?
struct _JNIENV {/    * don't rename this; it does not seem to be entirely opaque/    const struct JNINATIVEINTERFACE* Functions

and for C + +. _jnienv is a struct. It includes the structure of the jninativeinterface*. So I can see it from here. For C and C + +, they refer to jnienv in a slightly different way.Overall, jnienv. Whether it's C. or C + +, in fact the key is the jninativeinterface structure.

We can simply look at the definition of the jninativeinterface structure, such as the following:
struct Jninativeinterface {    void*       reserved0;    void*       reserved1;    void*       Reserved2;    void*       reserved3;    Jint        (*getversion) (jnienv *);    Jclass      (*defineclass) (jnienv*, const char*, jobject, const jbyte*,                        jsize);    Jclass      (*findclass) (jnienv*, const char*);    Jmethodid   (*fromreflectedmethod) (jnienv*, jobject);    Jfieldid    (*fromreflectedfield) (jnienv*, jobject);    /* Spec doesn ' t show jboolean parameter    *     /Jobject (*toreflectedmethod) (jnienv*, Jclass, Jmethodid, Jboolean) ;

You can see that there are a lot of function pointers defined in it, and these are defined. The JNI layer actually obtains a reference to the DVM by defining these function pointers. The ability to locate a JNI function table in a virtual machine to implement a JNI layer function call in a DVM.
So. It can be understood that, in fact, JNIEnv is a reference to the C + + function in the DVM execution environment. And because of this, when C + + wants to invoke a function in a DVM. Since they are in the DVM environment, they must be obtained by jnienv* this number of parameters before they can be used.


So when did this jnienv happen?
When the first Java thread in Android is going to call the local C + + code. The DVM generates a jnienv* pointer to the thread.

And each line Cheng C + + is called to each other. Its corresponding jnienv are also independent of each other.


Well. End.


Learning about JNI in Android (a) some understanding of jnienv

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.