首页 > 代码库 > 学习笔记 Android.mk 自动搜索

学习笔记 Android.mk 自动搜索

最近被Android.mk这东西折腾的一晚,这里记录一下。ios就省心多了,不用管那么多,xcode自己解决了。文本工具:MACVIM(文本编辑工具 非常好用 能清楚分清tab 和空格,以免编写.mk时出错)这个文件是向编译器描述源代码文件,便于它去解析。以下是cocos2dx-3.2新建项目时生成的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)
$(call import-add-path,$(LOCAL_PATH)/../../cocos2d/external)
$(call import-add-path,$(LOCAL_PATH)/../../cocos2d/cocos)


#模块名,唯一。
LOCAL_MODULE := cocos2dcpp_shared
#模块文件名,唯一(可选)
LOCAL_MODULE_FILENAME := libcocos2dcpp?
#把.cpp .c .cc等实现文件的路径赋值给LOCAL_SRC_FILES
我用的是C++版  所以后缀是.cpp 我们新建文件时,需要在这里添加路径。
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
# LOCAL_WHOLE_STATIC_LIBRARIES += cocos_network_static
# LOCAL_WHOLE_STATIC_LIBRARIES += cocos_extension_static


#根据编译器提供的变量BUILD_SHARED_LIBRARY生成动态库
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)
# $(call import-module,network)
# $(call import-module,extensions)


如果文件少  直接在LOCAL_SRC_FILES这个变量里添加文件的路径就好了
可惜我有几十个文件,一个个添加还不累死,而且难维护,难扩展,易出错,。。。


我是个懒人,参考子龙山人的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
#widcard为扩展通配符
#遍历目录和子目录
define walk
 $(wildcard $(1)) $(foreach e, $(wildcard $(1)/*), $(call walk, $(e)))
endef
#遍历路径Classes下得目录和子目录文件,并存入ALLFILES变量中
ALLFILES = $(call walk, $(LOCAL_PATH)/../../Classes)
#从ALLFILES目录中提取文件,并存入FILE_LIST中
#.cc是Linux/Unix下为C++源文件的默认扩展名,与.cpp一个意思
#.c 因为我要用到sqlite3.c  
FILE_LIST := hellocpp/main.cpp
FILE_LIST += $(filter %.cpp, $(ALLFILES))
FILE_LIST += $(filter %.c, $(ALLFILES))
FILE_LIST += $(filter %.cc, $(ALLFILES))


#搜索Classes下的子目录文件
FILE_INCLUDES += $(shell find $(LOCAL_PATH)/../../Classes -type d)
#根据搜索到的文件名 编译资源文件
LOCAL_SRC_FILES := $(FILE_LIST:$(LOCAL_PATH)/%=%)
#提供搜索文件的路径
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes 	$(FILE_INCLUDES) #静态库
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
LOCAL_WHOLE_STATIC_LIBRARIES += cocos_network_static
LOCAL_WHOLE_STATIC_LIBRARIES += cocos_extension_static


#编译动态库
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)
$(call import-module,network)
$(call import-module,extensions)


吐槽:这些零碎的东西应该是被引擎或者开发软件所解决的。