开发环境:win764位,android-ndk-r9,cocos2d-x-3.2,contrib-1.0b3,android-4.4
直接上代码
---------------------------------------------------- 目录初始化.... 生成R.java文件.... 编译aidl文件.... java源文件编译成class文件.... 将.class文件转化成.dex文件.... 将资源文件放进输出目录.... 编译native包...... 打包成未签证的apk.... Packaging signed apk for release... APK is released. path:${out-signed-package-ospath} 安装apk
local.properties文件
sdk.dir=D:\\devtools\\adt-bundle-windows-x86_64-20131030\\sdk ### 定义apk的包名 app_package=cn.hy.test ### 定义apk的启动入口Activity app_launch_activity=org.cocos2dx.cpp.AppActivity channels=11,22,33,44,55,66,77,88,99
在AndroidManifest.xml中加入
如果出现如下错误: *** Android NDK: Aborting . Stop.
将Application.mk中
APP_STL := c++_static NDK_TOOLCHAIN_VERSION=clang APP_CPPFLAGS := -frtti -DCC_ENABLE_CHIPMUNK_INTEGRATION=1 -std=c++11 -fsigned-char APP_LDFLAGS := -latomic
替换为:
APP_STL := gnustl_static APP_CPPFLAGS := -frtti -DCOCOS2D_DEBUG=1 -std=c++11 -Wno-literal-suffix -fsigned-char
如项目中使用到了cocostudio的导出文件,需要在Android.mk文件中的LOCAL_C_INCLUDES后面追加:
$(LOCAL_PATH)/../../cocos2d/cocos/editor-support/cocostudio \ $(LOCAL_PATH)/../../cocos2d/cocos/editor-support \ $(LOCAL_PATH)/../../cocos2d/extensions \ $(LOCAL_PATH)/../../cocos2d/cocos/ui \ $(LOCAL_PATH)/../../cocos2d/cocos
如果遇到下面的错误:
fatal error: json/document.h:
需要在Android.mk文件中的LOCAL_C_INCLUDES后面追加:
$(LOCAL_PATH)/../../cocos2d/external
如果遇到下面的错误:
ndk error: 'override' does not name a type
满屏都是这样的错误,'override'是c++11的新特性,可以判断gcc版本不支持c++11引起的问题,在build_native.py文件中有如下这段话:
'''Because ndk-r8e uses gcc4.6 as default. gcc4.6 doesn't support c++11. So we should select gcc4.7 when using ndk-r8e. But gcc4.7 is removed in ndk-r9, so we should determine whether gcc4.7 exist. Conclution: ndk-r8e -> use gcc4.7 ndk-r9 -> use gcc4.8 '''
因此要使用gcc4.8,需要在Application.mk(jni目录下)文件的最后加上:
NDK_TOOLCHAIN_VERSION = 4.8
如果在编译过程中遇到如下错误:
error: undefined reference to 'cocostudio
需要将Android.mk中注释掉的部分代码打开
# 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 #打开注释 ... # $(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) #打开注释
最终的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) LOCAL_MODULE := cocos2dcpp_shared LOCAL_MODULE_FILENAME := libcocos2dcpp LOCAL_SRC_FILES := hellocpp/main.cpp \ ../../Classes/AppDelegate.cpp \ ../../Classes/HelloWorldScene.cpp LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes \ $(LOCAL_PATH)/../../cocos2d/cocos/editor-support/cocostudio \ $(LOCAL_PATH)/../../cocos2d/cocos/editor-support \ $(LOCAL_PATH)/../../cocos2d/extensions \ $(LOCAL_PATH)/../../cocos2d/cocos/ui \ $(LOCAL_PATH)/../../cocos2d/cocos \ $(LOCAL_PATH)/../../cocos2d/external 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)
为了将apk安装到genymotion上,需要在Application.mk文件中最后一行追加
APP_ABI := armeabi armeabi-v7a x86
这句话的意思是打成x86架构的apk包,因为genymotion使用virtualbox模拟android设备。
最终的Application.mk文件内容如下:
APP_STL := gnustl_static APP_CPPFLAGS := -frtti -DCOCOS2D_DEBUG=1 -std=c++11 -Wno-literal-suffix -fsigned-char APP_DEBUG := $(strip $(NDK_DEBUG)) ifeq ($(APP_DEBUG),1) APP_CPPFLAGS += -DCOCOS2D_DEBUG=1 APP_OPTIM := debug else APP_CPPFLAGS += -DNDEBUG APP_OPTIM := release endif NDK_TOOLCHAIN_VERSION = 4.8 APP_ABI := armeabi armeabi-v7a x86
另外,在使用Genymotion的时候,如果出现“INSTALL_FAILED_CPU_ABI_INCOMPATIBLE”错误,下载Genymotion-ARM-Translation.zip,直接拖到模拟器界面安装即可。