Tutorial:
1. Prepare and configure the development environment:
Development Environment: Mac ox 10.7.3 + xcode4.2 + ndk R7 + eclipse Helios
Deployment environment: ZTE v880 has gone through the ROM of 2.3.5, And the simulator is API 7 and Android 2.1.
A. Install xcode and eclipse first. I will not talk much about the installation of these two tools. Xcode directly installs the installation package next, while eclipse also has the MACOs version, which does not need to be installed.
B, then, install the cocos2d-x. My installed version is the latest version.Cocos2d-1.0.1-x-0.11.0 release. (We recommend that you use gitSource codeThe repository is cloned so that when there is an update, only pull is required, and then the latest version can be obtained in a small amount of time. As for how to use git, you can check it online. This tool is becoming more and more popular. I am sorry to say that I am not git.ProgramClerk: Well, I am not talking about this. Others are talking about it, but there is a certain truth in it. You don't like it .)
After the download is complete, decompress the package, open the terminal (terminal), CD to the directory you just extracted, and then run
./Install-templates-xcode.sh-u-F
Next, you will get the following output:
Cocos2d-x template Installer
SelectThe template version to install
3 ForXcode3
4 ForXcode4
Input nothingForAll
Here you can choose the template type to install. Because xcode4 is used, I enter 4. If you like 3, enter 3 here or press Enter, you can install 3 and 4 at the same time.
Select 4 and press enter, as shown in:
Installing xcode4Box2d IOS Template
----------------------------------------------------
... Copying box2d files
Done!
Installing xcode4Lua IOS Template
----------------------------------------------------
... Copying Lua files
Done!
Done!
When this prompt appears, it indicates that the installation is successful.
C. Configure the android development environment, install the ndk, and set some environment variables.
In this process, supersuraccoon has already been written in his blog. I am going to talk about it here. Don't be surprised :)
4.1 Check that JDK has been installed.
You can run the "Java-version" command"
4.2 Confirm that ant has been installed (preferably 1. 8 . 0 or updated version)
You can run the "Ant-version" command"
4.3 Install eclipse
I am using the Helios version (eclipse-jee-helios-SR1-macosx-cocoa), which contains some of the plug-ins that are needed (the first to use is eclipse-SDK- 3.7 -MacOSX-cocoa, but some errors are reported when the ADT plug-in is installed later, so it is replaced)
4.4 Install ADT plug-in
Installation steps:
HTTP: // Developer.android.com/sdk/eclipse-adt.html
4.5 Install Android SDK
If you have a network connection, refer to the installation steps:
HTTP: // Developer.android.com/sdk/installing.html
However, if you have no network connection like me, the process will be troublesome:
4.5 . 1 Download all required packages
You can refer to this:
HTTP: // Qdevarena.blogspot.com/2010/05/download-android-sdk-standalone-for.html
Or
HTTP: // Azerdark.wordpress.com/2011/04/21/android-sdk-offline-installation/
4.5 . 2 Decompress the package to the corresponding location
4.5 . 3 Create and run a simulator
Download ndk R7 and decompress it to a directory that is easy to find. My is/volumn/mydata/android-ndk-r7b, then the SDK installation directory is/volumn/mydata/Android-SDK. I installed the SDK online. At that time, I only chose api7 and api15, that is, android2.1 and android3.0.
Next, configure the environment variable, open terminal, and enter the following statement:
Vim ~ /. Bash_profile
Then press SHIFT + G to jump to the end of the file, enter a in sequence, press enter, and copy the following statement. in bash_profile, you can modify the path based on the path where you install the SDK and ndk:
Export android_ndk_root =/volumes/mydata/android-ndk-r7b
Export cocos2dx_root =/volumes/mydata/Guanghui/cocos2d-x
Export ndk_root =/volumes/mydata/android-ndk-r7b
Export android_sdk_root =/volumes/mydata/Android-SDK
Export Path = $ path: $ android_ndk_root
Then Press ESC, and then press WQ (Press ENTER) to save it. If you do not like vim, you can use other editors. However, because./bash_profile is a hidden file, you need to Baidu to show how to hide the file. Next, we will officially start our cross-platform game journey.
2. Create an iPhone Project
This is very simple. Open xcode, file-> New-> new project, and select the cocos2d-x template, as shown in:
Then download a player Image,A projectile Image, AndA Target Image. Put these three images under the project's Resource Directory and add references.
Replace the init method of helloworldscene. cpp with the followingCode:
// On "init" you need to initialize your instance
Bool Helloworld: Init ()
{
//////////////////////////////
// 1. Super init first
If (! Cclayercolor: initwithcolor (ccc4 ( 255 , 255 , 255 , 255 )))
{
Return False ;
}
// Add a ninjia sprite to the left screen center
Ccsprite * ninjiasprite = ccsprite: spritewithfile (" Player.png " );
Ccdirector * Director = ccdirector: shareddire ();
Ccsize screensize = Director-> getwinsize ();
Ninjiasprite-> setposition (CCP (ninjiasprite-> getcontentsize (). width/ 2.0f ,
Screensize. Height/ 2.0f ));
This -> Addchild (ninjiasprite );
Ccsprite * targetsprite = ccsprite: spritewithfile ( " Target.png " );
Targetsprite-> setposition (CCP (screensize. width/ 2.0f , Screensize. Height/ 2.0f ));
This -> Addchild (targetsprite );
Return True ;
}
Compile and run, and you will see the output shown in:
Now, the iPhone project has been created. It's easy, huh, huh. Next, let's create an android project.
3. Create an android Project
First, open the terminal and enter:
CD $ cocos2dx_root (Press ENTER)
./Create-android-project.sh (Press ENTER)
Then, the following prompt appears:
UseGlobalDefinition of ndk_root:/volumes/mydata/android-ndk-r7b
UseGlobalDefinition of android_sdk_root:/volumes/mydata/Android-SDK
Input package path. For example: org. cocos2dx. Example
The preceding two prompts the script to officially use the environment variables we previously defined, which are the installation path of my android SDK and ndk respectively. If the two variables are not defined before, an error is returned.
Then it requires us to enter the Java package path and sample is provided. Here I enter com. andyque. simplegame, and then press Enter.
Next, it will appear:
Now cocos2d-x supports android 2.1 -Update1, 2.2 ,2.3 & 3.0
Other versions have not tested.
Available Android targets:
----------
ID: 1 Or " Android-7 "
Name: Android 2.1
Type: Platform
API level: 7
Revision: 3
Skins: hvga, qvga, wqvga400, wqvga432, wvga800 ( Default ), Wvga854
Abis: armeabi
----------
ID: 2 Or " Android-15 "
Name: Android 4.0 . 3
Type: Platform
API level: 15
Revision: 2
Skins: hvga, qvga, wqvga400, wqvga432, wsvga, wvga800 ( Default ), Wvga854, wxga720, wxga800
Abis: No Abis.
Input target ID:
Here, the script detects the API installed on my current system. As I said before, I only installed api7 and api15. note that there is a line above:
Now cocos2d-x supports android2.1-Update1,2.2,2.3&3.0
Other versions have not tested.
If the reader uses other android versions, there may be problems, because they have not been tested. You can help them to test them, maybe they will be accepted, haha.
Here I select 1, because the system installed on my mobile phone is 2.3.5, and I will run it on my mobile phone later.
Next, you will be prompted:
Input target ID:
1
Input your Project name:
I enter simplegame and press Enter:
Created project directory:/volumes/mydata/Guanghui/cocos2d-x/simplegame
Created directory/volumes/mydata/Guanghui/cocos2d-x/simplegame/src/COM/andyque/simplegame
Added file/volumes/mydata/Guanghui/cocos2d-x/simplegame/src/COM/andyque/simplegame. Java
Created directory/volumes/mydata/Guanghui/cocos2d-x/simplegame/Res
Created directory/volumes/mydata/Guanghui/cocos2d-x/simplegame/bin
Created directory/volumes/mydata/Guanghui/cocos2d-x/simplegame/libs
Created directory/volumes/mydata/Guanghui/cocos2d-x/simplegame/RES/Values
Added file/volumes/mydata/Guanghui/cocos2d-x/simplegame/RES/values/strings. xml
Created directory/volumes/mydata/Guanghui/cocos2d-x/simplegame/RES/Layout
Added file/volumes/mydata/Guanghui/cocos2d-x/simplegame/RES/layout/Main. xml
Added file/volumes/mydata/Guanghui/cocos2d-x/simplegame/androidmanifest. xml
Added file/volumes/mydata/Guanghui/cocos2d-x/simplegame/build. xml
Added file/volumes/mydata/Guanghui/cocos2d-x/simplegame/proguard. cfg
CP:/volumes/mydata/Guanghui/cocos2d-x/helloworld/Resource /* : No such file or directory
At this time, the android project of cocos2d-x is established. To ensure that your project is okay, you can run$ Cocos2dx_root/simplegame/Android/build_native.sh.
The method is simple. Open the terminal and enter
CD $ cocos2dx_root/simplegame/Android/
Then:
./Build_native.sh
TIPS: You can use the tab key to complete commands and files in Linux. The same applies to Windows. However, some kids shoes may not know about tips. In this way, you do not need to enter a letter or a letter. If you do not need to talk about it, it is easy to make mistakes.
By default, all android projects are built under the installation directory of the cocos2d-x, And the MK file inside is referencing the libraries and files under the cocos2d-x.
However, we need to create an independent project that supports both iOS and Android. Therefore, I will teach you how to integrate the android project you just created into the iPhone project.
4. Merge the android project into the iPhone Project
This step is cumbersome and error-prone. You should take a closer look.
Let's take a look at the group layout of the project file in xcode after Configuration:
A new Android folder is added here. However, only SH and MK files are displayed in xcode. However, in fact, these files are only references, and other files must be copied to the project.
(Note: The operation step is to first copy the entire android folder to the same level directory of iOS, and then add only references in. In fact, you can directly add references without copying them. However, there is a problem. On that day, if your file is moved, the reference of the project cannot be found. Therefore, to facilitate management, copy the android directory to the project .)
For details, refer:
First, select your favorite text editor and CD it to the android directory of simplegame, then open terminal, and enter:
Vim makefile
//A new window is created, enter I to enter the editing mode, and then enter the following three statements.
Clean:
Rm-RF libs/
Rm-rf obj/
Note: here, the clean statement is input by the top level, and the two RM statements are indented by the tab key. If it is not indented, an error is returned when you enter make clean.
After the input, ESC (return to command mode): Press enter, save and exit. OK. Then, enter make clean. If the following output is displayed, it indicates OK:
Rm-RF libs/
Rm-rf obj/
Next, copy some Android-related files to the previously created IOS project. (Note: $ symbol + environment variable name is the alias of the directory you previously defined, that is, $Cocos2dx_root indicates the decompressed directory of your cocos2dx)
Step 1: extract the android folder from$ Cocos2dx_root/simplegame/Android/Copy it to the iPhone project directory, which is in the same directory as the IOS folder. (The final directory is simplegame/Android /)
Step 2: extract the Android. mk file from$ Cocos2dx_root/simplegame/classes/copySimplegame/classes/below
Step 3: extract Android. mk from$ Cocos2dx_root/cocos2dx/copySimplegame/libs/cocos2dx/below
Step 4: extract the android folder from$ Cocos2dx_root/cocosdenshion copySimplegame/libs/cocosdenshion.
The last step is to extract the platform folder from$ Cocos2dx_root/cocos2dx/platform copySimplegame/libs/cocos2dx.
Of course, this platform is a little big, with a size of about 114 mb. Except for Android, third_party, and iOS, these three folders cannot be deleted, and all other folders can be deleted. Please note !!!!
Do not delete those files. One file cannot be deleted !!!!
Next, configure the build_native.sh script and replace it with the following:
# set Params
ndk_root =/volumes/mydata/android-ndk-r7b/
cocos2dx_root = .. /libs/cocos2dx
game_root = .. /
game_android_root = $ game_root/Android
resource_root = $ game_root/resources
# Make sure assets is exist
If [-d $ game_android_root/assets]; then
RM-RF $ game_android_root/assets
fi
mkdir $ game_android_root/assets
# copy resources
for file in $ resource_root /*
DO
If [-d $ file]; then
CP-RF $ File $ game_android_root/assets
fi
If [-F $ file]; then
CP $ File $ game_android_root/assets
fi
done
# build
$ ndk_root/ndk-Build-C $ game_android_root $ *
Note that the ndk_root here should be changed to your own path.
Then modifyAndroid/JNI/Android. mk:
Modify application. mk as follows:
App_stl: = gnustl_static
App_cppflags + =-frtti
App_modules: = cocos2dx_static cocosdenshion_shared game_logic_static game_static
ThenAndroid/JNI/helloworld/Android. mk is modified as follows:
ThenClasses/Android. mk is changed to the following format: (when the game grows, you can add the source file in local_src_files. Of course, if you want to use box2d or other third-party libraries,
So please look forward to the next tutorial :))
local_path :=$ (call my-DIR)
include $ (clear_vars)
local_module: = game_logic_static
local_src_files: = appdelegate. CPP \
helloworldscene. CPP
local_c_includes: = $ (local_path )/.. /libs/cocos2dx \
$ (local_path )/.. /libs/cocos2dx/platform \
$ (local_path )/.. /libs/cocos2dx/include \
$ (local_path )/.. /libs/cocosdenshion/include \
local_static_libraries: = libraries
local_static_libraries + = pai_static_prebuilt
libraries + = cocos2dx_static
local_shared_libraries: = cocosdenshion_shared
include $ (build_static_library)
$ (call import-module, cocos2dx/platform/third_party/Android/modules/libpng)
$ (call import-module, cocos2dx/platform/third_party/Android/modules/libxml2)
$ (call import-module, cocos2dx/platform/third_party/Android/modules/libjpeg)
Then open the terminal and enter:
Make clean
./Build_native.sh
If everything goes well, you will get the following output:
... (There is a lot of compilation information above. Be sure not to make any mistakes. Please post any errors)
Install: libcocosdenshion. So => libs/armeabi/libcocosdenshion. So
Compile ++ thumb: game_logic_static <= helloworldscene. cpp
Staticlibrary: libgame_logic_static.a
Sharedlibrary: libgame. So
Install: libgame. So => libs/armeabi/libgame. So
Finally, open eclipse and select File> New> project.... The following dialog box is displayed:
Select Android project and next
Next, select import from existing project: (correct it. You should select Create project from exisiting source, Then browse, and locate it under the android directory just now. I'm a little lazy, so I won't try again)
Next, select android2.1: And then click Finish.
Finally, right-click the project and run as Android Application.
After a while, the simulator will get up and take a long time, but once it is up, it will be faster to compile and install the program. However, it still seems inferior to the IOS simulator.
After the success, you can see a ninja and a monster.
However, I am not lucky, black screen, haha, but it doesn't matter. We copied simplegame.apk under the android/bindirectory to my SD card, and then installed it. The running result is correct.
Below is the small V:
Note: As I was just learning Android, I encountered many problems when compiling cocos2dx with ndk. Then, the problem was basically solved by searching through the network. I would like to share with you the solution to avoid making the same mistakes as me. The following are some of my errors and solutions. If you have any configuration problems, please leave a message or leave a message to let me know. I will try again.
Trouble Shooting:
1. If cocodension is also configured as a static library, the following error will occur:
./Obj/local/armeabi/libcosdenshion. A (simpleaudioenginejni. O): In function 'jni _ onload':
/Volumes/mydata/mytoolbox/cocos2dxlib/simplegame/Android/JNI/http://www.cnblogs.com/libs/cocosdenshion/android/jni/simpleaudioenginejni.cpp:21: Multiple definition of 'jni _ onload'
. /Obj/local/armeabi/objs-Debug/game_static/main. o:/volumes/mydata/mytoolbox/cocos2dxlib/simplegame/Android/JNI/helloworld/main. CPP:16: First defined here
Collect2: LD returned1Exit status
Make: *** [OBJ/local/armeabi/libgame. So] Error1
2. Sometimes a black screen is displayed on the simulator, but there is no error message in the program. In this case, try installing the program on the real machine.
To communicate with the Forum, click the portal!
ReferenceArticle:
Http://gameit.ro/2012/01/creating-an-iphone-and-android-cocos2d-x-hybrid-project-updated/
Http://www.supersuraccoon-cocos2d.com/2011/08/10/cocos2d-x-iphone-androidide-installation-and-setup-under-mac-os/