修改cocos2d-x 3.3 android mk文件 为 自动遍历*.cpp文件,亲测~~
我们在自己新建的项目中,比如项目Mycpp,在项目Mycpp\proj.android\jni的路径下找到android mk 文件,先来看看这个android.mk文件的内容,打开之后是这样子的,我们没有改过
LOCAL_PATH := $(call my-dir) 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) 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 LOCAL_WHOLE_STATIC_LIBRARIES := cocos2dx_static LOCAL_WHOLE_STATIC_LIBRARIES += cocosdenshion_static # LOCAL_WHOLE_STATIC_LIBRARIES += Box2d_static # LOCAL_WHOLE_STATIC_LIBRARIES += cocosbuilder_static # LOCAL_WHOLE_STATIC_LIBRARIES += spine_static # LOCAL_WHOLE_STATIC_LIBRARIES += cocostudio_static # 添加 cocostudio 静态库 # LOCAL_WHOLE_STATIC_LIBRARIES += cocos_network_static # LOCAL_WHOLE_STATIC_LIBRARIES += cocos_extension_static # 添加 cocos_extension 静态库 include $(BUILD_SHARED_LIBRARY) $(call import-module,.) $(call import-module,audio/android) # $(call import-module,Box2D) # $(call import-module,editor-support/cocosbuilder) # $(call import-module,editor-support/spine) # $(call import-module,editor-support/cocostudio) # 导入 cocostudio 模块 # $(call import-module,network) # $(call import-module,extensions) # 导入 extensions 模块
上面这段代码就是新建项目之后,原装的代码 其中 “#” 是注释
如果我们在自己的cpp项目中有用到这两个库的话,cocostudio,extensions,只需要去掉android.mk文件中的注释号“#”,虽然cocos2dx 的团队已经提醒了我们,只不过在前面加了“#”注释号,就是上面有注释的几行代码,不过还是需要注意的是,如果在cpp项目中要添加头文件的话,需要这样子写,
#include "extensions/cocos-ext.h" #include "cocostudio/CocoStudio.h
这样在编译android项目的时候,才不会报错
如果写成下面这个的话,win32环境下虽然能编译通过,但是在android环境下是编译不通过的,要注意
#include "cocos-ext.h" #include "CocoStudio.h
LOCAL_SRC_FILES := hellocpp/main.cpp \ ../../Classes/AppDelegate.cpp \ ../../Classes/HelloWorldScene.cpp
改为: @H_403_72@
@H_403_72@
# 遍历目录及子目录的函数 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)) LOCAL_SRC_FILES := $(FILE_LIST:$(LOCAL_PATH)/%=%)
其他部分不变。然后就可以了。。
改完之后完整android mk 文件如下:
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) $(call import-add-path,$(LOCAL_PATH)/../../cocos2d/cocos) LOCAL_MODULE := cocos2dcpp_shared LOCAL_MODULE_FILENAME := libcocos2dcpp # 遍历目录及子目录的函数 define walk $(wildcard $(1)) $(foreach e,$(ALLFILES)) LOCAL_SRC_FILES := $(FILE_LIST:$(LOCAL_PATH)/%=%) LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes LOCAL_WHOLE_STATIC_LIBRARIES := cocos2dx_static LOCAL_WHOLE_STATIC_LIBRARIES += cocosdenshion_static LOCAL_WHOLE_STATIC_LIBRARIES += Box2d_static LOCAL_WHOLE_STATIC_LIBRARIES += cocosbuilder_static LOCAL_WHOLE_STATIC_LIBRARIES += spine_static LOCAL_WHOLE_STATIC_LIBRARIES += cocostudio_static # 添加 cocostudio 静态库 LOCAL_WHOLE_STATIC_LIBRARIES += cocos_network_static LOCAL_WHOLE_STATIC_LIBRARIES += cocos_extension_static # 添加 cocos_extension 静态库 include $(BUILD_SHARED_LIBRARY) $(call import-module,audio/android) $(call import-module,Box2D) $(call import-module,editor-support/cocosbuilder) $(call import-module,editor-support/spine) $(call import-module,editor-support/cocostudio) # 导入 cocostudio 模块 $(call import-module,network) $(call import-module,extensions) # 导入 extensions 模块
然后就可以编译通过了。。
Android.mk文件
基本语法:
“#” 注释
“:=” 赋值
“+=” 追加
“$” 引用某变量的值
#宏函数my-dir返回编译系统提供的当前路径,即Android.mk的目录 #如(HelloWorld/proj.android/jni/XX) LOCAL_PATH := $(call my-dir) #初始化模块 重置除LOCAL_PATH变量以外的所有变量 include $(CLEAR_VARS) #导入库目录 $(call import-add-path,$(LOCAL_PATH)/../../cocos2d/cocos) #模块名,唯一。 LOCAL_MODULE := cocos2dcpp_shared #模块文件名,唯一(可选) LOCAL_MODULE_FILENAME := libcocos2dcpp #配置头文件的搜索范围 LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes #获取静态库 LOCAL_WHOLE_STATIC_LIBRARIES := cocos2dx_static #追加静态库 LOCAL_WHOLE_STATIC_LIBRARIES += cocosdenshion_static #根据编译器提供的变量BUILD_SHARED_LIBRARY生成动态库 include $(BUILD_SHARED_LIBRARY) #导入模块,以下是常用的几个模块 $(call import-module,audio/android) $(call import-module,editor-support/cocostudio) $(call import-module,extensions)原文链接:/cocos2dx/344988.html