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

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


This is third part of the tutorial series. In this part, it will walk you through the process of integrating “Facebook” step by step. There are some differences between Cocos2d-x 2.x and Cocos2d-x 3.0 with integrating “Facebook”.

Downloading and Installing:

You can download cocos2d-x v3.0beta from : http://www.cocos2d-x.org/download

The architecture and API is many difficult from v2.2.2/v3.0beta

The biggest change is v3.0: Changing namespace from “cocos2d” to “cc”

How to install it :

If you don not familiar with how to install it. just can reference "How to create a “Hello World” C*+ project with cocos2d-x2.2.2 or 3.0beta"

You can download FacebookSDK from : https://developers.facebook.com/docs/android/

Create a Facebook App

Access to Facebook Developers site, on the top bar, go to ‘Apps’ > ‘Create a New App’:

And enter basic information such as its name and a unique namespace:

Once created, note down the app ID shown at the top of the dashboard page. You’ll need to add this to your project files.

Go to ‘Setting’ in the left nav , then click on the ‘Add Platform’ button and select Android. Add the debug key hash that you obtained. And check “Single Sign On”

If you have a release key, you will need to add its hash in the same way.
To obtain the release key hash, On OS X, run:

keytool -exportcert -alias <RELEASE_KEY_ALIAS> -keystore <RELEASE_KEY_PATH> | openssl sha1 -binary | openssl base64

On windows ,Use:

keytool -exportcert -alias <RELEASE_KEY_ALIAS> -keystore <RELEASE_KEY_PATH> | openssl sha1 -binary | openssl base64

You will be prompted for a password. This should be ‘android’ without quotes.

You can get more information : https://developers.facebook.com/docs/android/getting-started

Integrate Facebook’s SDK in Eclipse

Before you Integrate Facebook’s SDK in Eclipse, you’ll move the SDK to the your cocos2d-x project root path “/YOUR_PROJECT/”

So let’s Open Eclipse and import 3 projects.

1, facebook-android-sdk:
under the path “/FacebookTutorialV3/facebook”

2, proj.android:
under the path “/YOUR_PROJECT/proj.android”

3, libcocos2dx:
under the path"/cocos2d-x-3.0beta2/cocos/2d/platform/android/java"

Then, make sure your “facebook-android-sdk” is a library for main project.

Now you have been integrated facebookSDK to your android projects. Go ahead and build and run the template as-is. If all works OK, you should see the following:

Facebook Plugin

You can download Facebook Plugin from :https://github.com/Ryeeeeee/FacebookTutorial

This is a simple Plugin we encapsulated method as following:


Move following files(.java) to “/Users/USERNAME/cocos2d-x/projects/FacebookTutorial/proj.android/src/org/cocos/fbtutorial”

Now we need to know how to connect “FacebookPlugin” to your “Main.Activity”.

Here is your “Main.Activity”, it dependent to What name you created the game.

Open your the AndroidManifest.xml and add following code:

Move “pick_friends_activity.xml” to “res/layout/”

Add the following code into “res/values/strings.xml”

Add the following code into “jni/android.mk”:

In this step, you’ll add code to implement some features.If you want to integreate the plugin you want, just add the plugin into your activity.For example, the followings show how to add Facebook Login Plugin. You can add other plugins in the same way.

Define a private variable for the FacebookLoginPlugin instance:

private FacebookConnectPlugin facebookLoginPlugin = null;

Next, override the onCreate() method to create a new FacebookLoginPlugin object, passing in the Context object.

protected void onCreate(Bundle savedInstanceState) {
		facebookLoginPlugin = new FacebookConnectPlugin(this);

To ensure that the sessions are set up correctly, your activity must override the activity lifecycle methods:onCreate(), onResume(), onActivityResult(), onPause(), onDestroy() and onSaveInstanceState() and call the corresponding FacebookLoginPlugin methods.

Add the following code to make those changes:

public void onResume() {

public void onActivityResult(int requestCode, int resultCode, Intent data) {
	super.onActivityResult(requestCode, resultCode, data);
	facebookLoginPlugin.onActivityResult(requestCode, resultCode, data);

public void onPause() {

public void onDestroy() {

public void onSaveInstanceState(Bundle outState) {

projectroot.png (57.6 KB)