一、下载所需软件
Java SDK http://www.oracle.com/technetwork/java/javase/downloads/index.html Windows x64
Eclipse http://www.eclipse.org/downloads/packages/ Eclipse IDE for Java EE Developers Windows x64
Android SDK http://developer.android.com/sdk/index.html
Android NDK http://developer.android.com/tools/sdk/ndk/index.html
Python
Cocos2dx
二、配置环境变量
新建变量
NDK_ROOT =
ANDROID_SDK_ROOT =
JAVA_HOME =
CLASS_PATH = .;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar;
在系统的Path变量最后增加 ;%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin;;C:\Python27; 改为详细路径
PATHEXT中增加 .PY;
三、编译SO文件
解压Cocos2dx3.0。使用Tool中的create_project.py创建工程
改动project下的jni/Android.mk文件内容
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
#当前模块名
LOCAL_MODULE := cocos2dcpp_shared
#当前模块编译的文件名称
LOCAL_MODULE_FILENAME := libcocos2dcpp
# 开启C++11和多线程支持
LOCAL_CPPFLAGS:=-std=c++11 -pthread
# 遍历文件夹及子文件夹的函数
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)/%=%)
# 全部头文件文件夹(改动你自己的文件夹)
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes \
$(LOCAL_PATH)/../../Classes/Animation \
$(LOCAL_PATH)/../../Classes/Combat \
$(LOCAL_PATH)/../../Classes/GameEvent \
$(LOCAL_PATH)/../../Classes/GameState \
$(LOCAL_PATH)/../../Classes/LuaGameAPI \
$(LOCAL_PATH)/../../Classes/Map \
$(LOCAL_PATH)/../../Classes/PathSeeker \
$(LOCAL_PATH)/../../Classes/Role \
$(LOCAL_PATH)/../../cocos2d/external/lua/tolua \
$(LOCAL_PATH)/../../cocos2d/extensions \
$(LOCAL_PATH)/../../cocos2d/cocos/scripting/lua/bindings
LOCAL_STATIC_LIBRARIES := curl_static_prebuilt
# 引用的静态库
LOCAL_WHOLE_STATIC_LIBRARIES := cocos2dx_static
LOCAL_WHOLE_STATIC_LIBRARIES += cocosdenshion_static
LOCAL_WHOLE_STATIC_LIBRARIES += cocos_lua_static
include $(BUILD_SHARED_LIBRARY)
# 包括的模块文件(相应的文件夹名下有Android.mk)
$(call import-module,2d)
$(call import-module,audio/android)
$(call import-module,scripting/lua/bindings)
#导入自己的库(这个是server的网络库。路径我是放到了2d文件夹一个层级,然后自己写mk)
$(call import-module,protocol)
改动Application.mk
NDK_TOOLCHAIN_VERSION:=4.8
APP_STL := gnustl_static
# APP_STL := stlport_static
APP_CPPFLAGS := -frtti -DCC_ENABLE_CHIPMUNK_INTEGRATION=1 -DCOCOS2D_DEBUG=1 -std=c++11 -fsigned-char -fpermissive
APP_ABI := armeabi
用build_native.py编译so文件
期间改动错误
四、编译apk
打开Eclipse,Import项目下的proj.android和cocos2d/cocos/2d/platform/android/java
然后复制cocos2d3.0(注意,不是项目的。而是原始下载的cocos2d-x3.0文件夹,我也不知道为什么)下的
cocos\2d\platform\android\java\src\org\cocos2dx的lib目录
到我们project的
proj.android\src\org\cocos2dx下
然后在project属性Java Build Path中Libraries选项卡中删除多余的两项
编译。执行
五、注意事项
假设有Lua的话,须要注意下安卓和win32执行Lua的方式不同(坑!!
!)
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
CCString* pstrFileContent = CCString::createWithContentsOfFile("script/main.lua");
if (pstrFileContent)
{
pEngine->executeString(pstrFileContent->getCString());
}
#else
string path = FileUtils::getInstance()->fullPathForFilename("script/main.lua");
pEngine->addSearchPath(path.substr(0, path.find_last_of("/")).c_str());
string sub = path.substr(0, path.find_last_of("/")) + "/GameState";
pEngine->addSearchPath(sub.c_str());
pEngine->executeScriptFile(path.c_str());
#endif
并且在Lua中的require要写绝对路径
比方在asserts中有个scripts文件夹,里面有个Test.lua脚本
我们要在main.lua中
require "scripts/Test"
才行。包含绝对路径
另外,宏定义不能用## ,仅仅能用一个#