Many Java classes in Android have native interfaces, which are implemented locally and then registered in the system.
The main JNI code is placed in the following path: frameworks/base/core/jni/, the contents of this path are compiled into a library libandroid_runtime.so, which is placed in the/system/lib directory of the target system. In addition, there are other JNI libraries for Android. Each file in the JNI is actually a normal C + + source file.
If you want to learn more about the Android framework layer, you must have an Android native layer run and development mechanism.
Here is a first introduction to some native basics
1. Interface definition
_JNIENV defines the interface of a virtual machine that provides access to all the functions of a virtual machine:
1) Assign the object (Allocobject/newobject) and control the reference count of the object (newglobalref/deleteglobalref/deletelocalref/issameobject/ NEWLOCALREF).
2) Get the definition of the class (Findclass) and get the ID of the method and member that gets the class by the definition of the class (Getmethodid/getfieldid)
3) Calling the class's normal method (Callobjectmethod) and static method (Callstaticobjectmethod) by method ID
4) Get and set ordinary members (Getobjectfield/setobjectfield) and static members (Getstaticobjectfield/setstaticobjectfield) of the class by member ID
The following are the more common methods:
1) Find the class:
Jclass xxx = (*env)->findclass (env, "lclass_name;");
2) Get the ID of the method:
Jmethodid xxx = (*env)->getmethodid (env, Jclass, MethodName, "(M) N");
3) Find the method of the class that needs to be called:
Jmethodid xxx = (*env)->getmethodid (env, Jclass, "(M) N");
4) Get the ID of the static method
Jmethodid xxx = (*env)->getstaticmethodid (Env,jclass, MethodName, "(M) N")
5) Initialize an instance of the class:
Jobject xxx = (*env)->newobject (env, Jclass, Jmethodid);
6) A method that invokes an instance:
(*env)->callobjectmethod (env, Jobject, Jmethodid, [Parameter1, Parameter2,...]);
7) Release the instance:
(*env)->deletelocalref (env, XXX);
8) Get the ID of the member variable
Jfieldid xxx = (*env)->getfieldid (Env,jclass, Jfieldid,jfieldtype)
9) Get the ID of the static member variable
Jfieldid xxx = Getstaticfieldid (Env,jclass, Jfieldid,jfieldtype)
Operating Environment of Jnienv-java
Jobject-represents Java's instance
Jclass-classes that represent Java
2. Function and Property signature
In both the Getmethodid and Getfieldid functions, the last parameter is a signature string that identifies the uniqueness of the Java function and the member.
Because overloaded functions exist in Java, a function name is not sufficient to uniquely specify a function, and a signature string is required to specify the function's argument list and return value type.
The function signature is a string: "(M) N"
The content in parentheses is the parameter type of the function, followed by parentheses to indicate the function's return value.
3. JNI type signature
"(m) n", where M and N refer to the type signature of the input and output parameters of the function (type Signature).
The corresponding relationship of each character is as follows
Character Java Type C type
V void void
Z Jboolean Boolean
I Jint int
J Jlong Long
D jdouble Double
F jfloat Float
B Jbyte Byte
C Jchar Char
S Jshort Short
The array starts with a "[" and is represented by two characters
[I Jintarray int[]
[F Jfloatarray float[]
[B Jbytearray byte[]
[C Jchararray char[]
[S Jshortarray short[]
[D Jdoublearray double[]
[J Jlongarray long[]
[Z Jbooleanarray boolean[]
If the Java function parameter is class, start with "L" and ";" End, the middle is a "/" separated by the package and the class name. and the corresponding C function name parameter is Jobject
One exception is the string class, whose corresponding class is jstring
ljava/lang/string; String jstring
Ljava/net/socket; Socket Jobject
If the Java function is in an embedded class, use $ as the delimiter between the class names.
For example "(ljava/lang/string; landroid/os/fileutils$filestatus;) Z "
Illustrate the meaning of "(M) N", for example:
(I) V with a parameter of type int, the return value type is void
() D no arguments, return double
This article welcomes the reprint, but please indicate the author and source:
Meteor
Source: Http://blog.sina.com.cn/staratsky
Learn more about the JNI (one) of the Android platform