Learning about JNI in Android (iii) Accessing Java-side objects at the JNI layer

Source: Internet
Author: User

In the first two articles, we briefly introduce some correspondence between the JNI layer and Java layer, including the method name, data type and method name, which can help us to understand the role of JNI in the local development of native, and also have a preliminary understanding of some concepts of JNI in the theoretical level. Because of the ability to express or understanding or limited, some places are not very clear, if you have a friend feel foggy, welcome to leave a message to study together.

Conceptual understanding helps us to better understand JNI, while some examples of practical points can better help us to master and apply JNI from the code.

In the first article, we learned from a small example, in which we returned a string through the JNI layer function, as follows:

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

This is the simplest case, but more often, we need to get the Java object in the JNI layer, manipulate it, and finally return the result to the Java side, so this time we are going to use the second parameter of the JNI function definition Jobject.

Previous article, we said, JNIEnv * and JOBEJCT parameters are the JNI layer method added parameters, about jnienv* we have in the previous article briefly introduced, and Jobject parameters, then we this article to manipulate the parameters.

For local methods, the native method defined in Java, there are static (static) and non-static methods, and we know that the static method is a method that belongs to this class, the object cannot manipulate it, and the non-static method is just the opposite, so in the method parameter of the JNI layer:

1) for static methods, the Jobject parameter represents a reference to the corresponding Java class.

2) for non-static methods, Jobject represents a reference to the corresponding Java object.

This should not be difficult to understand.

Next, we use a small demo to learn how to manipulate Java-side objects in the JNI layer and change their values.

First, we have a static variable testval in the Java class, and a method Changetestval () to change the value of the TestVal, as follows:

public class Paramtransfertest {public static int testval = 1;public native void Changetestval (); ...}

Of course, first, the first step is to implement its corresponding function in C, as follows:

jniexport void Jnicall java_com_lms_jni_paramtransfertest_changetestval  (jnienv * env, Jobject obj) {Jclass Clazz = ( *env)->getobjectclass (env,obj); Jint val = (*env)->getstaticintfield (env, Clazz, (*env)->getstaticfieldid ( Env, Clazz, "TestVal", "I")); Logi ("before change testval =%d", val); val = val + 1; Logi ("After change testval =%d", val);(*env)->setstaticintfield (env, Clazz, (*env)->getstaticfieldid (env, Clazz , "TestVal", "I"), Val);

We implement this native method in the corresponding C file, because the implementation of the non-static method, so Jobject is passed to the object reference, so we need to Getobjectclass method to obtain the corresponding class of the object.

Generally in JNI, we will use Findclass and Getobjectclass two methods to obtain the corresponding class, and put into the Jclass type of variable, but note here that the C implementation and the implementation of C + + code for JNI call method is not the same.

As stated in the previous article, C + + has a wrapper over the method defined by Jninativeinterface, so its parameters no longer need to pass env in, and C is needed, such as the method above *env call, if it is implemented in C + +, Then it is no longer necessary to pass the Env parameter in, that is Getobjectclass (Jobject) on it.

1) Using Getobjectclass method to obtain Jclass.

2) Call Getstaticintfieldid to get the corresponding class corresponding to the variable, that is, jclass in the type I (that is, int) the static (static) variable testval.

3) Call Getstaticintfield to get the value of the corresponding variable val.

4) Change the Val value, here we do the plus 1 operation.

5) Call Setstaticintfield to set the value of the corresponding variable.

So, here we find that Env actually provides a lot of methods, for accessing object variable value, divided into static non-static, basically is Get<type>field and Getstatic<type>field,

And correspondingly, there are Set<type>field and Setstatic<type>field methods.

And if the method is called, is to use the Call<type>method and Callstatic<type>method method, these people can go to the jni.h file themselves to see, we probably know how to do.

After the JNI layer has been implemented, we use the Ndk-build tool to regenerate a so library, load it into Android, and invoke the method directly in the activity as follows:

TextView tvchangetestval = (TextView) Findviewbyid (r.id.tvchangetestval); Paramtransfertest PTT = new Paramtransfertest ();p tt.changetestval (); Tvchangetestval.settext ("" + ptt.testval);

After we call the method, the value on the screen will be called after the method is displayed, the result should be 1+1=2, right, see the following results:

Indeed, as we have imagined, its value has changed to 2, right, which means that we did change the value of the JNI layer by the native method.

We just added log in the JNI to show the values before and after the change, as follows:

With such a simple small example, I believe you should know how to manipulate Java data in the JNI layer, right.

End!

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.