The Jnihelper class in cocos2d-x encapsulates what we need by acquiring the Java virtual machine through JNI, acquiring the current program's JNI environment, getting the Java class information that needs to be called through the JNI environment, and then getting the function information in the Java class that needs to be called. The corresponding Java functions are invoked using the class information and function information through the JNI environment.
Main interface:
static bool Getstaticmethodinfo (Jnimethodinfo &methodinfo, const char *classname, const char *methodname, const char * Paramcode);
function: Get Java class corresponding function information, CLASSID, Methodid, etc.
Arguments from left to right: the reference to the Jnimethodinfo, the path to the class, the name of the function calling Java, and the shorthand for invoking the Java function type: (parameter) return type
such as: (Ljava/lang/string;i) I means the function has string,int two parameters, the return value is int
Jint Callstaticintmethod (Jclass clazz, Jmethodid Methodid,
Function: Execute call via jnienv, return value int type
JNIEnv has a series of calling function formats: callstatic[return type]method, [return type] is different from function return type, corresponding to different function name
Arguments: When executing a call to a Java function, you need to pass in the arguments, add the parameters sequentially to ClassID, Methodid, and convert the C + + variable type to the Java type
such as: jstring Stringarg = Methodinfo.env->newstringutf (key);//Parameter conversion int Res=methodinfo.env->callstaticintmethod ( Methodinfo.classid, Methodinfo.methodid, stringarg,value);//execute parameter call and return value
Example:
Call Java static function Getint
static int getInt (const char* key,int value,const char* classPath) {Jnimethodinfo methodinfo;if (! Jnihelper::getstaticmethodinfo (MethodInfo, ClassPath, "GetInt", "(ljava/lang/string;i) I"))//Get Java class corresponding getInt function information { return-1;} Jstring Stringarg = Methodinfo.env->newstringutf (key);//Parameter conversion int Res=methodinfo.env->callstaticintmethod ( Methodinfo.classid, Methodinfo.methodid, stringarg,value);//execute the parameter call and return value Methodinfo.env->deletelocalref ( STRINGARG); Methodinfo.env->deletelocalref (METHODINFO.CLASSID); return res;}
Cococs2d-x C + + calls Java static functions