转自:http://www.2cto.com/kf/201205/130697.html
下面我给大家介绍下,用vs2010开发cocos2dx,然后如何使其编译到android上。步骤如下:
1、必要条件,你的eclipse能把代码编译到安卓手机或虚拟机上,如果这一步还没做好,先用谷歌百度查查看,然后把环境配置好。【我sdk的目录是:E:\android\android-sdk-windows】
2、下载cygwin并安装。这个也请到网上看看教程,转好的cygwin一定要保证能用gcc、g++,make,vi等命令。我安装的目录是:E:\android\cygwin
3、下载ndk。如果ndk还不知道什么意思的话,也请先一步谷歌百度。ndk你下载解压放在一个地方就好了。我解压后的目录是:E:\android\android-ndk-r7
4、下载最新版的cocos2dx。我将cocos2dx解压在E:\android\cocos2d-1.0.1-x-0.13.0-beta。在这个目录下找到:create-android-project.bat然后用cygwin进行编辑,我create-android-project.bat修改后的效果如下:
[plain]
@echo off
:: This script is used to create an android project.
:: You should modify _ANDROIDTOOLS _CYGBIN _NDKROOT to work under your environment.
:: Don't change it until you know what you do.
setlocal
:: Check if it was run under cocos2d-x root
if not exist "%cd%\create-android-project.bat" echo Error!!! You should run it under cocos2dx root & pause & exit 2
if not exist "%~dpn0.sh" echo Script "%~dpn0.sh" not found & pause & exit 3
:: modify it to work under your environment
set _CYGBIN=E:\android\cygwin\bin
if not exist "%_CYGBIN%" echo Couldn't find Cygwin at "%_CYGBIN%" & pause & exit 4
:: modify it to work under your environment
set _ANDROIDTOOLS=E:\android\android-sdk-windows\tools
if not exist "%_ANDROIDTOOLS%" echo Couldn't find android sdk tools at "%_ANDROIDTOOLS%" & pause & exit 5
:: modify it to work under your environment
set _NDKROOT=E:\android\android-ndk-r7
if not exist "%_NDKROOT%" echo Couldn't find ndk at "%_NDKROOT%" & pause & exit 6
:: create android project
set /P _PACKAGEPATH=Please enter your package path. For example: org.cocos2dx.example:
set /P _PROJECTNAME=Please enter your project name:
if exist "%CD%\%_PROJECTNAME%" echo "%_PROJECTNAME%" exists, please use another name & pause & exit 7
echo "Now cocos2d-x suppurts Android 2.1-update1, 2.2, 2.3 & 3.0"
echo "Other versions have not tested."
call "%_ANDROIDTOOLS%\android.bat" list targets
set /P _TARGETID=Please input target id:
set _PROJECTDIR=%CD%\%_PROJECTNAME%
echo Create android project
call "%_ANDROIDTOOLS%\android.bat" create project -n %_PROJECTNAME% -t %_TARGETID% -k %_PACKAGEPATH% -a %_PROJECTNAME% -p %_PROJECTDIR%
:: Resolve ___.sh to /cygdrive based *nix path and store in %_CYGSCRIPT%
for /f "delims=" %%A in ('%_CYGBIN%\cygpath.exe "%~dpn0.sh"') do set _CYGSCRIPT=%%A
:: Resolve current dir to cygwin path
for /f "delims=" %%A in ('%_CYGBIN%\cygpath.exe "%cd%"') do set _CURRENTDIR=%%A
:: Resolve ndk dir to cygwin path
for /f "delims=" %%A in ('%_CYGBIN%\cygpath.exe "%_NDKROOT%"') do set _NDKROOT=%%A
:: Throw away temporary env vars and invoke script, passing any args that were passed to us
endlocal & %_CYGBIN%\bash --login "%_CYGSCRIPT%" %_CURRENTDIR% %_PROJECTNAME% %_NDKROOT% %_PACKAGEPATH% "windows" -l -b
pause
修改的地方有三个:A: set _ANDROIDTOOLS=E:\android\android-sdk-windows\tools
B: set _NDKROOT=E:\android\android-ndk-r7
C:set _CYGBIN=E:\android\cygwin\bin
这三个地方修改成你自己安装的目录就好了。
5、以上几步是确保你eclipse环境配置完成,下面进入正题。你先把你的cocos2dx项目在vs2010上运行。
6、在cygwin上,进入cocos2dx,输入以下命令:
接着,回车,你将看到下面的信息,并输入:
回车,显示下面信息,并请输入:
回车,请稍等,你将看到下面的信息:
接着输入你想要创建android的版本,加入你想创建Android2.3.3,那么输入15,如下图:
回车,cygwin会进行一些处理,然后cocos2dx项目创建完毕。
打开cocos2dx文件夹,你将看到你刚创建的文件夹,文件夹里有以下文件夹:
7、这一步,请将你用vs2010编译的项目中的Classes下的文件拷贝到Classes,同理,vs2010下的Resource里的文件拷贝到Resource下
8、打开我们刚才生产的teachYou,修改\android\jni下的Android.mk文件。只把你的.cpp文件加进去就行了。
9、打开cygwin,把\android下的build_native.sh拖到cywin回车,编译就行了。
10、然后用eclipse将文件夹android import进去就可以了