Execute Java code from cpp with cocos2dx

environment

cocos2dx 3.17 (JavaScript) Android

problem

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.

crash example

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.

Call example

http://developer.wonderpla.net/entry/blog/engineer/cocos2d-x_jni2/

By implementing as follows, cpp-> java (-> javascript) was realized.

Implementation

#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

Execute Java code from cpp with cocos2dx
Code Java from Emacs with Eclim
Execute packaged Java code with commands
Execute non-Java instructions from Java
Java build with mac vs code
Work with Google Sheets from Java
Sample code using Minio from Java
Search and execute method by name from instance with processing (java)
What I learned from doing Java work with Visual Studio Code
Prepare Java development environment with VS Code
[JaCoCo (Java Code Coverage)] Use with NetBeans
[Java] Flow from source code to execution
Execute Java code stored on the clipboard.
Generate source code from JAR file with JD-GUI of Java Decompiler project
Script Java code
Run Rust from Java with JNA (Java Native Access)
Use native libraries from Scala via Java CPP + Java
Java code TIPS
Static code analysis with Checkstyle in Java + Gradle
[Java] Set the time from the browser with jsoup
Java sample code 02
Java sample code 03
[Java] Explanation of Strategy pattern (with sample code)
Text extraction in Java from PDF with pdfbox-2.0.8
Make Java code coverage more comfortable with Jacoco 0.8.0
Use Matplotlib from Java or Scala with Matplotlib4j
Using Gradle with VS Code, build Java → run
Java sample code 01
Java character code
Try debugging a Java program with VS Code
Build a Java development environment with VS Code
Getting started with Java programs using Visual Studio Code
Build VS Code + WSL + Java + Gradle environment from scratch
[Java 11] I tried to execute Java without compiling with javac
Call a method with a Kotlin callback block from Java
Why can I develop Java with Visual Studio Code?
I tried calling Java / Objective-C native code from Flutter
[Note] Create a java environment from scratch with docker
Build Java development environment with WSL2 Docker VS Code
How to build Java development environment with VS Code
[Environment construction] Build a Java development environment with VS Code!
Read temperature / humidity with Java from Raspberry Pi 3 & DHT11
Install java with Homebrew
Call Java from JRuby
Lombok with VS Code
Changes from Java 8 to Java 11
Sum from Java_1 to 100
Change seats with java
Install Java with Ansible
Eval Java source from Java
Comfortable download with JAVA
Access API.AI from Java
Switch java with direnv
From Java to Ruby !!
Build Java with Wercker
Endian conversion with JAVA
Monitor source code changes with guard-shell and make & execute automatically
How to decompile apk file to java source code with MAC
Find the address class and address type from the IP address with Java
[Java] Get Charset with Apathce Tika / Initialize String from Charset [Kotlin]
Java web application development environment construction with VS Code (struts2)