We know that the use of Javah generates C + + when in a header file, Java defines the native function to produce the corresponding JNI layer functionality, as follows:
/* Class: com_lms_jni_jnitest * Method: getteststring * Signature: () ljava/lang/string; * /Jniexport jstring jnicall java_com_lms_jni_jnitest_getteststring (jnienv *, jobject);
We can see that the method name starts with Java_com_lms_jni and so on. What else is called a signature. What do these facts mean? Let's have a brief look at it today.
JNI Naming conventions
For traditional JNI programming, the JNI method has a corresponding relationship with the name of the Java class method. To follow a certain naming convention, such as the following:
1) Prefix: Java_
2) The fully qualified name of the class. Delimited by underscore (_): Com_lms_jni_jnitest
3) method Name: getteststring
3) The JNI function specifies the first number of parameters: JNIEnv *
4) The JNI function specifies the second number of references: Jobject
5) Actual Java references: jstring, Jint ....
6) The number of references to the return value: Jstring, Jint ....so for one of the methods in Java class Com.lms.jni.HwDemo:
Public native string AddTail (string tail);
The method of its corresponding JNI layer is as follows:
Jstring Java_com_lms_jni_hwdemo_addtail (jnienv * E, jobject clazz, jstring tail);
Assuming this is not the case, when loading the dynamic library into the DVM, use the jnienv * pointer to find the appropriate JNI method for the Java native method. will not be found.
Note that we can also use the method of the function register to save the method name of the Java layer with the corresponding relationship of the method name of the JNI layer. In a DVM, there is no need for this naming convention.
JNI data type
We know that Java data types are not the same as the data types of c/s + +, and JNI is a layer in the middle of Java and native local libraries (most of which are written in C/s + +), and JNI must do a conversion between two different data types. So there is a corresponding relationship between the data types in JNI and Java.
in the JNI. Provides the following types of data, which can be categorized as native and reference types: for native types: Jchar, Jbyte, Jshort, Jint, Jlong, Jfloat, jdouble, Jboolean, It corresponds to the Java-side data type, such as the following table:
| Java |
Jni |
| Char |
Jchar |
| Byte |
Jbyte |
| Short |
Jshort |
| Int |
Jint |
| Long |
Jlong |
| Float |
Jfloat |
| Double |
Jdouble |
| Boolean |
Jboolean |
For reference types There are: Jobject, jstring, jthrowable, Jclass, Jarray, and inherit from Jarray. 8 Kinds of J<type>array and jobjectarray corresponding to their native type.
Know the conversion relationships for different data types. We know under what circumstances the data should be treated.
Jni method signature
Why would there be a way to sign something like that? This is because the Java side supports function overloading, that is, even though the number of references is different. But the method name is the same, then the method name will be the same in the JNI layer, and the JNI will be confused. Which one do you have to find?
It's just that it's not the same as the type of the parameter. So there is the method signature. Use method signatures and method names to uniquely determine the invocation of a JNI function.
Now that the method signature is based on the difference of the type of the parameter, the first thing to know is what the corresponding signature of the Java data type is, the so-called type signature,
A set of rules such as the following are already defined in the Jni.h file:
typedef Union JVALUE { jboolean z; Jbyte B; Jchar C; Jshort S; Jint i; Jlong J; Jfloat F; Jdouble D; Jobject l;} jvalue;
Corresponding to the Java side of the data type, we can also look at the following table:
| Java type |
Type signature |
| Boolean |
Z |
| Byte |
B |
| Char |
C |
| Short |
S |
| Int |
I |
| Long |
L |
| Float |
F |
| Double |
D |
| Class |
L fully qualified name; For example, String, whose signature is ljava/lang/util/string; |
| Array |
[Type signature, for example [B] |
For the class above, notice the other semicolon behind it.
For a method, its signature is the string of its parameter type signature and the return value type signature, in the form of such as the following:
(Type signature 1 type Signature 2 ... ) return value type signature
There are no spaces between each type signature. Here's a look at two examples:
There are method 1):
public string AddTail (string tail, int index)
Its corresponding signature is as follows:
(ljava/util/string;i) ljava/util/string;
Method 2):
public int addvalue (int index, String value,int[] arr)
Its corresponding signature is as follows:
(iljava/util/string; [I) I
I believe that through these two examples, you can also understand the method signature is the form of it, for JNI these strange forms of expression also have a certain understanding.
End.
Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.
Android about JNI Learning (two) some knowledge of jni method names, data types, and method signatures