cocos2dx 3.17 (JavaScript) Android
Speaking of native on Android of cocos2dx, you write code in Java, but depending on the library you use, you have to touch cpp.
The flow of js-> java-> cpp.
On the contrary, regarding the mechanism of executing js functions from native, there is java-> js, but I want to do cpp-> js.
Older cocos2djs used ScriptingCore :: evalString (code)
.
But if I was updating cocos2dx, this would crash.
https://github.com/cocos2d/cocos2d-x/issues/17718 https://github.com/cocos2d/cocos2d-x/issues/13807
After a lot of research, it seems that JniHelper should be used.
http://developer.wonderpla.net/entry/blog/engineer/cocos2d-x_jni2/
By implementing as follows, cpp-> java (-> javascript) was realized.
#include "platform/android/jni/JniHelper.h"
void executeJavaScript()
{
cocos2d::JniMethodInfo t;
if (cocos2d::JniHelper::getStaticMethodInfo(t,
"jp/example/java", //Java class
"executeJs", //Java method name (static)
"(Ljava/lang/String;)V" //argument
)) {
// jstring - C++ representation of Java String
//Convert JS method name by writing it as a character string
jstring stringArg = t.env->NewStringUTF("javascriptMethod('arg')");
// call the method, with arguments
t.env->CallStaticVoidMethod(t.classID, t.methodID, stringArg);
}
}
Recommended Posts