·Eclipse工程
在项目 proj.android\jni 目录下的 Android.mk 文件中,需要对 LOCAL_SRC_FILES 进行赋值,将项目中用到的cpp文件都写入,用模板创建的项目此段内容如下
1 LOCAL_SRC_FILES := hellocpp/main.cpp \ 2 ../../Classes/AppDelegate.cpp \ 3 ../../Classes/HelloWorldScene.cpp
这样写很麻烦,每增加一个cpp就要添加一行。使用通配符(wildcard)的方式就可以解决
1 SRC_FILES := hellocpp/main.cpp 2 SRC_FILES += $(wildcard $(LOCAL_PATH)/../../Classes/*.cpp) 3 SRC_FILES += $(wildcard $(LOCAL_PATH)/../../Classes/*/*.cpp) 4 SRC_FILES += $(wildcard $(LOCAL_PATH)/../../Classes/*/*/*.cpp) 5 LOCAL_SRC_FILES := $(SRC_FILES:$(LOCAL_PATH)/%=%)
上面相当于搜索Classes目录下的3目录中所有的cpp文件
以上写法参考了 http://blog.163.com/long_wtf/blog/static/18555327020149144495734/
cocos run -p android 即可编译运行出android项目,如果代码没有错误并且android设备已经处于连接状态,则会直接编译完并运行程序在设备上。
----------------------------------------------------------------------------------------------------------------------------------------------
·Android-studio工程
在项目 proj.android-studio中,由于jni文件夹放到了proj.android-studio\app中,因此要在上面的模板上多加一层回退 /.. 。
因此最后的模板如下:
1 LOCAL_PATH := $(call my-dir) 2 3 include $(CLEAR_VARS) 4 5 $(call import-add-path,$(LOCAL_PATH)/../../../cocos2d) 6 $(call import-add-path,$(LOCAL_PATH)/../../../cocos2d/external) 7 $(call import-add-path,$(LOCAL_PATH)/../../../cocos2d/cocos) 8 9 LOCAL_MODULE := cocos2dcpp_shared 10 11 LOCAL_MODULE_FILENAME := libcocos2dcpp 12 13 SRC_FILES := hellocpp/main.cpp 14 SRC_FILES += $(wildcard $(LOCAL_PATH)/../../../Classes/*.cpp) 15 SRC_FILES += $(wildcard $(LOCAL_PATH)/../../../Classes/*/*.cpp) 16 LOCAL_SRC_FILES := $(SRC_FILES:$(LOCAL_PATH)/%=%) 17 18 LOCAL_CFLAGS := -DUSE_FILE32API -DGL_GLEXT_PROTOTYPES=1 19 20 LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../../Classes 21 22 # _COCOS_HEADER_ANDROID_BEGIN 23 # _COCOS_HEADER_ANDROID_END 24 25 26 LOCAL_STATIC_LIBRARIES := cocos2dx_static 27 28 # _COCOS_LIB_ANDROID_BEGIN 29 # _COCOS_LIB_ANDROID_END 30 31 include $(BUILD_SHARED_LIBRARY) 32 33 $(call import-module,.) 34 35 # _COCOS_LIB_IMPORT_ANDROID_BEGIN 36 # _COCOS_LIB_IMPORT_ANDROID_END
编译时,输入命令为 cocos run -p android --android-studio 。