编译至Android平台时,若手动修改需编译的源文件及头文件目录,那会比较麻烦。还有就是若直接在Android.mk里面写自动搜索的话,需要Linux环境(或Windows上的Cygwin)才能正确运行。所以这里写了一个Python脚本,直接放在jni文件夹下运行即可生成Android.mk。生成的Android.mk文件内容如下图:
Python脚本:
import osfrom sets import Set
text = """LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)$(call import-add-path,$(LOCAL_PATH)/../../cocos2d)LOCAL_MODULE := cocos2dcpp_sharedLOCAL_MODULE_FILENAME := libcocos2dcppLOCAL_SRC_FILES := hellocpp/main.cpp"""includeSet = Set()def findall(path):global textdirList = os.listdir(path)for dirName in dirList:if os.path.isfile(path + "/" + dirName):if dirName[-2:]==".h":includeSet.add(path)elif dirName[-4:]==".cpp" or dirName[-2:]==".c":text += " \\\n" + path + "/" + dirNameelse:
findall(path+"/"+dirName)findall("../../Classes")text += "\n\n"
text += "LOCAL_C_INCLUDES :="
for include in includeSet:text += " \\\n$(LOCAL_PATH)/" + include
text += """LOCAL_STATIC_LIBRARIES := cocos2dx_staticLOCAL_LDLIBS += -L$(call host-path,$(NDK_ROOT)/sources/cxx-stl/llvm-libc++/libs/$(TARGET_ARCH_ABI)) -lc++_static
include $(BUILD_SHARED_LIBRARY)$(call import-module,./prebuilt-mk)"""f = open('Android.mk','w')
f.write(text)
f.close()