jni cocos2d-x移植到android:helloworld
前面搭建好了android开发环境
jni开发环境,那么就来用一用目前火热的cocos2d的游戏引擎吧
所需资料:
我下载的是较老的版本 cocos2d-1.0.1-x-0.11.0。如果你和我一样,也是第一次,建议也下这个版本
ndk版本使用 r7 的。
这样之后才会遇到较少的问题
下载完,解压 cocos2d。
进入目录下修改 create-android-project.bat 这个文件
@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 <SPAN style="COLOR: #ff0000">set _CYGBIN=d:\cygwin\bin</SPAN> if not exist "%_CYGBIN%" echo Couldn't find Cygwin at "%_CYGBIN%" & pause & exit 4 :: modify it to work under your environment <SPAN style="COLOR: #ff0000">set _ANDROIDTOOLS=d:\program files\easipass\android-sdk_r20.0.3-windows-all\android-sdk-windows\tools</SPAN> if not exist "%_ANDROIDTOOLS%" echo Couldn't find android sdk tools at "%_ANDROIDTOOLS%" & pause & exit 5 :: modify it to work under your environment <SPAN style="COLOR: #ff0000">set _NDKROOT=c:\android-ndk-r7b\android-ndk-r7b</SPAN> 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" pause @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=d:\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=d:\program files\easipass\android-sdk_r20.0.3-windows-all\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=c:\android-ndk-r7b\android-ndk-r7b 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" pause
修改红色字体部分为你对应的安装目录。
ok,接下来搞个helloworld吧。
哦,对了,这个需要有一个NDK_ROOT的环境变量,自己添加一下吧,不知道就百度哦。
添加完环境变量,重启计算机。
打开create-android-project.bat文件
按照提示进行输入:
第一步输入你工程包名:com.android.cocos2d.test(可以自己输入哦)
第二步输入工程名称:test
第三步窗口会列出所有可以选择的android版本,自己选择一个输入id号码 ,我输入 7 对应 android的2.3.3版本的
然后生成项目就ok了,可以看到当前目录下多了一个test目录,这个就是工程目录了,没看到的话刷新一下。
接下来就是编译,需要将代码编译成可以调用的so链接库
打开cygwin
进入生成的项目的android目录
执行build_native.sh这个shell脚本。就开始编译了。需要一点时间
如果你使用的不是我所说的对应的版本,那么可能这里会遇到形形易做图的问题,也欢迎把问题贴给我,我可能可以替你解决。但还是建议百度,这样速度来的比较块一点。
补充:移动开发 , 其他 ,