JNIEnv Parameters of NDK development

Source: Internet
Author: User

Even though our Java layer functions have no parameters, the native method has two parameters, the first of which is jnienv.
As follows:

Native method:

    publicnativestringFromC();    publicnativestringFromCpp();

Native methods:

jstring Java_com_example_jni_MainActivity_stringFromC(JNIEnv* env,jobject thiz){    return (*env)->NewStringUTF(env,"I am from C""C" jstring Java_com_example_jni_MainActivity_stringFromCpp(JNIEnv* env,jobject thiz){    return env->NewStringUTF("I am from C++");}

JNIENV is an interface pointer to the available JNI function table that uses the functions of the virtual machine through the various functions provided by the JNIEnv interface pointers. JNIEnv is a pointer to the thread-local data, and the thread-local data contains pointers to the thread table. The functions that implement the native methods will jnienv the interface pointers as their first arguments.

The native code is c and the native code is C + + its syntax for invoking the JNI function is different , in C code, JNIENV is a pointer to the JNINATIVEINTERFACE structure, in order to access any of the JNI functions, the pointer needs to be dereferenced first. Because the JNI function in C code does not understand the current JNI environment, the jnienv instance should be passed as the first parameter to each JNI function caller.

The correct wording should be the following:

jstring Java_com_example_jni_MainActivity_stringFromC(JNIEnv* env,jobject thiz){    return(*env)->NewStringUTF(env,"I am from C");}

However, in C + + code, JNIENV is actually a C + + class instance, and the JNI function exists as a member function because the JNI method has already accessed the current JNI environment, so the JNI method call does not require the JNIENV instance as a parameter, in C + +, The same function code should be done as follows:

"C" jstring Java_com_example_jni_MainActivity_stringFromCpp(JNIEnv* env,jobject thiz){    return env->NewStringUTF("I am from C++");}

This project source code download

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced. If there is a wrong place, I would appreciate it if I could criticize it.

JNIEnv Parameters of NDK development

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.