After the installation, the corresponding environment, we can start to create a cocos2d-x Android Project
Go to the cocs2d-x directory and open the create-android-project.bat in a text editor (if you double-click on Windows 7, some files may be created with insufficient permissions, resulting in project creation failure, you can add CD/d % ~ to the first line of this file ~ Dp0, right-click to run as Administrator)
Find the following definitions:
@ 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. CD / D % ~ Dp0 Setlocal :: Check if it was run under cocos2d-x Root If Not Exist "% \ Create-android-project.bat" Echo Error !!! You shoshould 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 = E: \ 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-ndk-r8-windows \ Android-ndk-r8 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 "% \ % _ 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 targets Set /P _ targetid = Please input target ID: Set _ Projectdir = % \ % _ 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 = % :: Resolve current dir to cygwin path For /F "delims =" % A in ('% _ cygbin % \ cygpath.exe "% "') Do Set _ Currentdir = % :: Resolve ndk dir to cygwin path For /F "delims =" % A in ('% _ cygbin % \ cygpath.exe "% _ ndkroot % "') Do Set _ Ndkroot = % :: 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
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. Test.
Enter the project name (do not enter the name of the existing folder. If the name is the same, it will be overwritten), such as testandroid, for example:
Then select the target ID,
It lists all versions of the SDK installed on the machine. We select 6, that is, the 2.3.2 SDK.
Go to the G: \ cocos2d-x directory and find a directory named testandroid, 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, we will compile the created testandroid project.
Open cygwin, go to the testandroid/proj. Android directory, and enter./build_native.sh to start compiling, as shown in:
After the execution is completed, compilation errors are found, such:
The File Permission denied error is reported. Consider the Windows 7 environment. Let's take a look at the permissions of the JNI/helloword/MAI. cpp file.
When the JNI directory is entered, the following error occurs:
We don't care, because the above reports a specific file (main. CPP) the file does not have permissions, rather than folders. This means that the compiler can access the folder. CPP file permissions:
It is found that the current user (ming) has no read and write permissions. Of course, an error is returned during compilation. We grant it full control permissions, as shown below:
Click application, and OK to execute./build_native.sh again.
Or the File Permission problem. Let's re-authorize the permission in the previous method and re-compile it. If there are other similar problems found in the process, grant permissions. In order to save trouble, the contents of a registry file with quick authorization are as follows:
Windows Registry Editor Version 5.00 [ Hkey_classes_root \ * \ shell \ RunAs ] @ = "Administrator ownership" "noworkingdirectory" = ""[ Hkey_classes_root \ * \ shell \ RunAs \ command ] @ = "Cmd.exe/C takeown/f \" % 1 \ "& icacls \" % 1 \ "/grant administrators: f "" isolatedcommand "=" cmd.exe/C takeown/f \ "% 1 \" & icacls \ "% 1 \"/grant administrators: F" [ Hkey_classes_root \ exefile \ shell \ runas2 ] @ = "Administrator ownership" "noworkingdirectory" = "" [ Hkey_classes_root \ exefile \ shell \ runas2 \ command ] @ = "Cmd.exe/C takeown/f \" % 1 \ "& icacls \" % 1 \ "/grant administrators: f "" isolatedcommand "=" cmd.exe/C takeown/f \ "% 1 \" & icacls \ "% 1 \"/grant administrators: F" [ Hkey_classes_root \ directory \ shell \ RunAs ] @ = "Administrator ownership" "noworkingdirectory" = "" [ Hkey_classes_root \ directory \ shell \ RunAs \ command ] @ = "Cmd.exe/C takeown/f \" % 1 \ "/R/d y & icacls \" % 1 \ "/grant administrators: f/T "" isolatedcommand "=" cmd.exe/C takeown/f \ "% 1 \"/R/d y & icacls \ "% 1 \"/grant administrators: f/T"
After running this file as an administrator, you can right-click Windows 7 to add a menu for ownership. For example:
If you have access permissions again, right-click the Directory and select the Administrator to obtain ownership.
The final compilation is successful as follows:
After the compilation is successful, the so file (libgame. So) is generated directly under the directory testandroid \ proj. Android \ libs \ armeabi)
After compilation, compile the SDK into an APK file in eclipse and install and run it.
1. Import the compiled project to eclipse.
Open eclipse as an administrator (because the permissions previously obtained as administrators, otherwise there will be issues with permissions during compilation), select File-> New-> Android project, then select the following:
Click Next and select build target, as shown below:
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)
As follows: