After hours of struggling with small bugs in JNI code I got tired and wrote these variadic template wrappers for cocos2d-x JniHelper to make calling Java from C++ nice and easy
Here’s an example. Suppose we have a Java class
class Logger {
public static void purchase(float sum, String currency) {
// some code here
}
}
Then we can call it from C++ with just one line of code
There can be any number of arguments and the method signature is automatically inferred under the hood with template magic. Supported argument data types are: bool, char, short, int, long, float, double, char* and std::string. Make sure the arguments on the Java side have corresponding primitive types (no Boolean, Integer, etc), except for char* and std::string, corresponding Java type for them is String.
One thing to note is that it should be obvious for the compiler to infer template types, otherwise incorrect signature might be generated and JNI won’t be able to find the method you are trying to call. This can be achieved with three different approaches:
@TheCodez major numbers are changed when a major version (think if big refactoring a possible breaking backward compatibility) is released. we are not planning that at the moment.
looks good to me. I added my comments. Basically I would add a tests that shows how to call system libraries… like creating a native button or something like that.
Sorry for bother old topic, but is there any reason why its not in release? There are any drawback or …?
Use this in two major projects, many many thanks to the author - almost all the pain from marshaling go away.
Cool! Not found it before because was looking for easy jni namespace, sorry. Perhaps need to more read cocos code, than this forum for outdated examples in another threads))