cocos2d-x android平台下在c++代码中调用java代码

JniHelper

cocos2d-x中封装好了一个JniHelper类,可以帮助在c++代码中调用java代码。
#include “platform/android/jni/JniHelper.h”

参考 http://blog.csdn.net/yuechuzhao/article/details/9283847

通过JniHelper获取java函数信息体

static bool getStaticMethodInfo(JniMethodInfo &methodinfo, const char *className, const char *methodName, const char *paramsAndReturntype);
static bool getMethodInfo(JniMethodInfo &methodinfo, const char *className, const char *methodName, const char *paramsAndReturytype;

示例:

static const char* JavaPackgeClassName="game/knowledge/maze/AppActivity";
JniMethodInfo methodInfo;
bool isHave = JniHelper::getStaticMethodInfo(methodInfo,JavaPackgeClassName,"fun", "(Ljava/lang/String;Ljava/lang/String;)V");

java类型简写对照

boolean Z
byte B
char C
short S
int I
long J
float F
double D
void V
Object Ljava/lang/Object;
Array [Ljava/lang/Object;

使用JniMethodInfo中的信息调用java函数

示例

//void fun(const char* title, const char* message)

jstring jtitle = methodInfo.env->NewStringUTF(title);
jstring jmessage = methodInfo.env->NewStringUTF(message);

methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID, jtitle, jmessage);

c++类型和jni类型对应关系

下面列表出部分
void jvoid
bool jboolean
char jchar
short jshort
int jint
long jlong
float jfloat
double jdouble
boolean[] jbooleanArray
char[] jcharArray
short[] jshortArray
int[] jintArray
long[] jlongArray
float[] jfloatArray
double[] jdoubleArray

NDK-JNI实战教程(二) JNI官方中文资料
http://www.cnblogs.com/jycboy/archive/2016/04/15/5396876.html