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=2 -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}") >= 0: 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}") >= 0: 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[ ]*=[ ]*new[ ]*File(settingsDir, 'app')" for line in lines: line_str = line.strip() match = re.match(pattern, line_str) if match: project_name = match.group(1) 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.2=../../cocos2d-x/plugin/plugins/facebook/proj.android/DependProject
注:如果此处路径添加错误 可能会有错误
BUILD FAILED /Users/yunhe/Documents/AndroidTool/sdk/tools/ant/build.xml:470: The following error occurred while executing this line: /Users/yunhe/workspace/projects/AIW/frameworks/cocos2d-x/plugin/protocols/proj.android/build.xml:90: 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(5, 6, 5, 0, 16, 8); 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++11 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:597: The following error occurred while executing this line: /Users/yunhe/Documents/AndroidTool/sdk/tools/ant/build.xml:649: The following error occurred while executing this line: /Users/yunhe/Documents/AndroidTool/sdk/tools/ant/build.xml:655: /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文件夹