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 proj.android/res/values/strings.xml:

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

Step2: Add the following codes in proj.android/AndroidManifest.xml:

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

<activity android:name="com.facebook.LoginActivity" />
<provider android:authorities="com.facebook.app.NativeAppCallContentProvider641876029265874"
          android:name="com.facebook.NativeAppCallContentProvider"
          android:exported="true"/>

Step3: Add plugin-x link lib in proj.android/jni/Android.mk

...
LOCAL_WHOLE_STATIC_LIBRARIES += PluginProtocolStatic
...

$(call import-module,cocos2d/plugin/protocols/proj.android/jni)

Step4: Add the following codes in proj.android/jni/hellocpp/main.cpp

...
#include "PluginJniHelper.h"
...
void cocos_android_app_init (JNIEnv* env, jobject thiz) {
    ...
    AppDelegate *pAppDelegate = new AppDelegate();
    JavaVM* vm;
    env->GetJavaVM(&vm);
    PluginJniHelper::setJavaVM(vm);
}

Step5:Add the following codes in proj.android/project.properties:

android.library.reference.2=../cocos2d/plugin/plugins/facebook/proj.android/DependProject

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

...
<linkedResources>
       ...
    <link>
        <name>plugin-x</name>
        <type>2</type>
        <locationURI>PARENT-1-PROJECT_LOC/cocos2d/plugin/publish</locationURI>
    </link>
</linkedResources>
...

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 proj.android 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" />
      </path>   
</target>

Step8:Modify Android.mk

...
LOCAL_STATIC_LIBRARIES += PluginProtocolStatic
...
$(call import-module,plugin/protocols/proj.android/jni)

Step9: Modify AppActivity.java,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 {
    @Override
    public Cocos2dxGLSurfaceView onCreateView() {
        Cocos2dxGLSurfaceView glSurfaceView = new Cocos2dxGLSurfaceView(this);
        // TestCpp should create stencil buffer
        glSurfaceView.setEGLConfigChooser(5, 6, 5, 0, 16, 8);

        PluginWrapper.init(this);
        PluginWrapper.setGLSurfaceView(glSurfaceView);
        FacebookWrapper.onCreate(this);
        return glSurfaceView;
    }

    @Override
    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);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        FacebookWrapper.onSaveInstanceState(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 Android.mk to configure the related lua bindins files

LOCAL_SRC_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_C_INCLUDES := \
...
$(LOCAL_PATH)/../../../cocos2d-x/plugin/luabindings/auto \
$(LOCAL_PATH)/../../../cocos2d-x/plugin/luabindings/manual
...

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();
        ScriptEngineManager::getInstance()->setScriptEngine(engine);

        ...

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

        sc->start();    

        ...

        engine->executeScriptFile(ConfigParser::getInstance()->getEntryFile().c_str());
        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.