C + + Call Java example
1 #if(Cc_target_platform = = cc_platform_android)2#include"Platform/android/jni/jnihelper.h"3 4 intKuniulogin (Lua_state *Lua)5 {6 Jnimethodinfo MethodInfo;7Auto IsOk = Jnihelper::getstaticmethodinfo (MethodInfo,"org/cocos2dx/cpp/appactivity","Sdklogin","() V");8 if(isOk)9 {TenAuto Activityobj = methodinfo.env->Callstaticobjectmethod (Methodinfo.classid, methodinfo.methodid); One } A return 0; - } - #endif
Premise hypothesis:
There is such a Java file.
Org/cocos2dx/cpp/appactivity.java
This class has a static common function of Sdklogin.
Class Appactivity {
public static void Sdklogin () {}
};
We need to call the above Java function Sdklogin through C + +.
JniHelper.h is the COCOS2DX engine that comes with the Java-based header file, which does not work on the Windows platform.
If you need a syntax hint for VS, you can write it out of conditional compilation,
This will be an error, because it contains the jni.h file, this file in the Java Virtual Machine installation directory can be found,
Put it in the VS include directory.
" org/cocos2dx/cpp/appactivity " " Sdklogin " " () V ");
The first parameter is a parameter of type Jnimethodinfo.
The second parameter specifies the path to the Java file,
The third argument is the function name,
The fourth parameter is a parameter signature. (Google has a more detailed explanation of the signature).
Returns true to indicate that the GET function succeeded.
So
Auto Activityobj = Methodinfo.env->callstaticobjectmethod (Methodinfo.classid, Methodinfo.methodid);
This line is called the ...
Java calls the C + + example.
1 extern "C" {2 #if(Cc_target_platform = = cc_platform_android)3 voidJava_org_cocos2dx_cpp_jnitesthelper_kuniulogin (JNIENV *env, Jobject thiz, jstring jstr)4 {5Auto Stropenid = env->Getstringutfchars (Jstr, NULL);6Env->Releasestringutfchars (Jstr, Stropenid);7 }8 #endif9}
Notice the name of the function.
Java prefix.
Org_cocos2dx_cpp the package name.
The Jnitesthelper class name.
The name of the Kuniulogin function.
Parameters:
The first two parameters are required, the specific use of Google has:
The following parameters fill in the required, note type.
When you get parameters inside a function, you get them through the above method.
Java just build a class, named Jnitesthelper,
Define the function Kuniulogin, which must be defined according to the NDK convention.
public static native void Kuniulogin (String openId);
The native prefix must be added, and this function does not need to be implemented in Java and automatically maps C + + functions.
When using the direct Jnitesthelper.kuniulogin (""); You can do it.
COCOS2DX java,c++ calling functions to each other