You can see in the JNI article in pure Java, the native method of Java corresponds to the C/C ++ code function through Java _ <package name >_< class name >_< Method Name>, that is, it is statically registered. To use the current C/C ++ code function, you need to define the packaging function in this form and call the existing C/C ++ code function in the packaging function; this function name is also very long and is not suitable for management. Dynamic registration is not restricted by the above naming conventions.
The following example shows how to change samples \ hello-JNI in Android ndk from static registration to dynamic registration. You only need to modify the JNI section.
The original code for the hello-jni.c is as follows:
# Include <string. h>
# Include <JNI. h>
/* This is a trivial JNI example where we use a native method to return a new VM string. See the corresponding JAVA Source
* File located:
* Apps/samples/Hello-JNI/project/src/COM/example/hellojni. Java
*/
Jstring
Java_com_example_hellojni_hellojni_stringfromjni (jnienv * ENV,
Jobject thiz)
{
Return (* env)-> newstringutf (ENV, "hello from JNI! ");
}
Change it
[CPP] view plaincopy
# Include <stdlib. h>
# Include <string. h>
# Include <stdio. h>
# Include <JNI. h>
# Include <assert. h>
/* This is a trivial JNI example where we use a native method to return a new VM string. See the corresponding JAVA Source
* File located:
* Apps/samples/Hello-JNI/project/src/COM/example/hellojni. Java
*/
Jstring native_hello (jnienv * ENV, jobject thiz)
{
Return (* env)-> newstringutf (ENV, "dynamically register JNI ");
}
// Method table
Static jninativemethod gmethods [] = {
{"Stringfromjni", "() ljava/lang/string;", (void *) native_hello}, // bind
};
// Register a local method for a class
Static int registernativemethods (jnienv * env
, Const char * classname
, Jninativemethod * gmethods, int nummethods ){
Jclass clazz;
Clazz = (* env)-> findclass (ENV, classname );
If (clazz = NULL ){
Return jni_false;
}
If (* env)-> registernatives (ENV, clazz, gmethods, nummethods) <0 ){
Return jni_false;
}
Return jni_true;
}
// Register a local method for all classes
Static int registernatives (jnienv * env ){
Const char * kclassname = "com/example/hellojni"; // specify the class to be registered
Return registernativemethods (ENV, kclassname, gmethods,
Sizeof (gmethods)/sizeof (gmethods [0]);
}
/*
* Called when system. loadlibrary ("lib ")
* If the JNI version is returned successfully,-1 is returned if the JNI version fails to be returned.
*/
Jniexport jint jnicall jni_onload (JavaVM * Vm, void * Reserved ){
Jnienv * Env = NULL;
Jint result =-1;
If (* VM)-> getenv (Vm, (void **) & ENV, jni_version_1_4 )! = Jni_ OK ){
Return-1;
}
Assert (ENV! = NULL );
If (! Registernatives (ENV) {// register
Return-1;
}
// Succeeded
Result = jni_version_1_4;
Return result;
}
After Dynamic Registration, the native_hello function is dynamically registered when the public native string stringfromjni () method of the hellojni class is called.
There is no difficulty in the above Code. The only thing you cannot understand is the table corresponding to the method. Let's talk about it below.
Jninativemethod is a struct defined by the JNI mechanism.
[CPP] view plaincopy
Typedef struct {
Const char * Name; // name of the function in Java
Const char * signature; // parameters and return values of functions described in strings
Void * fnptr; // function pointer to the C function
} Jninativemethod;
The second parameter is hard to understand, for example
"() V"
"(Ii) V"
"(Ljava/lang/string;) V"
In fact, these characters correspond to the function parameter types one by one.
The character in "()" represents a parameter, and the subsequent character represents the return value. For example, "() V" indicates void func ();
"(Ii) V" indicates void func (INT, INT );
The relationship between each character is as follows:
Character Java type C type
V 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 "[" 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 []
The above are all basic types. If the Java function parameter is a class, it starts with "L" and ends with ";" and ends with a package and class name separated. The parameter of the corresponding C function name is jobject. An exception is the string class, and its corresponding class is jstring.
Ljava/lang/string; string jstring
Ljava/NET/socket; socket jobject
If a Java function is located in an embedded class, $ is used as the delimiter between class names. For example, "(ljava/lang/string; landroid/OS/fileutils $ filestatus;) z"
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