1.到项目文件的proj.android目录下,编辑build_native.sh文件,指定NDK和cocos2dx路径
export NDK_ROOT=/home/liyuchen/dev/android-ndk-r9d export NDK_MODULE_PATH=/home/liyuchen/dev/cocos2d-x-2.2.1/cocos2dx
2.执行脚本
./build_native.sh此处报错多与ndk版本有关
3.在eclipse中(已经配置好android sdk和ndk)中import,Android下的Existing Android Code into Workspace
通常到达这一步会报错,如果是缺少源文件,请到cocos2d-x-2.2.1/cocos2dx/platform/android/java/src目录下的全部文件导入到eclipse工程中的src下
4.修改jni/Android.mk文件
如果新建的工程不需要修改,跳过此步。如果已有的工程要移植到安卓,将新建的源文件添加到Android.mk文件中,这个文件类似于makefile文件,例如在Classes目录下新增了MainScene.cpp,那么需要做如下修改(红色部分为修改)
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := cocos2dcpp_shared
LOCAL_MODULE_FILENAME := libcocos2dcpp
LOCAL_SRC_FILES := hellocpp/main.cpp ../../Classes/AppDelegate.cpp ../../Classes/HelloWorldScene.cpp ../../Classes/MainScene.cpp
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes
LOCAL_WHOLE_STATIC_LIBRARIES += cocos2dx_static
LOCAL_WHOLE_STATIC_LIBRARIES += cocosdenshion_static
LOCAL_WHOLE_STATIC_LIBRARIES += box2d_static
LOCAL_WHOLE_STATIC_LIBRARIES += chipmunk_static
LOCAL_WHOLE_STATIC_LIBRARIES += cocos_extension_static
include $(BUILD_SHARED_LIBRARY)
$(call import-module,cocos2dx)
$(call import-module,cocos2dx/platform/third_party/android/prebuilt/libcurl)
$(call import-module,CocosDenshion/android)
$(call import-module,extensions)
$(call import-module,external/Box2D)
$(call import-module,external/chipmunk)
5.修改AndroidManifest文件,将横屏改为竖屏,此步可以忽略,视情况而定。
android:screenOrientation="landscape"修改为 android:screenOrientation="portrait"
如果运行时jni目录下的Android.mk文件报错,这个文件修改起来较麻烦,错误
大多与ndk的版本有关,我在换成了ndk9d的版本后问题解决