cocosjs jsbinding笔记
cocosjs jsbinding笔记
IOS
步骤
- 在cocos2d/cocos/scripting/js-bindings/proj.ios_mac/目录中复制自带的工程,改成自己需要的名称。
- 在cocos2d/tools/tojs/中创建ini,直接从现有ini拷贝一个,修改文件名以及编译相关参数,文件路径等等。
- 在cocos2d/tools/tojs/创建py文件,直接将genbindings.py拷贝一份,修改文件吗,以及修改cmd_args为新建的ini文件
- 修改cocos2d/tools/bindings-generator/clang/目录cindex.py的bug。第3395行library = cdll.LoadLibrary(self.get_filename())改为library = cdll.LoadLibrary("../bindings-generator/libclang/" + self.get_filename())
- 执行cocos2d/tools/tojs/中的py生产auto文件。
- 自定义头文件加到jsext.h
ini文件说明
[cocos2dx_jsext]
# the prefix to be added to the generated functions. You might or might not use this in your own
# templates
prefix = cocos2dx_jsext
# create a target namespace (in javascript, this would create some code like the equiv. to `ns = ns || {}`)
# all classes will be embedded in that namespace
target_namespace = ccext
android_headers = -I%(androidndkdir)s/platforms/android-19/arch-arm/usr/include -I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.7/libs/armeabi-v7a/include -I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.7/include -I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.8/libs/armeabi-v7a/include -I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.8/include
android_flags = -D_SIZE_T_DEFINED_
clang_headers = -I%(clangllvmdir)s/lib/clang/%(clang_version)s/include
clang_flags = -nostdinc -x c++ -std=c++11 -U __SSE__
cocos_headers = -I%(cocosdir)s -I%(cocosdir)s/cocos -I%(cocosdir)s/cocos/base -I%(cocosdir)s/cocos/platform/android -I%(cocosdir)s/extensions -I%(cocosdir)s/external -I%(cocosdir)s/cocos/editor-support -I%(cocosdir)s/cocos/network -I%(cocosdir)s/cocos/ui/UIEditBox -I%(cocosdir)s/cocos/ui -I%(cocosdir)s/jsext -I%(cocosdir)s/jsext/system -I%(cocosdir)s/jsext/alipay -I%(cocosdir)s/jsext/video -I%(cocosdir)s/jsext/webview -I%(cocosdir)s/jsext/umeng
#cocos_headers = -I%(cocosdir)s -I%(cocosdir)s/cocos -I%(cocosdir)s/cocos/platform/android
cocos_flags = -DANDROID
cxxgenerator_headers =
# extra arguments for clang
extra_arguments = %(android_headers)s %(clang_headers)s %(cxxgenerator_headers)s %(cocos_headers)s %(android_flags)s %(clang_flags)s %(cocos_flags)s %(extra_flags)s
# what headers to parse 头文件路径
headers = %(cocosdir)s/jsext/jsext.h
# what classes to produce code for. You can use regular expressions here. When testing the regular
# expression, it will be enclosed in "^$", like this: "^Menu*$".
#包含的类,新添加文件需要修改
classes = Ext1 Ext2 AlipayUtil SysUtil UMUtil VideoUtil WebviewUtil
#需要在js里面派生的类
classes_need_extend = Ext1 Ext2
# what should we skip? in the format ClassName::[function function]
# ClassName is a regular expression, but will be used like this: "^ClassName$" functions are also
# regular expressions, they will not be surrounded by "^$". If you want to skip a whole class, just
# add a single "*" as functions. See bellow for several examples. A special class name is "*", which
# will apply to all class names. This is a convenience wildcard to be able to skip similar named
# functions from all classes.
skip =
rename_functions =
rename_classes =
# for all class names, should we remove something when registering in the target VM?
remove_prefix =
# classes for which there will be no "parent" lookup
classes_have_no_parents =
# base classes which will be skipped when their sub-classes found them.
base_classes_to_skip = Ref
# classes that create no constructor
# Set is special and we will use a hand-written constructor
abstract_classes =
# Determining whether to use script object(js object) to control the lifecycle of native(cpp) object or the other way around. Supported values are 'yes' or 'no'.
script_control_cpp = no
默认python版本编译出错解决
进入cocos2d/tools/tojs/执行
python genbindings.js
报错: Import Error: No module named yaml
解决:执行 sudo easy_install pyyaml
报错: Cheetah。。。
解决:下载cheetahtemplate-cheetah-v2.4.0.0.zip
进入目录,执行:sudo python setup.py install
Android
步骤
- 创建Android.mk
- 修改cocos2d/cocos/scripting/js-bindings/proj.android/Android.mk,在
LOCAL_C_INCLUDES
中加上导出的头文件目录,LOCAL_STATIC_LIBRARIES
加上本lib的名称,最后加上
$(call import-module,jsext),如下所示
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../manual \
$(LOCAL_PATH)/../../../../jsext \
$(LOCAL_PATH)/../../../../jsext/alipay/ \
$(LOCAL_PATH)/../../../../jsext/system/ \
$(LOCAL_PATH)/../../../../jsext/video/ \
$(LOCAL_PATH)/../../../../jsext/webview/ \
$(LOCAL_PATH)/../../../../jsext/umeng/
LOCAL_STATIC_LIBRARIES += cocos_jsext_static
$(call import-module,jsext)
LOCAL_SRC_FILES
加上auto.cpp,如下所示
LOCAL_SRC_FILES := ../auto/jsb_cocos2dx_blank_auto.cpp \ ../auto/jsb_jsext_auto.cpp \
3. 调用项目中添加头文件头文件目录
Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := cocos_jsext_static
LOCAL_MODULE_FILENAME := libjsext
ifeq ($(USE_ARM_MODE),1)
LOCAL_ARM_MODE := arm
endif
LOCAL_SRC_FILES := \
ext1.cpp \
alipay/AlipayUtil-android.cpp \
system/SysUtil-android.cpp \
umeng/UMUtil.cpp \
video/VideoUtil.cpp \
webview/WebviewUtil.cpp
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/ \
$(LOCAL_PATH)/alipay/ \
$(LOCAL_PATH)/system/ \
$(LOCAL_PATH)/video/ \
$(LOCAL_PATH)/webview/ \
$(LOCAL_PATH)/umeng/
LOCAL_C_INCLUDES := $(LOCAL_PATH)/ \
$(LOCAL_PATH)/../cocos/ui \
$(LOCAL_PATH)/umeng/UMCocos2dxAnalyticsNative_no_idfa/android/libmobclickcpp/include/ \
$(LOCAL_PATH)/alipay/ \
$(LOCAL_PATH)/system/ \
$(LOCAL_PATH)/video/ \
$(LOCAL_PATH)/webview/ \
$(LOCAL_PATH)/umeng/
LOCAL_STATIC_LIBRARIES := cocos2dx_static
#LOCAL_CXXFLAGS += -fexceptions
#LOCAL_EXPORT_C_INCLUDES :=
include $(BUILD_STATIC_LIBRARY)
Android.mk 2
编译成功后,在项目的proj.android/obj/local/armeabi/目录下可以找到lib,复制到代码目录,将mk文件修改为如下,在下次编译时,lib将不再编译。
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := cocos_jsext_static
LOCAL_MODULE_FILENAME := libjsext
ifeq ($(USE_ARM_MODE),1)
LOCAL_ARM_MODE := arm
endif
LOCAL_SRC_FILES := libjsext.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/
LOCAL_C_INCLUDES := $(LOCAL_PATH)/
include $(PREBUILT_STATIC_LIBRARY)