[原创]COCOS2DX 3.8 增加protobuf为external库

此处为记录防止忘记,最近使用COCOS练习一些新的库,所以需要把PROTOBUF编译进去,看上去很麻烦,这里记录一下,以防忘记。

 

首先下载protobuf 2.5.0版本,下载地址请自行百度。

准备工作:

CYGWIN下载:http://cygwin.com/setup-x86.exe

1、请确保机器中有CYGWIN,并且CYGWIN必须安装好GCC,GDB和MAKE等,文章很多,请自行搜索

2、配置CYGWIN的NDK环境:

打开CYGWIN目录下的home\Administrator\.bash_profile,并在最下面添加内容:

NDK=/cygdrive/J/android-ndk-r10d

export NDK

重启CYGWIN,测试执行:cd $NDK ,此处NDK环境配置完毕

3、确保安装好COCOS2DX最新版

4、曾经使用过PROTOBUF,本文非PROTOBUF教程,为配置引导教程。

 

编译PROTOBUF ANDROID

解压缩PROTOBUF文件至COCOS目录,注意:为了防止文件混乱建议最好在COCOS根目录下新建名为THRDLIB的目录存放PROTOBUF。

解压后用WINDOWS版本先编译,如果出现MIN和MAX函数错误,只需要在:src\google\protobuf\io 目录下找到 zero_copy_stream_impl_lite.cc 文件,在文件头部增加 INCLUDE:#include <algorithm>,此处BUG修复,请安心兼容ANDROID。

(建议:请先新建一份COCOS的DEMO游戏,并将WINDOWS版本的PROTOBUF放入引用,新建对应的测试协议,确保在WINDOWS一切正常)

编译ANDROID版本步骤:

1、进入PROTOBUF解压所在目录

2、新建JNI目录,进入该目录

3、新建文件:Application.mk

内容:

APP_STL := gnustl_static 
APP_CPPFLAGS := -frtti 
APP_ABI := armeabi armeabi-v7a x86

解释:此处ABI为兼容COCOS的3中情况,分别编译3中类型的库,稍后会提供配置方式。

4、新建文件:Android.mk

内容:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := protobuf_static

LOCAL_MODULE_FILENAME := protobuf

LOCAL_SRC_FILES :=src/google/protobuf/io/coded_stream.cc \
 src/google/protobuf/stubs/common.cc \
 src/google/protobuf/descriptor.cc \
 src/google/protobuf/descriptor.pb.cc \
 src/google/protobuf/descriptor_database.cc \
 src/google/protobuf/dynamic_message.cc \
 src/google/protobuf/extension_set.cc \
 src/google/protobuf/extension_set_heavy.cc \
 src/google/protobuf/generated_message_reflection.cc \
 src/google/protobuf/generated_message_util.cc \
 src/google/protobuf/io/gzip_stream.cc \
 src/google/protobuf/compiler/importer.cc \
 src/google/protobuf/message.cc \
 src/google/protobuf/message_lite.cc \
 src/google/protobuf/stubs/once.cc \
 src/google/protobuf/compiler/parser.cc \
 src/google/protobuf/io/printer.cc \
 src/google/protobuf/reflection_ops.cc \
 src/google/protobuf/repeated_field.cc \
 src/google/protobuf/service.cc \
 src/google/protobuf/stubs/structurally_valid.cc \
 src/google/protobuf/stubs/strutil.cc \
 src/google/protobuf/stubs/substitute.cc \
 src/google/protobuf/text_format.cc \
 src/google/protobuf/io/tokenizer.cc \
 src/google/protobuf/unknown_field_set.cc \
 src/google/protobuf/wire_format.cc \
 src/google/protobuf/wire_format_lite.cc \
 src/google/protobuf/io/zero_copy_stream.cc \
 src/google/protobuf/io/zero_copy_stream_impl.cc \
 src/google/protobuf/io/zero_copy_stream_impl_lite.cc \
 src/google/protobuf/stubs/stringprintf.cc

LOCAL_EXPORT_C_INCLUDES :=
LOCAL_EXPORT_LDLIBS :=

LOCAL_C_INCLUDES := $(LOCAL_PATH) \
$(LOCAL_PATH)/src

LOCAL_LDLIBS := \
-llog \
-lz

include $(BUILD_STATIC_LIBRARY)

5、回到上一级目录,将SRC目录整个拷贝至JNI目录下。

6、打开CYGWIN,进入PROTOBUF目录下(访问WINDOWS目录方法为:cd /cygdrive/f/protobuf)

7、输入:

$NDK/ndk-build

静静的等待安装完毕。

 

将protobuf安装至external目录

编译完毕protobuf后,我们开始安装对应的文件。

步骤如下(以COCOS2DX 3.8为例):

1、进入COCOS框架目录下:Cocos\Framework\frameworks\cocos2d-x-3.8\external

2、新建目录protobuf

3、在目录下新建 include 以及 prebuilt 目录 ,此处开始较为重要

4、将PROTOBUF目录下的src目录下的.h文件拷贝至COCOS框架下的external/protobuf/include目录下(注意COPY完毕后,其相对路径会是:external\protobuf\include\google)。

5、进入external/protobuf/prebuilt,新建目录android(如已经编译好WIN32版本,请直接新建WIN32目录,并COPY lib文件至该目录下)

6、回到PROTOBUF编译目录下的obj\local目录下,拷贝目录下的3个文件夹:armeabi、armeabi-v7a、x86 至COCOS框架目录下的:external/protobuf/prebuilt/android目录下,并记得删除每个目录内的obj文件夹,保留.a文件,别删错噢。

7、在external/protobuf/prebuilt/android目录下新建文件:Android.mk

内容如下:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := protobuf_static
LOCAL_MODULE_FILENAME := libprotobuf
LOCAL_SRC_FILES := $(TARGET_ARCH_ABI)/libprotobuf.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/../../include
include $(PREBUILT_STATIC_LIBRARY)

8、进入COCOS的cocos2d-x-3.8\cocos\prebuilt-mk目录下 打开文件 Android.mk 文件,请务必使用NOTEPAD++或UE等工具打开,否则可能排版混乱

9、修改该文件内容如下:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := cocos2dx_internal_static

LOCAL_MODULE_FILENAME := libcocos2dxinternal

ifeq ($(USE_ARM_MODE),1)
LOCAL_ARM_MODE := arm
endif

ifeq ($(TARGET_ARCH_ABI),armeabi-v7a)
MATHNEONFILE := math/MathUtil.cpp.neon
else
MATHNEONFILE := math/MathUtil.cpp
endif

LOCAL_SRC_FILES := ..\..\prebuilt\android/$(TARGET_ARCH_ABI)/libcocos2dxinternal.a


LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/.. \
$(LOCAL_PATH)/../. \
$(LOCAL_PATH)/../.. \
$(LOCAL_PATH)/../platform \
$(LOCAL_PATH)/../base \
$(LOCAL_PATH)/../../external \
$(LOCAL_PATH)/../../external/tinyxml2 \
$(LOCAL_PATH)/../../external/unzip \
$(LOCAL_PATH)/../../external/chipmunk/include/chipmunk \
$(LOCAL_PATH)/../../external/xxhash \
$(LOCAL_PATH)/../../external/nslog \
$(LOCAL_PATH)/../../external/poly2tri \
$(LOCAL_PATH)/../../external/poly2tri/common \
$(LOCAL_PATH)/../../external/poly2tri/sweep \
$(LOCAL_PATH)/../../external/clipper


LOCAL_EXPORT_LDLIBS := -lGLESv2 \
                       -llog \
                       -landroid

LOCAL_WHOLE_STATIC_LIBRARIES := cocos_freetype2_static
LOCAL_WHOLE_STATIC_LIBRARIES += cocos_png_static
LOCAL_WHOLE_STATIC_LIBRARIES += cocos_jpeg_static
LOCAL_WHOLE_STATIC_LIBRARIES += cocos_tiff_static
LOCAL_WHOLE_STATIC_LIBRARIES += cocos_webp_static
LOCAL_WHOLE_STATIC_LIBRARIES += cocos_chipmunk_static
LOCAL_WHOLE_STATIC_LIBRARIES += cocos_zlib_static
LOCAL_WHOLE_STATIC_LIBRARIES += recast_static
LOCAL_WHOLE_STATIC_LIBRARIES += bullet_static

LOCAL_WHOLE_STATIC_LIBRARIES += cocos2dxandroid_static

# define the macro to compile through support/zip_support/ioapi.c
LOCAL_CFLAGS   :=  -DUSE_FILE32API
LOCAL_CFLAGS   +=  -fexceptions
LOCAL_CPPFLAGS := -Wno-deprecated-declarations -Wno-extern-c-compat
LOCAL_EXPORT_CFLAGS   := -DUSE_FILE32API
LOCAL_EXPORT_CPPFLAGS := -Wno-deprecated-declarations -Wno-extern-c-compat

include $(PREBUILT_STATIC_LIBRARY)

#==============================================================

include $(CLEAR_VARS)

LOCAL_MODULE := cocos2dx_static
LOCAL_MODULE_FILENAME := libcocos2d
LOCAL_SRC_FILES := ..\..\prebuilt\android/$(TARGET_ARCH_ABI)/libcocos2d.a

LOCAL_WHOLE_STATIC_LIBRARIES := cocostudio_static
LOCAL_WHOLE_STATIC_LIBRARIES += cocosbuilder_static
LOCAL_WHOLE_STATIC_LIBRARIES += cocos3d_static
LOCAL_WHOLE_STATIC_LIBRARIES += spine_static
LOCAL_WHOLE_STATIC_LIBRARIES += cocos_network_static
LOCAL_WHOLE_STATIC_LIBRARIES += audioengine_static
#此处增加额外的POTOBUF库文件
LOCAL_WHOLE_STATIC_LIBRARIES += protobuf_static

include $(PREBUILT_STATIC_LIBRARY)
#==============================================================
$(call import-module,freetype2/prebuilt/android)
$(call import-module, platform/android/prebuilt-mk)
$(call import-module,png/prebuilt/android)
$(call import-module,zlib/prebuilt/android)
$(call import-module,jpeg/prebuilt/android)
$(call import-module,tiff/prebuilt/android)
$(call import-module,webp/prebuilt/android)
$(call import-module,chipmunk/prebuilt/android)
$(call import-module, 3d/prebuilt-mk)
$(call import-module, audio/android/prebuilt-mk)
$(call import-module, editor-support/cocosbuilder/prebuilt-mk)
$(call import-module, editor-support/cocostudio/prebuilt-mk)
$(call import-module, editor-support/spine/prebuilt-mk)
$(call import-module, network/prebuilt-mk)
$(call import-module, ui/prebuilt-mk)
$(call import-module, extensions/prebuilt-mk)
$(call import-module, Box2D/prebuilt-mk)
$(call import-module, bullet/prebuilt-mk)
$(call import-module, recast/prebuilt-mk)
$(call import-module,curl/prebuilt/android)
$(call import-module,websockets/prebuilt/android)
$(call import-module, flatbuffers/prebuilt-mk)
#此处增加额外的PROTOBUF库
$(call import-module, protobuf/prebuilt/android)

至此,可以在COCOS STUDIO中尝试编译你的项目至ANDROID,PROTOBUF应该已经在里面了,而且不管新建几个项目,都会自动包含,不用另外配置。

此处额外分享一套自动搜索CPP,C以及CC文件的MK内容,不必挨个增加编译文件噢(适用于大部分COCOS项目,几乎不必修改任何内容,新增CPP也会编译在内,非常方便噢)。

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := cocos2dcpp_shared LOCAL_MODULE_FILENAME := libcocos2dcpp # 遍历目录及子目录的函数 define walk $(wildcard $(1)) $(foreach e, $(wildcard $(1)/*), $(call walk, $(e))) endef # 遍历Classes目录 ALLFILES = $(call walk, $(LOCAL_PATH)/../../Classes) FILE_LIST := hellocpp/main.cpp # 从所有文件中提取出所有.cpp文件 FILE_LIST += $(filter %.cpp, $(ALLFILES)) FILE_LIST += $(filter %.cc, $(ALLFILES)) FILE_LIST += $(filter %.c, $(ALLFILES)) LOCAL_SRC_FILES := $(FILE_LIST:$(LOCAL_PATH)/%=%) #LOCAL_SRC_FILES += $(LOCAL_PATH)/../../libs/protobuf/android/$(TARGET_ARCH_ABI)/libprotobuf.a # 修改include路径 FILE_INCLUDES := $(shell find $(LOCAL_PATH)/../../Classes -type d) LOCAL_C_INCLUDES := $(FILE_INCLUDES) #LOCAL_SRC_FILES := hellocpp/main.cpp \ # ../../Classes/AppDelegate.cpp \ # ../../Classes/HelloWorldScene.cpp # ../../Classes/ LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes LOCAL_C_INCLUDES += $(LOCAL_PATH)/../../libs/protobuf/include LOCAL_CFLAGS := -v # _COCOS_HEADER_ANDROID_BEGIN # _COCOS_HEADER_ANDROID_END LOCAL_STATIC_LIBRARIES := cocos2dx_static LOCAL_STATIC_LIBRARIES += libprotobuf # _COCOS_LIB_ANDROID_BEGIN # _COCOS_LIB_ANDROID_END include $(BUILD_SHARED_LIBRARY) $(call import-module,./prebuilt-mk) # _COCOS_LIB_IMPORT_ANDROID_BEGIN # _COCOS_LIB_IMPORT_ANDROID_END

 

posted @ 2015-09-27 15:35  koangel  阅读(1070)  评论(0编辑  收藏  举报