Android JNI development advanced

Source: Internet
Author: User

Statement: This article reposted from Android Development Network, http://www.android123.com.cn/androidkaifa/683.html
A powerful and useful function in Android JNI development is to create a Java class from the JNI layer, or execute methods on the Java layer to obtain attributes.

I. operations related to Classes

1.Jclass findclass (jnienv * ENV, const char * Name );Search Class

This function may have been developed in Java and is implemented in the JNI layer. Note that the second parameter is of the const char * type, if we use the getstringutfchars function to convert the Unicode-encoded jstring type from the Java slave layer to the const char * of utf8, if the Java Class Object jclass is returned successfully, related exceptions may occur.

(1. The data format of the classformaterror class is invalid.
(2. classcircularityerror: the class or interface is its own superclass or superinterface.
(3. noclassdeffounderror does not find the class or interface with the specified name.
(4. OOM memory insufficiency error, that is, outofmemoryerror

2.Jclass getsuperclass (jnienv * ENV, jclass clazz );Obtain the parent class or superclass

The second parameter of this function is the jclass class. When we call this function, it is a subclass. Otherwise, the return value is null.

3.Jboolean isassignablefrom (jnienv * ENV, jclass clazz1, jclass clazz2 );Determine whether the class1 object can be safely forcibly converted to a class2 object

If jni_true can be returned, the defined value of jni_true is 1; otherwise, jni_false is returned, that is, 0. Here, android123 details which situations may return true:

(1) These two class parameters reference the same Java class
(2. The first class is the subclass of the second class.
(3. The second class is an interface of the first class.

4.Jclass getobjectclass (jnienv * ENV, jobject OBJ );Get this class through the object

This function is relatively simple. The only note is that the object cannot be null. Otherwise, the obtained class certainly returns NULL.

5.Jboolean isinstanceof (jnienv * ENV, jobject OBJ, jclass clazz );Determines whether the object is an instance of a class.

This function is implemented on the JNI layer. I believe everyone is familiar with it. The Android Development Network reminds everyone that the returned value may have objections. If the second parameter is a null object, null objects can be forcibly converted to various types. Therefore, jni_true is returned in this case. Therefore, it must be determined whether the input object is null.

6.Jboolean issameobject (jnienv * ENV, jobject ref1, jobject ref2 );Determine whether two objects reference the same class

It should be noted that if both objects are empty, the returned value will also be jni_true, so the object is null when used.

Ii. Call the Java method

First, let's talk about the signature sig related, such as "ljava/lang/string ;"

1.Jmethodid getmethodid (jnienv * ENV, jclass clazz, const char * Name, const char * sig );Obtains the ID of a Java method.

This function returns the method ID of a non-static class or interface instance method. This method can be defined in a clazz superclass or inherited from clazz. The last parameter is the signature. The last two parameters are of the const char * type and are of the utf8 type. It should be noted that android123 reminds you that executing the getmethodid () function will lead to initialization of uninitialized classes. To obtain the method ID of the constructor, use <init> as the method name, at the same time, void (v) is used as the return type. If the specified ID cannot be found, null is returned, and the exception may include:

(1 nosuchmethoderror cannot find the specified Java method.
(2 exceptionininitializererror if the class initialization program fails due to an exception
(3 outofmemoryerror: insufficient memory

2.Nativetype callxxxmethod (jnienv * ENV, jobject OBJ, jmethodid methodid, va_list ARGs );Call the Java method of XXX type

When executing a method in a Java class, note that the Java class in this class is non-static. Because there are many Java methods, this function may have the following forms, for example, callobjectmethod, callbooleanmethod, callbytemethod, callcharmethod, callshortmethod, callintmethod, calllongmethod, callfloatmethod, calldoublemethod, and callvoidmethod, the third parameter of this function is the method ID obtained through the getmethodid function. The last parameter is the parameter table of this method. The final va_list macro can obtain the specific usage through search, here, the android development network will not go into details.

3.Nativetype callnonvirtualxxxmethod (jnienv * ENV, jobject OBJ, jclass clazz, jmethodid methodid, jvalue * ARGs );

The callnonvirtualxxxmethod function differs from the preceding callxxxmethod function by adding a jclass parameter. callxxxmethod calls a method based on an object, while callnonvirtualxxxmethod calls a method based on an instance of the class. The difference lies in this.

The above three are non-static class acquisition, execution call, need to instantiate this class can be executed, the following is static call.

4. jmethodid getStaticMethodid (jnienv * ENV, jclass clazz, const char * Name, const char * sig );

5. nativetype callStaticXxxmethod (jnienv * ENV, jclass clazz, jmethodid methodid ,...);

III,Access the Java object domain

Execution of similar methods for fields and attributes of Java objects

1. jfieldid getfieldid (jnienv * ENV, jclass clazz, const char * Name, const char * sig); get the domain ID of the Instance Object

Note that non-static instantiated objects may cause exceptions

(1 nosuchfielderror cannot find the specified domain
(2) exceptionininitializererror class initialization fails due to an exception
(3) outofmemoryerror has insufficient memory.

2. nativetype getxxxfield (jnienv * ENV, jobject OBJ, jfieldid fieldid );

Similar to the getxxxmethod function, some types may include getobjectfield, getbooleanfield, getbytefield, getcharfield, geteffecfield, getintfield, getlongfield, getfloatfield, and getdoublefield.

3. Void setxxxfield (jnienv * ENV, jobject OBJ, jfieldid fieldid, nativetype value );

Java fields can be assigned values. Some types may include setobjectfield, setbooleanfield, setbytefield, setcharfield, setshortfield, setintfield, setlongfield, setfloatfield, and setdoublefield.

The above three cases are non-static object fields. For fields that do not need to be instantiated, you can directly use the following.

4. jfieldid getstaticfieldid (jnienv * ENV, jclass clazz, const char * Name, const char * sig );

5. nativetype getstaticxxxfield (jnienv * ENV, jclass clazz, jfieldid fieldid );

6. Void setstaticxxxfield (jnienv * ENV, jclass clazz, jfieldid fieldid, nativetype value );

4. Example code: android123 provides an example for netizens to help them develop Android JNI. you can transplant it to the android ndk environment for execution, and users can access Android JNI development code (Android
JNI instance code (1 ))

Finally, for the final content of Android JNI, Android development network mainly describes the global references of JVM and JNI, such as local Global references of localglobalref and weak global references of weakglobalref, advanced methods for thread processing in JNI, such as attachcurrentthread, AND NiO related features in JNI will be explained tomorrow, for more information about Android ndk development, see our android ndk development skills series.

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.