Porting jni cocos2d-x to android: helloworld

Source: Internet
Author: User

The android development environment has been set up.

In the jni development environment, use the currently popular cocos2d game engine.

 


Required information:

I downloaded an older version of cocos2d-1.0.1-x-0.11.0. If you are the same as me, this version is also the first time.

The ndk version uses r7.

 


In this way, fewer problems will be encountered.

 


After downloading, decompress cocos2d.

Go to the directory to modify the create-android-project.bat File

 

@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 rootif 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\binif not exist "%_CYGBIN%" echo Couldn't find Cygwin at "%_CYGBIN%" & pause & exit 4:: modify it to work under your environmentset _ANDROIDTOOLS=d:\program files\easipass\android-sdk_r20.0.3-windows-all\android-sdk-windows\toolsif not exist "%_ANDROIDTOOLS%" echo Couldn't find android sdk tools at "%_ANDROIDTOOLS%" & pause & exit 5:: modify it to work under your environmentset _NDKROOT=c:\android-ndk-r7b\android-ndk-r7bif not exist "%_NDKROOT%" echo Couldn't find ndk at "%_NDKROOT%" & pause & exit 6:: create android projectset /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 7echo "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 targetsset /P _TARGETID=Please input target id:set _PROJECTDIR=%CD%\%_PROJECTNAME%echo Create android projectcall "%_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 pathfor /f "delims=" %%A in ('%_CYGBIN%\cygpath.exe "%cd%"') do set _CURRENTDIR=%%A:: Resolve ndk dir to cygwin pathfor /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 usendlocal & %_CYGBIN%\bash --login "%_CYGSCRIPT%" %_CURRENTDIR% %_PROJECTNAME% %_NDKROOT% %_PACKAGEPATH% "windows"pause

Modify the red font to your corresponding installation directory.

 


OK. Let's start with helloworld.

Oh, by the way, this requires an NDK_ROOT environment variable. Please add it yourself. If you don't know, Baidu.

After adding environment variables, restart the computer.

 


Open a create-android-project.bat File

Follow the prompts to enter:

Step 1 enter your project package name: com. android. cocos2d. test (you can enter it by yourself)

Step 2 enter the Project name: test

The third step window will list all available android versions. You can select one and enter the id number.

Then the project is generated and OK. You can see that there is an additional test directory under the current directory. This is the project directory. If you don't see it, refresh it.

 


The next step is to compile the code. You need to compile the code into a so link library that can be called.

Open cygwin

Go to the android directory of the generated project

Run the shell script build_native.sh. The compilation starts. It takes some time

 


If you are not using the corresponding version, you may encounter various problems here. You are also welcome to post the problem to me, and I may be able to solve it for you. However, we recommend that you use Baidu to compare the speed.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.