今天对cocos2d-x编译成android做个笔记,本文使用的是2.2.1版本。这里指记录如何编译。本文主要参考http://blog.csdn.net/sttyytw/article/details/17005263

1、创建通过python来创建项目

python create_project.py -project helloworld -package com.xzw.game -language cpp

 另外提供批处理,方便下次创建:


@echo off
 :label1
 @cls
 echo 欢迎使用Python创建Cocos2d-x工程
 set /p project=请输入需要创建的工程名:
 set /p aID=请输入需要创建的android版本包标识名:
 echo 您输入的工程名为%project%
 echo 您输入的android版本包标识名为%aID%
 echo 确认创建工程吗?
 CHOICE /C 123 /M "确认请按 1,取消请按 2,或者退出请按 3。"
 echo %errorlevel%
 if %errorlevel%==1 goto label2
 if %errorlevel%==2 goto label1
 if %errorlevel%==3 goto label3
 :label2
 echo 正在创建工程...
 python create_project.py -project %project% -package %aID% -language cpp
 :label3
 pause

COCOS2D-X编译成android的项目_android


创建好后会在cocos2d-x-2.2.1\projects目录下。

2、使用cygwin进入proj.android目录下,然后执行./build_native.sh就可以进行编译了。

COCOS2D-X编译成android的项目_cocos2d-x_02

前提是需要对build_natvie.sh进行修改,这样才能够自动将所需要cocos2d-x的类加到工程中。

首先在cygwin中添加cocos2d-x的配置

COCOS2DX_DIR=/cygdrive/e/resource/cocos2d-x/cocos2d-x-2.2.1/cocos2d-x-2.2.1
export COCOS2DX_DIR

然后修改cocos2d-x-2.2.1\template\multi-platform-cpp\proj.android下的build_native.sh文件进行修改。如下图:

COCOS2D-X编译成android的项目_编译_03

APPNAME="HelloCpp"
# options
buildexternalsfromsource=
usage(){
cat << EOF
usage: $0 [options]
Build C/C++ code for $APPNAME using Android NDK
OPTIONS:
-s  Build externals from source
-h  this help
EOF
}
while getopts "sh" OPTION; do
case "$OPTION" in
s)
buildexternalsfromsource=1
;;
h)
usage
exit 0
;;
esac
done
# paths
if [ -z "${NDK_ROOT+aaa}" ];then
echo "please define NDK_ROOT"
exit 1
fi
if [ -z "${COCOS2DX_DIR+aaa}" ];then
echo "please define COCOS2DX_DIR"
exit 1
fi
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# ... use paths relative to current directory
COCOS2DX_ROOT="$DIR/../../.."
APP_ROOT="$DIR/.."
APP_ANDROID_ROOT="$DIR"
echo "COCOS2DX_DIR = $COCOS2DX_DIR"
echo "NDK_ROOT = $NDK_ROOT"
echo "COCOS2DX_ROOT = $COCOS2DX_ROOT"
echo "APP_ROOT = $APP_ROOT"
echo "APP_ANDROID_ROOT = $APP_ANDROID_ROOT"
# make sure assets is exist
if [ -d "$APP_ANDROID_ROOT"/assets ]; then
    rm -rf "$APP_ANDROID_ROOT"/assets
fi
mkdir "$APP_ANDROID_ROOT"/assets
# copy resources
for file in "$APP_ROOT"/Resources/*
do
if [ -d "$file" ]; then
    cp -rf "$file" "$APP_ANDROID_ROOT"/assets
fi
if [ -f "$file" ]; then
    cp "$file" "$APP_ANDROID_ROOT"/assets
fi
done
for file in "$COCOS2DX_DIR"/cocos2dx/platform/android/java/src/*
do
if [ -d "$file" ]; then
    cp -rf "$file" "$APP_ANDROID_ROOT"/src
fi
if [ -f "$file" ]; then
    cp "$file" "$APP_ANDROID_ROOT"/src
fi
done
# run ndk-build
if [[ "$buildexternalsfromsource" ]]; then
    echo "Building external dependencies from source"
    "$NDK_ROOT"/ndk-build -C "$APP_ANDROID_ROOT" $* \
        "NDK_MODULE_PATH=${COCOS2DX_ROOT}:${COCOS2DX_ROOT}/cocos2dx/platform/third_party/android/source"
else
    echo "Using prebuilt externals"
    "$NDK_ROOT"/ndk-build -C "$APP_ANDROID_ROOT" $* \
        "NDK_MODULE_PATH=${COCOS2DX_ROOT}:${COCOS2DX_ROOT}/cocos2dx/platform/third_party/android/prebuilt"
fi


3、eclipse导入比运行

COCOS2D-X编译成android的项目_android_04

COCOS2D-X编译成android的项目_cocos2d-x_05

运行即可。就这么就记录了。