calling c++ function from java

I have completed my gameplay section of my game and now i am working on facebook integration with cocos2dx. I have successfully integrated facebook with cocos2dx, i.e. i can call facbook features(ex inviting friends, sharing , posting scores etc) using JNI…

but the problem i am facing is that i cannot call any c++ function from java.

i am facing similar problem as mentioned in the link below:

i have also read the tutorial given in the forum:
http://www.cocos2d-x.org/wiki/How_to_use_jni

and also:
http://purplelilgirl.tumblr.com/post/54583532324/code-bit-how-to-use-jni-in-cocos2dx-android

Although i have read the above tutorials but it is still not very much clear to me about how to get started wit it.
can any one provide me with a detailed help….

In you Java file, you will have to declare your method as native.

class FacebookHandler {
    public static native callCppMethod();
}

Then, on your C*+ class, you define the native method via Java_<your_package_name>_<project_name>_<method_name>(JNIEnv *, jObject, <args>)
<pre>

extern “C” {
void Java_com_company_HelloWorld_callCppMethod {
// Do something in C*+
}
}

You can then call your native method from anywhere in your Java code, just like any java method.
class FacebookHandler {
    public static openSession() {
        // Do something
        // Report to c++
        callCppMethod();
    }

    public static native callCppMethod();
}
1 Like

Thanx a lot Lance…. I tried the same solution with few more modification and it worked…
I would like to share it(in case any one might need it)….

What i did is same as above ,but my
void Java_com_company_HelloWorld_callCppMethod(JNIEnv * env, jObject jObj)’ method cannot access any other method of the same class, for that i had to use ‘CCNotificationCenter’ to call the methods of the class….

ex-

@
#ifdef cplusplus
extern “C” {
#endif
#if
Java_com_company_HelloWorld_callCppMethod
{
CCLog;
CCNotificationCenter::sharedNotificationCenter->postNotification;
}
#endif
#ifdef
cplusplus
}
#endif
@

and add an “Observer” in your init() method:

@
#if(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
CCNotificationCenter::sharedNotificationCenter()->addObserver(this, callfuncO_selector(HelloWorld::printSomethingInCPP), “shareCallback”, NULL);
#endif
@

1 Like

Oh yeah, I forgot the method return type. Sorry about that. :stuck_out_tongue: