cocos2d接安卓facebook插件(已测cocos-x 3.7 3.8版本)
1 控制台创建新工程:
a 控制台 进入cocos2d文件夹下面,如cocos2d-x-3.7.1,执行setup.py,未设置NDK SDK ANT 路径的设置路径,需要改路径的 explore 命令改路径,source 命令保存
b new 一个工程,我的是js工程,cocos new -p com.xxxxx.aiw -l js ~/workspace/test,一顿等待,在用户名下的workspace里出现了新的工程文件夹
2 没facebook的注册新账号,我注册时候要上传身份照片,不是美帝没身份证么,还查户口啊,果断借了个同事账号,账号下新建一个app,得到AppID,下面开启安卓开发选项,此时需要KeyHashs,具体看http://www.cnblogs.com/pengyingh/articles/4759575.html
3 具体参考 http://www.cocos.com/doc/article/index?type=cocos2d-x&url=/doc/cocos-docs-master/manual/framework/cocos2d-js/catalog/../../html5/facebook-sdk/facebook-sdk-on-android/zh.md
为增加代入感,复述一遍
a 在frameworks/runtime-src/proj.android/res/values/strings.xml添加app name和app id:
b 在frameworks/runtime-src/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.NativeAppCallContentProvider1450063488603945"
android:name="com.facebook.NativeAppCallContentProvider"
android:exported="true"/>
c 在frameworks/runtime-src/proj.android/jni/Android.mk中添加Plugin-x链接库:
LOCAL_WHOLE_STATIC_LIBRARIES += jsb_pluginx_static
$(call import-module,cocos2d-x/plugin/jsbindings)
ps:改成
$(call import-module,plugin/jsbindings) 否则找不到路径
pps:如果还不行一定是添加顺序问题
LOCAL_WHOLE_STATIC_LIBRARIES += jsb_pluginx_static
LOCAL_EXPORT_CFLAGS := -DCOCOS2D_DEBUG= -DCOCOS2D_JAVASCRIPT include $(BUILD_SHARED_LIBRARY) $(call import-module, scripting/js-bindings/proj.android)
$(call import-module, plugin/jsbindings)
要这样,我会说谎?
为啥这样改?看下面。。。 /tools/cocos2d-console/plugins/project_compile/build_android.py
self.ndk_module_paths = cfg['ndk_module_path']
这里ndk_module_path就是build_cfg.json里面的参数
"ndk_module_path" :[
"../../cocos2d-x",
"../../cocos2d-x/cocos",
"../../cocos2d-x/external"
],
def do_ndk_build(self, ndk_build_param, build_mode, compile_obj):
cocos.Logging.info(MultiLanguage.get_string('COMPILE_INFO_NDK_MODE', build_mode))
ndk_root = cocos.check_environment_variable('NDK_ROOT') toolchain_version = self.get_toolchain_version(ndk_root, compile_obj) if self.use_studio:
ndk_work_dir = os.path.join(self.app_android_root, 'app')
else:
ndk_work_dir = self.app_android_root
reload(sys)
sys.setdefaultencoding('utf8')
ndk_path = cocos.CMDRunner.convert_path_to_cmd(os.path.join(ndk_root, "ndk-build")) module_paths = []
for cfg_path in self.ndk_module_paths:
if cfg_path.find("${ENGINE_ROOT}") >= :
cocos_root = cocos.check_environment_variable("COCOS_X_ROOT")
module_paths.append(cfg_path.replace("${ENGINE_ROOT}", cocos_root))
elif cfg_path.find("${COCOS_FRAMEWORKS}") >= :
cocos_frameworks = cocos.check_environment_variable("COCOS_FRAMEWORKS")
module_paths.append(cfg_path.replace("${COCOS_FRAMEWORKS}", cocos_frameworks))
else:
module_paths.append(os.path.join(self.app_android_root, cfg_path))
#打印一下
#hhahahah1:~AIW/frameworks/runtime-src/proj.android/../../cocos2d-x 安卓工程文件夹的爷爷文件夹下的cocos2d-x
#hhahahah1:~AIW/frameworks/runtime-src/proj.android/../../cocos2d-x/cocos 安卓工程文件夹的爷爷文件夹下的cocos2d-x下的cocos文件夹
#hhahahah1:~AIW/frameworks/runtime-src/proj.android/../../cocos2d-x/external 安卓工程文件夹的爷爷文件夹下的cocos2d-x下的external文件夹
#好吧你赢了 end
# delete template static and dynamic files
obj_local_dir = os.path.join(ndk_work_dir, "obj", "local")
if os.path.isdir(obj_local_dir):
for abi_dir in os.listdir(obj_local_dir):
static_file_path = os.path.join(ndk_work_dir, "obj", "local", abi_dir)
if os.path.isdir(static_file_path):
self.remove_c_libs(static_file_path) # windows should use ";" to seperate module paths
if cocos.os_is_win32():
ndk_module_path = ';'.join(module_paths)
else:
ndk_module_path = ':'.join(module_paths) ndk_module_path= 'NDK_MODULE_PATH=' + ndk_module_path if ndk_build_param is None:
ndk_build_cmd = '%s -C %s %s' % (ndk_path, ndk_work_dir, ndk_module_path)
else:
ndk_build_cmd = '%s -C %s %s %s' % (ndk_path, ndk_work_dir, ' '.join(ndk_build_param), ndk_module_path) ndk_build_cmd = '%s NDK_TOOLCHAIN_VERSION=%s' % (ndk_build_cmd, toolchain_version) if build_mode == 'debug':
ndk_build_cmd = '%s NDK_DEBUG=1' % ndk_build_cmd self._run_cmd(ndk_build_cmd)
结束语:ndk_module_path中是不包含 cocos2d-x这个文件的父文件路径的,所以
$(call import-module,cocos2d-x/plugin/jsbindings) 找路径不到就正常啦 > <
改改改
d 在frameworks/runtime-src/proj.android/jni/hellojavascript/main.cpp文件中添加以下代码片段:
#include "PluginJniHelper.h"
void cocos_android_app_init (JNIEnv* env, jobject thiz) {
LOGD("cocos_android_app_init");
AppDelegate *pAppDelegate = new AppDelegate();
JavaVM* vm;
env->GetJavaVM(&vm);
PluginJniHelper::setJavaVM(vm);
}
e 在frameworks/runtime-src/Classes/AppDelegate.cpp中添加如下代码片段。注意:请留意插入代码的位置:
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#include "jsb_cocos2dx_pluginx_auto.hpp"
#include "jsb_pluginx_extension_registration.h"
#endif bool AppDelegate::applicationDidFinishLaunching()
{
ScriptingCore* sc = ScriptingCore::getInstance(); ... // Add these lines before sc->start()
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
sc->addRegisterCallback(register_all_pluginx_protocols);
sc->addRegisterCallback(register_pluginx_js_extensions);
#endif sc->start(); ... return true;
}
f 在frameworks/runtime-src/proj.android/build-cg.json文件的copy_resources
添加如下代码片段:
{
"from": "../../js-bindings/cocos2d-x/plugin/jsbindings/script",
"to": ""
},
{
"from": "../../js-bindings/cocos2d-x/plugin/protocols/proj.android/src",
"to": "../src"
},
{
"from": "../../js-bindings/cocos2d-x/plugin/plugins/facebook/proj.android/src",
"to": "../src"
},
{
"from": "../../js-bindings/cocos2d-x/plugin/plugins/facebook/proj.android/sdk",
"to": "../libs"
}
from是相对于工程的目录,例如在我的工程目录下,就是 ~/workspace/projects/AIW/frameworks/runtime-src/proj.android ,../是工程爸爸所在目录 ../../工程爷爷目录 如此理解发现这里路径就不对了
改为
{
"from": "../../cocos2d-x/plugin/jsbindings/script",
"to": ""
},
{
"from": "../../cocos2d-x/plugin/protocols/proj.android/src",
"to": "../src"
},
{
"from": "../../cocos2d-x/plugin/plugins/facebook/proj.android/src",
"to": "../src"
},
{
"from": "../../cocos2d-x/plugin/plugins/facebook/proj.android/sdk",
"to": "../libs"
}
注:to 是 assets文件夹下,../是assets文件夹所在的目录
具体参看: build_android.py
路径 ~/cocos2d-x-3.7.1(根据每个人版本不同)/tools/cocos2d-console/plugins/project_compile/build_android.p
if self.use_studio:
assets_dir = os.path.join(self.app_android_root, "app", "assets")
project_name = None
setting_file = os.path.join(self.app_android_root, 'settings.gradle')
if os.path.isfile(setting_file):
# get project name from settings.gradle
f = open(setting_file)
lines = f.readlines()
f.close() pattern = r"project\(':(.*)'\)\.projectDir[ \t]*=[ \t]*new[ \t]*File\(settingsDir, 'app'\)"
for line in lines:
line_str = line.strip()
match = re.match(pattern, line_str)
if match:
project_name = match.group()
break if project_name is None:
# use default project name
project_name = 'app'
gen_apk_folder = os.path.join(self.app_android_root, 'app/build/outputs/apk')
else:
assets_dir = os.path.join(self.app_android_root, "assets")
project_name = self._xml_attr(self.app_android_root, 'build.xml', 'project', 'name')
gen_apk_folder = os.path.join(self.app_android_root, 'bin')
# copy resources
for cfg in res_files:
cocos.copy_files_with_config(cfg, app_android_root, assets_dir)
cocos.py
~/cocos2d-x-3.7.1/tools/cocos2d-console/bin/cocos.py
def copy_files_with_config(config, src_root, dst_root):
src_dir = config["from"]
dst_dir = config["to"] src_dir = os.path.join(src_root, src_dir)
dst_dir = os.path.join(dst_root, dst_dir) include_rules = None
if "include" in config:
include_rules = config["include"]
include_rules = convert_rules(include_rules) exclude_rules = None
if "exclude" in config:
exclude_rules = config["exclude"]
exclude_rules = convert_rules(exclude_rules) copy_files_with_rules(
src_dir, src_dir, dst_dir, include_rules, exclude_rules)
g 在frameworks/runtime-src/proj.android/project.properties中添加如下代码片段:
android.library.reference.=../../cocos2d-x/plugin/plugins/facebook/proj.android/DependProject
注:如果此处路径添加错误 可能会有错误
BUILD FAILED
/Users/yunhe/Documents/AndroidTool/sdk/tools/ant/build.xml:: The following error occurred while executing this line:
/Users/yunhe/workspace/projects/AIW/frameworks/cocos2d-x/plugin/protocols/proj.android/build.xml:: Cannot find /Users/yunhe/workspace/projects/AIW/frameworks/cocos2d-x/plugin/protocols/proj.android/${plugin.dir}/tools/android/build_common.xml imported from /Users/yunhe/workspace/projects/AIW/frameworks/cocos2d-x/plugin/protocols/proj.android/build.xml
h 在frameworks/runtime-src/proj.android/src/org/cocos2dx/javascript/AppActivity.java中添加如下代码片段:
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);
glSurfaceView.setEGLConfigChooser(, , , , , );
PluginWrapper.init(this);
PluginWrapper.setGLSurfaceView(glSurfaceView);
FacebookWrapper.onCreate(this);
return glSurfaceView;
} @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
FacebookWrapper.onAcitivityResult(requestCode, resultCode, data);
} @Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
FacebookWrapper.onSaveInstanceState(outState);
} //...
}
proj.android就是我们的工程 * 100 说一百遍,src里面就是调用的java文件,copyResource正确,里面会出现plugin文件夹
前面第三条c点得作用很重要
LOCAL_WHOLE_STATIC_LIBRARIES += jsb_pluginx_static
$(call import-module, plugin/jsbindings)
plugin/jsbindings 下有一个Android.mk
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := jsb_pluginx_static LOCAL_MODULE_FILENAME := libcocos2dxjsbpluginx LOCAL_SRC_FILES := auto/jsb_cocos2dx_pluginx_auto.cpp \
manual/jsb_pluginx_basic_conversions.cpp \
manual/jsb_pluginx_extension_registration.cpp \
manual/jsb_pluginx_manual_callback.cpp \
manual/jsb_pluginx_manual_protocols.cpp \
manual/jsb_pluginx_spidermonkey_specifics.cpp \
manual/pluginxUTF8.cpp LOCAL_CFLAGS := -DCOCOS2D_JAVASCRIPT -std=c++ LOCAL_EXPORT_CFLAGS := -DCOCOS2D_JAVASCRIPT LOCAL_C_INCLUDES := $(LOCAL_PATH)/manual \
$(LOCAL_PATH)/auto \
$(LOCAL_PATH)/../../cocos \
$(LOCAL_PATH)/../protocols/include LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/manual \
$(LOCAL_PATH)/auto LOCAL_WHOLE_STATIC_LIBRARIES += spidermonkey_static
LOCAL_WHOLE_STATIC_LIBRARIES += cocos2dx_static
LOCAL_WHOLE_STATIC_LIBRARIES += PluginProtocolStatic include $(BUILD_STATIC_LIBRARY) $(call import-module,external/spidermonkey/prebuilt/android)
$(call import-module,.)
$(call import-module,plugin/protocols/proj.android/jni)
LOCAL_C_INCLUDES := ...
manual auto 等等引用,此时main.cpp中的 #include "PluginJniHelper.h" 再也不会找不到所在地了
4 打包:在工程下
cocos run -p android -j 4
-j参数指定使用多少个线程执行编译,官方的说法是可以加快编译速度。(可选项)
接个facebook费半管血,毁在平时没怎么学习android,话说一个游戏开发工具,教程也n久不更新,接个插件要使用者做这么多工作,真的好么?免费不是借口,我还是学习去吧。。
注意:1. 打包前需要修改 proj.android 目录下的 build_native.py 文件
resources_dir = os.path.join(app_android_root, "../../cocos2d-x/cocos/scripting/js-bindings/script") #pengyingh
cocos_root = os.path.join(project_root, "..", "..", "cocos2d-x") #pengyingh
2.如果build报错
BUILD FAILED
/Users/yunhe/Documents/AndroidTool/sdk/tools/ant/build.xml:: The following error occurred while executing this line:
/Users/yunhe/Documents/AndroidTool/sdk/tools/ant/build.xml:: The following error occurred while executing this line:
/Users/yunhe/Documents/AndroidTool/sdk/tools/ant/build.xml:: /Users/yunhe/workspace/projects/AIW/frameworks/cocos2d-x/plugin/plugins/facebook/proj.android/DependProject/src does not exist.
只需要在
/Users/yunhe/workspace/projects/AIW/frameworks/cocos2d-x/plugin/plugins/facebook/proj.android/DependProject/
下创建src文件夹
cocos2d接安卓facebook插件(已测cocos-x 3.7 3.8版本)的更多相关文章
- 【03】Chrome提示印象笔记剪藏插件"已停用不支持的扩展程序"怎么办?
[03] Chrome提示印象笔记剪藏插件"已停用不支持的扩展程序"怎么办? 刚好也遇上了这个问题,百度了一下,以下是解决方法,亲测可行: 1.首先把需要安装的第三方插件,后缀.c ...
- Eclipse4.5 Mars版本安装activiti插件 亲测可用
Eclipse4.5 Mars版本安装activiti插件 亲测可用 学习使用activiti 在线安装一直,国内的网络真心的是 很苦 啊:在茫茫网络上面找到了很多插件的离线包 终于找到一个可以使用的 ...
- 【linux环境】Linux环境 php连接oracle11g数据库(相关插件已备份至U盘)
1.环境:centos6 . LNMP(linux环境都可以,跟服务器没啥大关系) 2.前期准备:弄清楚 项目php的运行目录,php.ini的配置目录,php-config的运行目录 3.安装先知: ...
- cordova-plugin-android-update安卓版本更新插件使用
原文:cordova-plugin-android-update安卓版本更新插件使用 安装插件,使用方法官方都有. 安卓7.0以上要在AndroidMainfest.xml里加一句 <uses- ...
- 使用 Android Studio 开发 widget 安卓桌面插件
•What AppWidget 即桌面小部件,也叫桌面控件,就是能直接显示在Android系统桌面上的小程序: 这么说可能有点抽象,看图: 像这种,桌面上的天气.时钟.搜索框等等,都属于 APP Wi ...
- 【转】NPAPI 插件无法在 Chrome 42 版及更高版本上正常运行
原文网址:https://support.google.com/chrome/answer/6213033 NPAPI 插件无法在 Chrome 42 版及更高版本上正常运行 您可以利用插件在浏览器中 ...
- unity5.3 安卓广告插件打包出错的理解
今天打包带广告插件的安卓包,出现了问题 ,上网找了很多解决的办法.整理一下. 1,有的说法是unity5的BUG 因为同一个项目 用unity4.6打包就没问题 到5就出问题,当然 姑且可以这么认为 ...
- cocos2d-js IOS接facebook插件
当前测试版本:cocos2d-x 3.8.1 3.7也试用,之下的版本没测过,一般是路径改变,文件名称一般不会变 注:当前工程是通过控制台new的工程,不是cocosStudio创建的工程 ...
- 开发原生安卓cordova插件(有原生界面)
上文开发的插件没有调用原生界面,本文介绍开发带有activity的插件 本文很多操作与上文重复,重复部分会省略 首先打开plug1,先开发插件的原生代码 在以下命名空间创建一个activity 名称为 ...
随机推荐
- python编程之禅
在python界面输入 import this >>> import this The Zen of Python, by Tim Peters Beautiful is bette ...
- windows10系统右键添加cmd命令
https://blog.csdn.net/Mr_BEelzebub/article/details/78776104 首先,在桌面新建一个文本文档. Windows Registry Editor ...
- elt区间分布
select DATE_FORMAT(CURDATE(),'%Y%m%d') DateId,elt(interval(curnum,0, 10000,20000,30000,40000,50000, ...
- 字典(dictionary) 的基本操作
info = { ’stu1101‘ : ’xiaoming’, ‘stu1102 : xiahong‘, ’stu1103 : ‘xiaozhi', } 1. 字典的获取 info.get('stu ...
- MySQL JSON 类型数据操作
1.Java 中动态扩展字段,会导致数据库表被锁,在MySQL 5.7.8版本之前,因为MySQL不能直接操作JSON类型数据,可以将一个字段设定成varchar类型,里面存放JSON格式数据,这样在 ...
- sublime打开文本时会记忆上次关闭时鼠标停留的位置
sublime打开文本时会记忆上次关闭时鼠标停留的位置
- orientdb 学习
简介 OrientDB 是一款 NoSQL 数据库.是一种文档-图数据库.即:既具有文档数据库的特性,又具有图数据库的功能. 端口 2424 OrientDB 监听 二进制 访问的的端口 (即:通过 ...
- ComputeSignature 中行支付签名报错(win7 64位系统)
在做中行加密验签的时候出现的问题.原本在XP系统下可以正常运行的,现在换了win7 64位系统就出现了这个问题,没头绪 所以发上来求各位大大支招 有什么好的解决方案.. 我的解决办法: 1.C:\Do ...
- 《Blue Flke》第一次作业:团队亮相
1.队名:Blue Flke 团队格言:决心是成功的力量,耐心是成功的保障. 2.团队成员组成: 201571030129/ 王胜海 (组长) 201571030126/ 妥志福 20157103 ...
- Haddop SHUTDOWN_MSG: Shutting down NameNode at java.net.UnknownHostException: process01: process01: unknown error
SHUTDOWN_MSG: Shutting down NameNode at java.net.UnknownHostException: process01: process01: unknown ...