(Editing)How to integrate “Facebook” and use it in cocos2d-x2.2.2/3.0beta2version (Tutorial1: for Android )Part2

(Editing)How to integrate “Facebook” and use it in cocos2d-x2.2.2/3.0beta2version (Tutorial1: for Android )Part2
0.0 0


This tutorial is the second of two parts,If you’re looking to start from scratch, Part One is the tutorial for you!

The first part of this series shows how to integrate ‘Facebook Plugin’ into your android project. In this part of the tutorial, you’ll learn how to call those methods of the ‘Facebook Plugin’ in c++ side.

Integrate Facebook in C++ side

Add ‘FacebookInterface.cpp’ and ‘FacebookInterface.h’ to ‘/Users/USERNAME/cocos2d-x/projects/FacebookTutorial/proj.android/src/org/cocos/fbtutorial.’

Open HelloWorldScene.cpp and insert the following code:

#include "FacebookInterface.h"

Its easy to use Facebook Login , just call the following method. After that , the callback method CallFunctionName() will be called.You can override this method to get the result of callback.The parameter ‘cbIndex’ is a tag that will be returned to first parameter of CallFunctionName().

FacebookInterface::login(int cbIndex,const char* scope);

And you will see this if you did not install Facebook:

It works.And you can start to integrate FacebookPlugin into your project if you don’t want to know how it works.

How Facebook Plugin Works

Now,Lets see how the Login works,Jump to the define of Login() ,this method use JNI to call the static method login() of FacebookConnectPlugin.java .

void FacebookInterface::login(int cbIndex,const char* scope){
	cocos2d::JniMethodInfo t;
	if (cocos2d::JniHelper::getStaticMethodInfo(t
		, FBJavaLoginClassName
		, "login"
		, "(ILjava/lang/String;)V"))
		if (scope)
			jstring jeventId = t.env->NewStringUTF(scope);
			t.env->CallStaticVoidMethod(t.classID, t.methodID, cbIndex,jeventId);
			t.env->CallStaticVoidMethod(t.classID, t.methodID, cbIndex,NULL);

If you don’t know JNI , here is a good tutorial about JNI http://www3.ntu.edu.sg/home/ehchua/programming/java/JavaNativeInterface.html

Lets jump to the FacebookConnectPlugin.java , we can see the following code in it. C++ side will call login() to log in.

public static void login(int cbIndex, String scope) {
		callIndex = cbIndex;

public void login_() {
	Session session = Session.getActiveSession();
	if (!session.isOpened() && !session.isClosed()) {
		session.openForRead(new Session.OpenRequest(activity)
	} else {
		Session.openActiveSession(activity, true, statusCallback);

If the session state changes , the callback method call(Session session ,SessionState state,Exception exception) will be called. The nativeCallback() method will be called.

private class SessionStatusCallback implements Session.StatusCallback {
	public void call(Session session, SessionState state, Exception exception) {
	// Respond to session state changes, ex: updating the view
		onSessionStateChange(session, state, exception);

private void onSessionStateChange(Session session, SessionState state,Exception exception) {
	if (state.isOpened()) {
	} else if (state.isClosed()) {

Lets see the define of nativeCallback(). Its a native method and contains no body. The keyword ‘native’ donates that it is implemented in another language.The method is contained in the native library loaded.

private static native void nativeCallback(int cbIndex, String params);

And Facebook Plugin has implemented it in FacebookInterface,cpp. Jump to FacebookInterface.cpp, and we will see the following code in it.

void Java_org_cocos_fbtutorial_FacebookConnectPlugin_nativeCallback(JNIEnv*  env, jobject thiz, jint cbIndex,jstring params)
        if (params != NULL)
		const char* str;
        	str = env->GetStringUTFChars(params, 0);
        	std::string tstr(str);
	}else	{
		std::string tstr = "";

Finally,the callback method ‘CallFunctionName(int cbIndex,std::string tstr)’ will be called. You should override it to get result.

weblogin.png (62.9 KB)

facebookinterface.png (71.5 KB)

Facebook, Twitter and Leaderboard Integration
How to integrate “Facebook” and use it in cocos2d-x2.2.2/3.0beta2version (Tutorial1: for Android )Part1
YUYE'Tutorials(What kind of the tutorial do you want to learn and we'll make it for you)

Thank you, hope you well.


hi Yuye,
thanks for your tutorial.
I try to apply your tutorial in my project follow there step:

  1. create facebook app
  2. copy facebook SDK in my project
  3. copy your java file(I’m not copy your code in the helloworldscene, only FacebookInterface)
  4. config to build project
  5. use FacebookInterface function(login, post)
  6. run
    it run ok. But when i’m use this function my game crash. can you help me?
    thanks in advance!


I’m not sure but I think i have problem with Session.


Same here, it makes my game crash :confused: