Monday, March 12, 2012

Upgrade with cocos2d-1.0.1-x-0.12.0 and integrated with your original JNI functions...

Here are some notes that when I upgraded my game to .12 version what I learned about with new JNI architecture that cocos2d-x used.

Question 1:In the jni\helloworld\main.cpp, you can find there is already JNI_Onload() there. Yes, that means you can't implement your JNI_Onload.

Solution:You can call RegisterNatives() at JNI_Onload() which at main.cpp instead to register your jni class.

Something like the code below :

jclass clazz;
JNIEnv *env;
vm->GetEnv((void**) &env, JNI_VERSION_1_6);
const char *classPathName = "com/zhihmeng/ChickenEggsX/ChickenEggsX";
JNINativeMethod methods[] = {
{"init", "(Ljava/lang/Object;)V",
(void *)Java_com_zhihmeng_ChickenEggsX_ChickenEggsX_init},
clazz = env->FindClass(classPathName);
if (clazz == NULL) {
// LOGE("Native registration unable to find class '%s'", className);
return JNI_FALSE;
if (env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0])) < 0) { // LOGE("RegisterNatives failed for '%s'", className); return JNI_FALSE; } Question 2:You may do some modifications on your JNI class, and JniHelper make these thing easier. Solution:Use JniHelper to get some static method info like MessageJni.cpp(located at cocos2dx\platform\android\jni) does and call your method by it. The code will like below: JniMethodInfo t; if (JniHelper::getStaticMethodInfo(t , "com/zhihmeng/ChickenEggsX/ChickenEggsX" , "exportToShare" , "()V")) { t.env->CallStaticVoidMethod(t.classID, t.methodID);

Finally, thanks the tweaks of cocos2d-x new JniHelper, it make jni calling more elegant.

You are also welcome to my studio page to see some of my works :

No comments:

Post a Comment