因为业务需要,公司原来使用eclipes作为Android工程输出的项目,需要转到Andiro Studio(后面简称AS)环境下。在折腾了2周失败后,在一个之前从没有用过AS的大神帮助下,帮我在AS下成功转移了环境,并出了可以测试的包……
先说一下项目环境:应该是Cocos2dx 3.4以后的版本,大神说我们的项目当时修改了Cosos的引擎,具体是哪个版本他记不清了……我拿到的工程目录大致是这样的。
只有eclipes用的proj.android目录和Viso Studio编辑的Win32工程目录,外加游戏项目的主要代码:Classes目录和资源Resources目录。
开干!
首先用3.13的cocos2dx新建一个Cocos2D工程,比如在D:work2目录下,按住Shift键,点击鼠标右键,选择在此处打开Powershe窗口(如果没有此选项的,可以选择:在此处打开命令窗口)
输入新建工程的命令,如果不能执行,请自行寻找如何配置Cocos环境的教程
D:\work2> cocos new mygame -p com.funland.magame -l cpp
新建的Cocos工程目录结构如下:
把原来工程的5个目录:
\Classes
\cocos2d
\proj.android
\proj.win32
\Resources
拷贝并覆盖到新建工程里。
接下来要检查和修改两个重要的配置文件Android.mk,这里提供了2个mk文件的下载。注意,从下面复制代码进入mk文件,有可能会导致编译失败!
先看D:\work2\mygame\proj.android\jni\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)
$(call import-add-path,$(LOCAL_PATH)/../../cocos2d/cocos/audio/include)
LOCAL_MODULE := MyGame_shared
LOCAL_MODULE_FILENAME := libMyGame
#traverse all the directory and subdirectory
define walk
$(wildcard $(1)) $(foreach e, $(wildcard $(1)/*), $(call walk, $(e)))
endef
#traverse Classes Directory
ALLFILES = $(call walk, $(LOCAL_PATH)/../../Classes)
FILE_LIST := hellocpp/main.cpp
FILE_LIST += $(filter %.cpp, $(ALLFILES))
FILE_LIST += $(filter %.c, $(ALLFILES))
FILE_INCLUDES := $(shell find $(LOCAL_PATH)/../../Classes -type d)
#source file will be compiled
LOCAL_SRC_FILES := $(FILE_LIST:$(LOCAL_PATH)/%=%) \
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes \
$(LOCAL_PATH)/../../cocos2d/cocos/storage/local-storage
# _COCOS_HEADER_ANDROID_BEGIN
# _COCOS_HEADER_ANDROID_END
LOCAL_STATIC_LIBRARIES := cocos2dx_static
LOCAL_STATIC_LIBRARIES += cocos_localstorage_static
LOCAL_STATIC_LIBRARIES += cocos2d_lua_static
# _COCOS_LIB_ANDROID_BEGIN
# _COCOS_LIB_ANDROID_END
include $(BUILD_SHARED_LIBRARY)
$(call import-module,.)
$(call import-module,storage/local-storage)
$(call import-module,scripting/lua-bindings/proj.android)
# _COCOS_LIB_IMPORT_ANDROID_BEGIN
# _COCOS_LIB_IMPORT_ANDROID_END
再看: D:\work2\mygame\proj.android-studio\app\jni\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)
$(call import-add-path,$(LOCAL_PATH)/../../../cocos2d/cocos/audio/include)
LOCAL_MODULE := MyGame_shared
LOCAL_MODULE_FILENAME := libMyGame
L