Developers Manual > Cocos2d-x > Advanced Features > Third Party SDK Integration > Plugin-X > Facebook Support for Cocos2d-x Beta > Facebook integration instruction for Cocos2d-x and Cocos2d-x-Lua on android

(!) Plugin-X is obsoleted. Please check out SDKBOX for helps with your service SDK integration.

Facebook integration instruction for Cocos2d-x and Cocos2d-x-Lua on android

Create a Facebook App

Please refer to Step 5. Create a Facebook App - Getting Started with the Facebook SDK for Android to create a Facebook App with Android platform.

Integrate Facebook into Your cpp Android Project

Notice:Our project was created by cocos new which is the Cocos command.

Step1: Add app name and app id in

<string name="app_name">MyProject<string>
<string name="app_id">641876029265874<string>

Step2: Add the following codes in

<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/app_id" />

<activity android:name="com.facebook.LoginActivity" />
<provider android:authorities=""

Step3: Add plugin-x link lib in


$(call import-module,cocos2d/plugin/protocols/

Step4: Add the following codes in

#include "PluginJniHelper.h"
void cocos_android_app_init (JNIEnv* env, jobject thiz) {
    AppDelegate *pAppDelegate = new AppDelegate();
    JavaVM* vm;

Step5:Add the following codes in


Step6: Add plugin-x Linked Resources,you can modify the .project file in the as follows:


Step7:Set Java Build Path by the Eclipse,right click the MyProject,choose Properties->Java Build Path->Libaries to add JARS,

you should add five jars as follows:

Step7: Modify build.xml in the as follows:

<target name="-pre-compile">
     <!-- Redefine project.all.jars.path before -compile executes -->
     <path id="project.all.jars.path">
            <fileset file="../cocos2d/plugin/publish/protocols/android/libPluginProtocol.jar" />
            <fileset file="../cocos2d/plugin/publish/plugins/facebook/android/android-support-v4.jar" />
            <fileset file="../cocos2d/plugin/publish/plugins/facebook/android/facebooksdk.jar" />
            <fileset file="../cocos2d/plugin/publish/plugins/facebook/android/bolts.jar" />
            <fileset file="../cocos2d/plugin/publish/plugins/facebook/android/libPluginFacebook.jar" />
            <fileset file="../cocos2d/cocos/platform/android/java/bin/classes.jar" />


LOCAL_STATIC_LIBRARIES += PluginProtocolStatic
$(call import-module,plugin/protocols/

Step9: Modify,add the following codes:

import org.cocos2dx.lib.Cocos2dxGLSurfaceView;
import org.cocos2dx.plugin.PluginWrapper;
import org.cocos2dx.plugin.FacebookWrapper;
import android.content.Intent;
import android.os.Bundle;
public class AppActivity extends Cocos2dxActivity {
    public Cocos2dxGLSurfaceView onCreateView() {
        Cocos2dxGLSurfaceView glSurfaceView = new Cocos2dxGLSurfaceView(this);
        // TestCpp should create stencil buffer
        glSurfaceView.setEGLConfigChooser(5, 6, 5, 0, 16, 8);

        return glSurfaceView;

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(!PluginWrapper.onActivityResult(requestCode, resultCode, data))
            super.onActivityResult(requestCode, resultCode, data);
        FacebookWrapper.onAcitivityResult(requestCode, resultCode, data);

    public void onSaveInstanceState(Bundle outState) {

Through these steps,we could begin to use the Facebook API.

Integrate Facebook into your Lua Android Project

The most steps are similar with Integrate Facebook into your Lua android Project,the special steps as follows:

Step1: Modify the to configure the related lua bindins files

../../../cocos2d-x/plugin/luabindings/auto/lua_cocos2dx_pluginx_auto.cpp \
../../../cocos2d-x/plugin/luabindings/manual/lua_pluginx_basic_conversions.cpp \
../../../cocos2d-x/plugin/luabindings/manual/lua_pluginx_manual_callback.cpp \
../../../cocos2d-x/plugin/luabindings/manual/lua_pluginx_manual_protocols.cpp \

$(LOCAL_PATH)/../../../cocos2d-x/plugin/luabindings/auto \

Step2: Add necessary codes

  • Add related header files of Lua bindings in AppDelegate.cpp

        #include "lua_cocos2dx_pluginx_auto.hpp"
        #include "lua_pluginx_manual_callback.h"
        #include "lua_pluginx_manual_protocols.h"
  • Register lua bindings codes in AppDelegate::applicationDidFinishLaunchingfunction of AppDelegate.cpp.
    Notice: Please pay attention to the location of the codes.

    bool AppDelegate::applicationDidFinishLaunching()

        auto engine = LuaEngine::getInstance();


        //register custom function
        auto luaState = stack->getLuaState();
        lua_getglobal(luaState, "_G");



        return true;

Step3: Modify the build-cfg.json:

    "must_copy_resources": [
            "from": "../../../cocos2d-x/plugin/luabindings/script/",
            "to": "src/cocos/lua_plugin"

Through these special steps,we could begin to use the Facebook API by Lua.

To know more detail about Plugin-x,please refer to Plugin-x Architecture

How to Use Facebook API

Please visit Facebook API Reference for Cocos2d-x and Cocos2d-x-lua

add_plugin_jar.jpg (99.8 kB) samuele3hu, 2014-11-11 08:02

finish_add_jar.jpg (116.3 kB) samuele3hu, 2014-11-11 08:02

Sign up for our newsletter to keep up with the latest developments, releases and updates for Cocos2d-x.