cocos2d-x 减少编译时间/免除重复编译

前端之家收集整理的这篇文章主要介绍了cocos2d-x 减少编译时间/免除重复编译前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

cocos2d-x游戏引擎在每新建一个工程时都会附带好多库文件要编译,导致开发效率降低,一个helloworld项目初次编译可能都要10来分钟。

所以减少编译时间是非常有必要的。 (本文只讲VS2013和Android NDK的方法,其他平台大同小异)

有一个参考 http://blog.csdn.net/etring/article/details/8608032但他的还不够。

我用的是cocos2d-x-3.x 一共有30来个静态库,17个要源码编译,所以做了大量改进更通用一些。

1.新建一个项目HelloCocos2dx: cocos.py new HelloCocos2dx -p com.longshu -l cpp ,有300多M。进入proj.android目录执行ndk-build或者build_native.py

如果是android-studio,在proj.android-studio\app\jni下执行ndk-build命令。 (建议使用COCOS_ROOT/tests/cpp-empty-test空项目,免去新建项目操作)

2.成功后在proj.android\obj\local\下会默认生成armeabi平台所需要的各种静态库,其他平台在proj.android\jni\Application.mk文件添加即可

比如APP_ABI := armeabi armeabi-v7a x86生成3个平台的静态库。(两个平台第一次编译大概占880M)

3.在COCOS_ROOT(游戏引擎安装目录)下新建一个prebuild文件夹,在prebuild下新建android,android下新建armeabi armeabi-v7a x86(根据自己要的平台),

复制第2步proj.android\obj\local下的相应平台到prebuild下的相应平台目录,一共17个*.a文件(大概210M,libcocos2dcpp.so不要)。复制好后记得 ndk-build clean

4.prebuild/android文件夹下面新建一个Android.mk,内容文章最后面。该文件是根据COCOS_ROOT/cocos/Android.mk修改而来。

5. 修改proj.android/Android.mk 内容如下:

LOCAL_PATH := $(call my-dir)

#Cocos2d-x安装目录,如果环境变量有可以不配
COCOS_ROOT := D:/Dev_Tool/Cocos2d-x

include $(CLEAR_VARS)
#去除
#$(call import-add-path,$(LOCAL_PATH)/../../cocos2d)
#$(call import-add-path,$(LOCAL_PATH)/../../cocos2d/external)
#$(call import-add-path,$(LOCAL_PATH)/../../cocos2d/cocos)
#要添加 不然import-module找不到编译好的静态库
$(call import-add-path,$(COCOS_ROOT))

LOCAL_MODULE := cocos2dcpp_shared

LOCAL_MODULE_FILENAME := libcocos2dcpp

LOCAL_SRC_FILES := hellocpp/main.cpp \
                   ../../Classes/AppDelegate.cpp \
                   ../../Classes/HelloWorldScene.cpp

LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes

# _COCOS_HEADER_ANDROID_BEGIN
# _COCOS_HEADER_ANDROID_END

LOCAL_STATIC_LIBRARIES := cocos2dx_static

# _COCOS_LIB_ANDROID_BEGIN
# _COCOS_LIB_ANDROID_END

include $(BUILD_SHARED_LIBRARY)
#去除
#$(call import-module,.)
# 保存了编译好的静态库目录
$(call import-module,prebuild/android)

# _COCOS_LIB_IMPORT_ANDROID_BEGIN
# _COCOS_LIB_IMPORT_ANDROID_END


这里新加了两句:

$(call import-add-path,$(COCOS_ROOT))

$(call import-module,prebuild/android)

去除了原来的import-add-path和import-module 。

这样就可以在修改了Classes下的代码和java代码时,在几十秒内完成编译了。以后新建项目只要修改第5步的Android.mk文件就OK了。

而且还可以删除(建议不要,VS或者其他开发工具要用到)cocos2d下的extensions目录和cocos下除platform以外的目录(platform里面还有一个引用项目)。效果如下:

安卓编译效果

============================================================================================

下面介绍VS2013减少编译时间的方法 和安卓的方法不同

1.打开COCOS_ROOT/build/cocos2d-win32.sln也可以使用上面的HelloCocos2dx,主要是为了方便调试。选择libcocos2d项目,Debug模式,右键->生成,漫长等待后,

在build\Debug.win32目录下会有大量的*.lib和*.dll库文件生成(其实好多是来自Cocos已经编译好的复制过来的),一共有45个文件

2.在COCOS_ROOT(游戏引擎安装目录)下新建一个prebuild文件夹,在prebuild下新建win32文件夹(最好在下面新建Release和Debug区分编译版本),复制第1步的lib到win32下Debug和复制dll文件win32下。

3.修改项目配置: 选择HelloCocos2dx解决方案->右键->属性->通用属性-项目依赖 右边选择HelloCocos2dx项目-去除小面的勾。

选择解决方案下HelloCocos2dx(你的项目)右键->属性->通用属性-引用 移除中间的引用。如下图:

4. 在HelloCocos2dx(你的项目)属性->配置属性->上面选择所有配置->VC++目录 $(Configuration)表示Release和Debug

对于添加 $(COCOS_ROOT)\prebuild\win32和$(COCOS_ROOT)\prebuild\win32\$(Configuration) 如下图:

5.选择HelloCocos2dx(你的项目,不要选择libcocos2d项目了否则又要编译好久),右键-生成-运行 ,是不是飞一般的快呢!?

到这里可以选择libcocos2d项目,右键清理。

存在问题:EXE程序在加载动态库dll文件时会在当期目录和系统path环境变量里找,所以还要把在系统path后面添加 ;%COCOS_ROOT%\prebuild\win32

否则会出现dll文件没找到的错误

运行效果:

========================================================================================================

安卓部分 prebuild/android文件夹下面新建一个Android.mk内容

### COCOS 预编译静态库,确保环境变量有COCOS_ROOT或者项目里有

# 文件位置 $(COCOS_ROOT)/prebuild/android
LOCAL_PATH := $(call my-dir)

#$(call import-add-path,$(COCOS_ROOT))
$(call import-add-path,$(COCOS_ROOT)/external)
$(call import-add-path,$(COCOS_ROOT)/cocos)

#==============================================================
include $(CLEAR_VARS)

LOCAL_MODULE := cocos2dx_static
LOCAL_MODULE_FILENAME := libcocos2d

LOCAL_SRC_FILES := $(TARGET_ARCH_ABI)/libcocos2d.a
LOCAL_EXPORT_C_INCLUDES := \
	$(COCOS_ROOT)/cocos \
	$(COCOS_ROOT)/cocos/platform/android
LOCAL_STATIC_LIBRARIES := cocostudio_static
LOCAL_STATIC_LIBRARIES += cocosbuilder_static
LOCAL_STATIC_LIBRARIES += cocos3d_static
LOCAL_STATIC_LIBRARIES += spine_static
LOCAL_STATIC_LIBRARIES += cocos_network_static
LOCAL_STATIC_LIBRARIES += audioengine_static

include $(PREBUILT_STATIC_LIBRARY)
#include $(BUILD_STATIC_LIBRARY)

#==============================================================
include $(CLEAR_VARS)

LOCAL_MODULE := cocos_ui_static
LOCAL_MODULE_FILENAME := libui
LOCAL_SRC_FILES := $(TARGET_ARCH_ABI)/libui.a
LOCAL_EXPORT_C_INCLUDES := \
	$(COCOS_ROOT)/cocos/ui \
	$(COCOS_ROOT)/cocos/editor-support
LOCAL_C_INCLUDES := \
	$(COCOS_ROOT)/cocos/ui \
	$(COCOS_ROOT)/cocos/editor-support
LOCAL_STATIC_LIBRARIES := cocos_extension_static

include $(PREBUILT_STATIC_LIBRARY)
					
#==============================================================
include $(CLEAR_VARS)

LOCAL_MODULE := cocos_flatbuffers_static
LOCAL_MODULE_FILENAME := flatbuffers
LOCAL_SRC_FILES := $(TARGET_ARCH_ABI)/flatbuffers.a
LOCAL_EXPORT_C_INCLUDES := \
	$(COCOS_ROOT)/external

LOCAL_C_INCLUDES := \
	$(COCOS_ROOT)/external \
	$(COCOS_ROOT)/external/flatbuffers

include $(PREBUILT_STATIC_LIBRARY)
#==============================================================
include $(CLEAR_VARS)

LOCAL_MODULE := cocos_extension_static
LOCAL_MODULE_FILENAME := libextension
LOCAL_SRC_FILES := $(TARGET_ARCH_ABI)/libextension.a

LOCAL_STATIC_LIBRARIES := cocos2dx_internal_static
LOCAL_STATIC_LIBRARIES += cocos_curl_static
LOCAL_STATIC_LIBRARIES += Box2d_static
LOCAL_STATIC_LIBRARIES += bullet_static
LOCAL_STATIC_LIBRARIES += cocos_network_static

LOCAL_CXXFLAGS += -fexceptions
LOCAL_C_INCLUDES := \
	$(COCOS_ROOT) \
	$(COCOS_ROOT)/extensions
LOCAL_EXPORT_C_INCLUDES := \
	$(COCOS_ROOT) \
	$(COCOS_ROOT)/extensions \
	$(COCOS_ROOT)/extensions/GUI/CCControlExtension \
	$(COCOS_ROOT)/extensions/GUI/CCScrollView \
	$(COCOS_ROOT)/cocos/network
include $(PREBUILT_STATIC_LIBRARY)

#==============================================================
include $(CLEAR_VARS)

LOCAL_MODULE := cocos2dx_internal_static
LOCAL_MODULE_FILENAME := libcocos2dxinternal
LOCAL_SRC_FILES := $(TARGET_ARCH_ABI)/libcocos2dxinternal.a
LOCAL_EXPORT_C_INCLUDES := \
	$(COCOS_ROOT)/cocos \
	$(COCOS_ROOT) \
	$(COCOS_ROOT)/cocos/platform \
	$(COCOS_ROOT)/cocos/base \
	$(COCOS_ROOT)/external \
	$(COCOS_ROOT)/external/tinyxml2 \
	$(COCOS_ROOT)/external/unzip \
	$(COCOS_ROOT)/external/chipmunk/include/chipmunk \
	$(COCOS_ROOT)/external/xxhash \
	$(COCOS_ROOT)/external/nslog \
	$(COCOS_ROOT)/external/poly2tri \
	$(COCOS_ROOT)/external/poly2tri/common \
	$(COCOS_ROOT)/external/poly2tri/sweep \
	$(COCOS_ROOT)/external/clipper

LOCAL_C_INCLUDES := \
	$(COCOS_ROOT)/cocos/platform \
	$(COCOS_ROOT)/external \
	$(COCOS_ROOT)/external/tinyxml2 \
	$(COCOS_ROOT)/external/unzip \
	$(COCOS_ROOT)/external/chipmunk/include/chipmunk \
	$(COCOS_ROOT)/external/edtaa3func \
	$(COCOS_ROOT)/external/xxhash \
	$(COCOS_ROOT)/external/ConvertUTF \
	$(COCOS_ROOT)/external/nslog \
	$(COCOS_ROOT)/external/poly2tri \
	$(COCOS_ROOT)/external/poly2tri/common \
	$(COCOS_ROOT)/external/poly2tri/sweep \
	$(COCOS_ROOT)/external/clipper

LOCAL_EXPORT_LDLIBS := -lGLESv2 -llog -landroid

LOCAL_STATIC_LIBRARIES := cocos_freetype2_static
LOCAL_STATIC_LIBRARIES += cocos_png_static
LOCAL_STATIC_LIBRARIES += cocos_jpeg_static
LOCAL_STATIC_LIBRARIES += cocos_tiff_static
LOCAL_STATIC_LIBRARIES += cocos_webp_static
LOCAL_STATIC_LIBRARIES += cocos_chipmunk_static
LOCAL_STATIC_LIBRARIES += cocos_zlib_static
LOCAL_STATIC_LIBRARIES += recast_static
LOCAL_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 := cocostudio_static
LOCAL_MODULE_FILENAME := libcocostudio
LOCAL_SRC_FILES := $(TARGET_ARCH_ABI)/libcocostudio.a
LOCAL_EXPORT_C_INCLUDES := \
	$(COCOS_ROOT)/cocos/editor-support \
	$(COCOS_ROOT)/cocos/editor-support/cocostudio
LOCAL_C_INCLUDES := \
	$(COCOS_ROOT)/cocos/editor-support \
	$(COCOS_ROOT)/cocos/editor-support/cocostudio \
	$(COCOS_ROOT)/cocos/editor-support/cocostudio/WidgetReader \
	$(COCOS_ROOT)/cocos/editor-support/cocostudio/ActionTimeline \
LOCAL_STATIC_LIBRARIES := cocos_ui_static
LOCAL_STATIC_LIBRARIES += cocosdenshion_static
LOCAL_STATIC_LIBRARIES += cocos_flatbuffers_static

include $(PREBUILT_STATIC_LIBRARY)

#==============================================================
include $(CLEAR_VARS)

LOCAL_MODULE := cocosbuilder_static
LOCAL_MODULE_FILENAME := libcocosbuilder
LOCAL_SRC_FILES := $(TARGET_ARCH_ABI)/libcocosbuilder.a
LOCAL_EXPORT_C_INCLUDES := \
	$(COCOS_ROOT)/cocos/editor-support
LOCAL_C_INCLUDES := \
	$(COCOS_ROOT)/cocos/editor-support \
	$(COCOS_ROOT)/cocos/2d \
LOCAL_STATIC_LIBRARIES := cocos_extension_static

include $(PREBUILT_STATIC_LIBRARY)

#==============================================================
include $(CLEAR_VARS)

LOCAL_MODULE := spine_static
LOCAL_MODULE_FILENAME := libspine
LOCAL_SRC_FILES := $(TARGET_ARCH_ABI)/libspine.a
LOCAL_EXPORT_C_INCLUDES := \
	$(COCOS_ROOT)/cocos/editor-support
LOCAL_C_INCLUDES := \
	$(COCOS_ROOT)/cocos/editor-support
LOCAL_STATIC_LIBRARIES := cocos2dx_internal_static

include $(PREBUILT_STATIC_LIBRARY)

#==============================================================
include $(CLEAR_VARS)

LOCAL_MODULE := cocos3d_static
LOCAL_MODULE_FILENAME := libcocos3d
LOCAL_SRC_FILES := $(TARGET_ARCH_ABI)/libcocos3d.a
LOCAL_EXPORT_C_INCLUDES := \
	$(COCOS_ROOT)/cocos
LOCAL_C_INCLUDES := \
	$(COCOS_ROOT)/cocos
LOCAL_STATIC_LIBRARIES := cocos2dx_internal_static

include $(PREBUILT_STATIC_LIBRARY)

#==============================================================
include $(CLEAR_VARS)

LOCAL_MODULE := Box2d_static
LOCAL_MODULE_FILENAME := libBox2d
LOCAL_SRC_FILES := $(TARGET_ARCH_ABI)/libBox2d.a
LOCAL_EXPORT_C_INCLUDES := $(COCOS_ROOT)/external
LOCAL_C_INCLUDES := $(COCOS_ROOT)/external \
	$(COCOS_ROOT)/external/Box2D
include $(PREBUILT_STATIC_LIBRARY)

#==============================================================
include $(CLEAR_VARS)

LOCAL_MODULE := recast_static
LOCAL_MODULE_FILENAME := librecast
LOCAL_SRC_FILES := $(TARGET_ARCH_ABI)/librecast.a
LOCAL_EXPORT_C_INCLUDES := \
	$(COCOS_ROOT)/external
LOCAL_C_INCLUDES := \
	$(COCOS_ROOT)/external

include $(PREBUILT_STATIC_LIBRARY)

#==============================================================
include $(CLEAR_VARS)

LOCAL_MODULE := bullet_static
LOCAL_MODULE_FILENAME := libbullet
LOCAL_SRC_FILES := $(TARGET_ARCH_ABI)/libbullet.a
LOCAL_EXPORT_C_INCLUDES := \
	$(COCOS_ROOT)/external
LOCAL_C_INCLUDES := \
	$(COCOS_ROOT)/external

include $(PREBUILT_STATIC_LIBRARY)
#==============================================================
include $(CLEAR_VARS)

LOCAL_MODULE := cocos_network_static
LOCAL_MODULE_FILENAME := libnetwork
LOCAL_SRC_FILES := $(TARGET_ARCH_ABI)/libnetwork.a
LOCAL_EXPORT_C_INCLUDES :=

LOCAL_C_INCLUDES := \
	$(COCOS_ROOT)/external/curl/include/android \
	$(COCOS_ROOT)/external/websockets/include/android

LOCAL_STATIC_LIBRARIES := cocos2dx_internal_static
LOCAL_STATIC_LIBRARIES += cocos_curl_static
LOCAL_STATIC_LIBRARIES += libwebsockets_static

include $(PREBUILT_STATIC_LIBRARY)
#==============================================================
include $(CLEAR_VARS)

LOCAL_MODULE := cocos2dxandroid_static
LOCAL_MODULE_FILENAME := libcocos2dandroid
LOCAL_SRC_FILES := $(TARGET_ARCH_ABI)/libcocos2dandroid.a
LOCAL_EXPORT_C_INCLUDES := \
	$(COCOS_ROOT)/cocos/platform/android \
LOCAL_C_INCLUDES := \
	$(COCOS_ROOT)/cocos/platform/android \
	$(COCOS_ROOT)/cocos/platform \
	$(COCOS_ROOT)/cocos
LOCAL_EXPORT_LDLIBS := -lGLESv1_CM \
                       -lGLESv2 \
                       -lEGL \
                       -llog \
                       -landroid

LOCAL_CPPFLAGS := -Wno-extern-c-compat

LOCAL_EXPORT_CPPFLAGS := -Wno-extern-c-compat
include $(PREBUILT_STATIC_LIBRARY)
#========AudioEngine库加入失败因为本身存在编译警告========
#New AudioEngine
#include $(CLEAR_VARS)
#LOCAL_MODULE := audioengine_static

#LOCAL_MODULE_FILENAME := libaudioengine

#LOCAL_SRC_FILES := $(TARGET_ARCH_ABI)/libaudioengine.a

#LOCAL_EXPORT_C_INCLUDES := $(COCOS_ROOT)/cocos/audio/include
#LOCAL_EXPORT_LDLIBS := -lOpenSLES
#LOCAL_C_INCLUDES := $(COCOS_ROOT)/cocos/audio/include \
                    $(COCOS_ROOT)/cocos \
                    $(COCOS_ROOT)/cocos/audio/platform/android

#include $(PREBUILT_STATIC_LIBRARY)
#SimpleAudioEngine
#include $(CLEAR_VARS)

#LOCAL_MODULE := cocosdenshion_static
#LOCAL_MODULE_FILENAME := libcocosdenshion
#LOCAL_SRC_FILES := $(TARGET_ARCH_ABI)/libcocosdenshion.a
#LOCAL_STATIC_LIBRARIES := audioengine_static
#LOCAL_EXPORT_C_INCLUDES := $(COCOS_ROOT)/cocos/audio/include

#LOCAL_C_INCLUDES := $(COCOS_ROOT)/cocos/audio/include \
                    $(COCOS_ROOT)/cocos \
                    $(COCOS_ROOT)/cocos/audio/platform/android

#include $(PREBUILT_STATIC_LIBRARY)

#==============================================================
##以下是cocos编译好的静态库
#==============================================================
$(call import-module,freetype2/prebuilt/android)
$(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,curl/prebuilt/android)
$(call import-module,websockets/prebuilt/android)
#==============================================================
#AudioEngine在这里加入编译
$(call import-module,audio/android)

到这里就全部结束了,希望能帮到大家!!

猜你在找的Cocos2d-x相关文章