Cocos2d-x create Android project and compile + import to eclipse compile

Source: Internet
Author: User

Based on the previous two articles, we have constructed a development environment. Now we can create an android project and compile it:

 

1. cygwin Compilation

Go to the cocos2d-x, open the create-android-project.bat in the editor, and modify the three paths:

@ Echo off: This script is used to create an android project.: You shoshould 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 shoshould run it under cocos2dx root & pause & Exit 2if not exist "% ~ Dpn0.sh "Echo script" % ~ Dpn0.sh "not found & pause & Exit 3: modify it to work under your environment set _ cygbin = G: \ cygwin \ bin // modify 1if not exist "% _ cygbin %" Echo couldn't find cygwin at "% _ cygbin %" & pause & Exit 4 :: modify it to work under your environmentset _ androidtools = G: \ Android \ SDK \ tools // modify 2if not exist "% _ androidtools %" Echo couldn't find Android SDK tools at "% _ androidtools %" & pause & Exit 5 :: modify it to W Ork under your environmentset _ ndkroot = G: \ cygwin \ android-ndk-r8e // modify 3if 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 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 targetsset/P _ targetid = Please input target ID: set _ projectdir = % Cd % \ % _ projectname % echo create Android projectmkdir % _ projectdir % echo create Android project inside proj. androidcall "% _ androidtools % \ android. bat "Create Project-N % _ projectname % -T % _ targetid %-K % _ packagepath %-A % _ projectname %-P % _ projectdir % \ proj. androidcall "% _ androidtools % \ android. bat "Update Project-l .. /.. /cocos2dx/platform/Android/Java-P % _ projectdir % \ proj. android: Resolve ___. sh to/cygdrive based * nix path and store in % _ cygscript % for/F "delims =" % A in ('% _ cygbin % \ cygpath.exe "% ~ Dpn0.sh "') do set _ cygscript = % :: resolve current dir to cygwin pathfor/F "delims =" % A in ('% _ cygbin % \ cygpath.exe "% Cd %"') do set _ currentdir = %:: 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

Set _ cygbin = to the absolute path of cycgwin \ bin installation.

Set _ androidtools to the absolute path of Android SDK tools

Set _ ndkroot to the absolute path of Android ndk.

 

Modified and saved

Double-click the create-android-project.bat to start the project creation wizard

 

First, enter the package name (we recommend that you follow the naming rules of Android), such as com. Android. Hello.

Enter the project name (do not enter the name of an existing folder. If the name is the same, it will be overwritten), such as helloworld, for example:

 

Then select target ID: the SDK of all versions installed on the machine. We select 6, that is, the SDK of 2.3.2. Press enter to create the project:

Note the red box below. This text will appear if the ndk environment variable is successfully configured at the end of the previous article.

 

Go to the G: \ cocos2d-x directory and find an additional directory named helloworld, which contains three directories

Classes: stores the C ++ files of the default project.

Proj. Android is an Eclipse project of Android.

Resources resource file

 

Next compile the helloworld project, open cygwin, enter CD/cygdrive/G/cocos2d-x/helloworld/proj. Android to go to helloworld/proj. Android

 

Enter./build_native.sh to start Compilation:

 

This process will take a long time. Please wait. Note: If the ndk_root error occurs during the compilation process, for example, if the file cannot be found, it is because the configuration of the ndk environment variable is incorrect. I have been entangled in this problem for a long time and finally found it. when adding an ndk path to bash_profile, the path is not opened with ue, so you must use the UE editor to edit it again. For detailed configuration, see the last part of the previous article.

 

The final compilation is successful as follows:

 

 

2. Import to eclipse for compilation

Next, we need to import the compiled Android project into eclipse. After eclipse compilation is successful, it can be run on the mobile phone.

First, import proj. Android in helloworld to eclipse:

 

After the import, if the following error occurs, that is, you did not import the cocos2d-x package into the Eclipse project:

 

Right-click the project and choose build path> link source> browser to add G: \ cocos2d-x \ cocos2dx \ platform \ Android \ Java \ SRC to the project.

The preceding import method is not permanent. Now we will introduce it once and for all.

Modify copy_file.sh under XXX \ cocos2d-x \ template \ Android:

Add CP-RF $ cocosjavalib_root/src $ app_dir/proj. Android to the copy_src_and_jni function.

The packages required by the cocos2d-x will be automatically imported each time you import the project.

Run the project, right-click the project, choose run as-> Android Application (from the cocos2d-2.0-rc0a-x-2.0, supports open gl es2.0, the default project creation is required to run on Open GL es2.0, that is, the current simulator is not supported, so you must use a real machine for debugging ).

 

At this point, the android project and compilation are created and imported to the eclipse compilation tutorial.

 

Related Article

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.