[Original] Detailed how to compile the COCOS2DX project onto the Android platform (mode one: CYWIN+NDK)

Source: Internet
Author: User

Link Address: http://m.blog.csdn.net/blog/yhc13429826359/29357815

2014-6-8 Read 578 comments 0

Foreword: As an open-source mobile 2D game framework, COCOS2DX's cross-platform features make it popular with development companies. Here I do not do a conceptual explanation, through this article you will understand that its cross-platform "what is the appearance of"---a little anthropomorphic I give a comparative image of the example: A, B, C three tribes, a tribe of the language is a and O language, B tribe of the language is B and O language, The C-Tribe language is the C and O language. What language would you choose to study as an extraterrestrial person, so that you could walk between tribes? O Language Right!       Because you learn the language you can communicate with people in any tribe! Back in development, each tribe is a system platform (Android, IOS), Android development is based on the Java language but also supports C + +, iOS development based on OBJECT-C (new swift--launched the next day on the company's 30w 3 years of experience as a swift programmer, I want to say: "Which house company?" You can recruit me after three years ") but it also supports C + +, and you and I all know that COCOS2DX is written in C + +, then the COCOS2DX cross-platform is written in C + + with a little modification can be compiled in Android and iOS run. This is COCOS2DX's "write once, multi-platform compile run". Note: This is different from the Java cross-platform feature "compile once, run everywhere" (Java-developed programs run based on the JVM (virtual machine), no matter what platform, as long as you install the JVM, you can run Java programs) This article will take you through, How to create a COCOS2DX HelloWorld program on the Win32 platform and then port it to Android (Mac platform later) I machine configuration:
win7+vs2012 (with very cool, used to always use VS2008, now directly over vs2010--It starts super slow) This article development environment: vs2012+cocos2d-x-2.2.2+cywin+ndk+ Android development environment (in my previous article on how to build the Android development environment--http://blog.csdn.net/longming_xu/article/details/28241045, this article does not introduce)
Study Progress Chart:
    1. Download and install vs2012 (if it has been installed, I will not introduce his installation here, if you install the other version is also matter, after all, he is only an auxiliary development of the IDE (but very strong), only a few inconsistencies in the configuration)
    2. Download and install Visual Assist X (Tomato plugin-because its icon is a tomato-like, so called tomato plug-in, here I will not introduce how to download the installation hack, self-search online, a lot of)
    3. Download Install python (Python is a powerful scripting language, COCOS2DX the most predecessor is written in Python, this needs to look at the history of Cocos2d. COCOS2DX Create a new project using a Python script, and you'll see it's creation scripts later.
    4. Download cocos2d-x-2.2.2 version
    5. Create a HelloWorld program
    6. Download Cygwin installation
    7. Download NDK and install
    8. Cross-compiling projects
    9. Android Development Environment Building
Start serving ... First step: Download and install vs2012:
I used to do the end-trip development has been using vs2008+ tomato plug-in, with a good! Think at the time, just contact with the company project, the guy, code files up to 3000, imagine, a habit of scripting, and suddenly to compile these 3,000 files, this is how the project!!!! Change one common header file at a time (⊙o⊙)! Ya will have to use my half an hour more time!!!! But fortunately later contacted IncrediBuild this online compiling tool, it is simply "artifact" for C + + developer. I was in the company three machines installed on-line compilation, guys!!! It took only 10 minutes or so to get rid of more than 3,000 files. VS Series + tomato plug-in +incredibuild is a super-strong combination of A/C + + development! Speaking of which, drool!! .... Drool???? How can I think of this word??? VS2008---vs2010---vs2012 i have installed, personal feeling vs2010 a bit brittle! Light open It is enough for me, so not a few days to change the vs2012 (its installation is really long enough) between them to use feelings, you can search the Internet (perhaps later I will write such an article ...) I assume here that you have installed the VS2012 development environment.

Step two: Download and install Visual Assist X:
Visual Assist X---We call it the tomato plugin because his icon is like a tomato (a tomato, right). It is very powerful, can prompt the input of the keyword, automatic error correction, can recognize the system function, the user defines functions and variables, For the development of software has brought great aspects, not only improve the development efficiency, but also improve the fun of programming. It is present as a vs enhancement plugin, which makes our programming faster and easier. There are a number of small features that can speed up your development and error correction capabilities. (Here I assume you have downloaded the installation, of course you can also choose not to download, he is just a vs feature enhancement plugin, without it you can use VS for development, but it is really "fascinating", with a use to know, do not forget to crack ... It's all Chinese!! )
Step three: Download and install Python:
Python is an object-oriented, interpreted computer programming language. A common application scenario is to use Python to quickly build a prototype of a program (sometimes even the final interface of a program) and then rewrite it in a more appropriate language, such as a graphics rendering module in 3D games, with a particularly high performance requirement, and then rewrite it in C + + with a particularly demanding part. The most I use of Python is the processing of files and project flow control (packaging release AH), game development using Python script can solve a lot of tedious work, for their own project development save a large part of the time, who use who know!!! Later you can see how COCOS2DX uses it to create the project. : https://www.python.org/download/releases/2.7.5/(we download version 2.7.5, there is no reason why, because I use this version, more than 3.0 version of the change is very large, some usage has changed, As if the COCO2DX installation script does not support more than 3.0 version, so be good to download the 2.7 version of it----sometimes not the latest is the best, you know)
    after downloading, perform python-2.7.5.msi to install it. Do not forget to configure the environment variables after the installation is complete Oh, do the program development if you do not understand the configuration environment variables then you are miserable ... (my article has a http://blog.csdn.net/longming_xu/article/details/28241045 that describes the configuration of environment variables)   Fourth step: Download COCOS2DX 2.2.2 Version cocos2dx Each version of the changes are very large (if you want to know the changes in each version, please google), this is my beginner's time to compare the place where the egg hurts, sometimes wondering why cocos2dx do not engage in an integrated development environment like UNITY3D? Each version of the change, even the construction of the project is not the same, let us love what AH (3.0 version of the change is said to be quite large to ... )!!! Here I only introduce 2.2.2 version of the project created, other versions of Google (because this is not the main content of this article, if necessary, you can consider the next supplement). : Http://www.cocos2d-x.org/download/version#Cocos2d-x found the v2.2.2 version (now it's out to v3.1.1)   ok! Here you have the basic development environment (IDE (vs2012+ tomato plugin) +COCOS2DX Development Library), can be coco2dx developed, below we create a HelloWorld program.   Fifth step: Create a HelloWorld program: 1, unzip the zip file  2, unzip the file structure is this look (introduction organization structure of the article, self-Google)  3, how to create a project? COCOS2DX has provided us with the tools to create the project, he is a Python script file (so you need to install the Python environment) ① See the Red Box Tools folder, open it into the Project-creator directory, The red box is the project creation script (you don't need to know how he did it – you can understand if you want to):   ② Click on the current directory blank, then press shift+ right, select-here to open the Command Line window  ③ Open the command line, Then enter create_project.py after the return, prompted, he will tell you you need to pass the parameter information:-project is the new project name-package is the Java way of the package structure (this is set for Android porting)-language is the development used by your projectLanguage (CPP is c++,lua,javascript three kinds)    ################################################################ ############################################### #33小提示: You can write your own bat small script named Createproject.bat, use the time to run directly on it ( Called createproject.py)
 @echo offset/p projectname= Please enter the project name: if "%projectname%" = "" Goto inputerror1set/p Packagename= Please enter the package name: if "%packagename%" = = "" Goto inputerror2set/p language= Please enter the language: if "%language%" = = "" Goto Inputerror3create_project.py-project%projectname%-package%packagename%-language%language%pauseexit: Inputerror1@echo project name cannot be empty! : Inputerror2@echo package name cannot be empty! : Inputerror3@echo Select the language cannot be empty! Pause 
 ######################################################################################################### #####④ then we follow the prompts to create our new project, such as the Red box 1 content. If a done! occurs after execution It means you've created it! Red Box 2 indicates where the project was created.   ⑤ so we go into project folder project, you will find that the newly created project HelloWorld  ⑥ into the project, and its file organization structure, such a COCO2DX new project is complete. The classes folder is the code location for the project Proj. The beginning of the folder is the portal implementation of each platform resource folder is the resource file location  ⑦ Let's compile and run it, Enter Proj.win32, double-click the HelloWorld.sln solution file, open the vs2012, run the compilation (needless to say) ⑧ see the result, when when ... Done! (so that we can develop under the Win32 platform---Indulge in the ravages of COCOS2DX bar)   So far, we have learned how to create COCOS2DX project, and HelloWorld program run up, can be Win32 developed, So the next step is to cross-compile the project onto Android, and you'll see the cross-platform of COCOS2DX. Here's a little introduction: Why does the C + + code you write run on Android? In fact, this is a JNI (Java Native Interface--java Local call) technology that uses Java, and JNI technology allows Java code to interact with code written in other languages (c/s). Encyclopedia: As we all know, the Android SDK is based on Java implementations, which means that third-party apps that are developed based on the Android SDK must use the Java language. But this is not the same as "third-party apps can only use Java." When the Android SDK was first released, Google announced that its virtual machine Dalvik supported JNI programming, which means that third-party applications could call their own C dynamic library via JNI, which means that "java+c/c++" is programmed on the Android platform. (The NDK that will be used below is actually using JNI technology, simplifying it)    sixth step: Download Cygwin installation First you'll ask Cygwin what's the matter, but he's a unix&nb.SP; Simulate the environment, which allows you to run the unix  environment under Windows without having to install a virtual machine! It is useful for learning unix/linux operating environments, porting applications from UNIX to Windows, or doing some special development work, especially for embedded system development on Windows using the GNU toolset. With the growing popularity of embedded system development in China, more and more developers have been interested in Cygwin. In short, it is a small Linux system (simulated), in Cygwin you can use most of the Linux software and features. such as Gcc,make,vim,emacs and so on. In short, if you want to use a Linux function, and Windows can not find a good substitute, you will be able to use Cygwin. The most frequent uses are GCC and make. They are often used to compile some open source projects that I download from the Internet. These projects are often cumbersome to compile on windows.
: https://cygwin.com/install.html It's installation tutorial I will not write, with a lot of pictures .... I'll first "steal" someone else's link for your reference: http://jingyan.baidu.com/article/60ccbceb02fe4264cbb19776.htmlhttp://www.xpgod.com/ Article/16739.htmlhttp://www.33lc.com/article/7276.html Note: During the installation of the components, the gun make components that must be installed will need to be used to compile A/C + + file. If you are unfamiliar with Linux, install all of the components in the Devel directory directly. You can also download the platform's component packages locally and install them locally, depending on which method you choose.  When the installation is complete, open a command-line window with a Linux shell and test the GNU make version: Make-v, which displays the GNU make version number to indicate that the installation was successful, and then you can simulate the Linux operation. So why install it? This is related to the NDK we installed below (Native Development Kit Local Development Kit)!! Since the NDK (note: The NDK R7 version and later versions come with a compiler that does not need to be Cygwin) to compile the code, you must use make and GCC, so you must first build a Linux environment, Cygwin is a UNIX simulation environment running on the Windows platform , you can use the NDK to compile C, C + + code without installing Linux.

Seventh step: Download the NDK and install what about the NDK? NDK full Name native Development Kit Local Development kit.   Encyclopedia: The NDK is a collection of tools------①NDK provides a range of tools to help developers quickly develop C (or C + +) dynamic libraries and automatically package so and Java applications together as APK. These tools are great for developers. ②NDK integrates the cross compiler, and provides the corresponding MK file isolation CPU, platform, ABI and other differences, developers only need to simply modify the Mk file ("which files need to compile", "compile feature requirements", etc.), you can create a so. ③NDK can automatically package so with Java applications, greatly reducing the developer's packaging effort.   If you downloaded the previous version of the NDK R7, you will find that the Cygwin download is mentioned (to provide MINGNU environment for make and GCC). The NDK R7 and later versions have their own compilers, so there is no need to download Cygwin. (Here I no matter which version, all use Cygwin to do cross-compiling, as is learning Cygwin, the following will be introduced directly using the NDK R7 with the compiler, do not use Cygwin)  :   1, After the download is completed to obtain a compressed package, extracted to the specified folder can be, I extracted to the previous build Android when the SDK peer directory under the D:\Android under the     2, decompression completed, Don't forget to configure the NDK's system environment Variables!!!!     so far, CYGWIN+NDK download installation finished!!!! So how do you use them for cross-compiling? Eighth step: cross-compiling the project 1, enter the name of the cygwin/home/computer, open the. bash_profile 2, and finally add the following in the. bash_profile file: #ndk的根路径NDK_ROOT =/cygdrive/d/ Android/android-ndk-r9dexport ndk_root  #cocos2dx的根路径COCOS2DX_ROOT =/cygdrive/g/projects-cocos2dx/ Cocos2d-x-2.2.2export cocos2dx_root   Tip: The Export command in bash----to turn a bash local variable into a bash global variable   3, Now you can open the Cygwin terminal and perform the following lifeMake the CD $NDK _ROOTCD $COCOS 2dx_root output is configured successfully, you can directly use these variables (why to configure these variables, I will explain why!!!!!)  4, enter cocos2dx proj.android\jni modify android.mk file (add the file path you want to compile in the red box content, HelloWorld sample program only the following three)  5, Enter the COCOS2DX proj.android, execute the build_native.sh file    6, and then wait patiently for the compilation to complete!! It will take a few minutes!! When the compilation is complete, the following interface appears, and the so file is generated under Proj.android\libs\armeabi\ (binggo!! Cross compilation succeeded! )    #################################################################################### Supplemental Description: What is the use of ndk_root and cocos2dx_root when configuring Cygwin. bash_profile files in the second step above? You didn't notice. When you execute build_native.sh, you print some information, we look at the build_native.sh script content!! RED Box 1---Determine if the ndk_root variable is defined; (This is why we configured the Ndk_root) red Box 2--Gets the path of the currently executing bash script; red box 3--printing related path information; Red Box 4--Create a new Assets resource folder, copy resources under resource to assets! people who are familiar with bash should not be difficult to understand.
####################################################################################
Nineth Step: Android Development Environment Building (my previous article  http://blog.csdn.net/longming_xu/article/details/28241045  detailed introduction, will not repeat, You can use it directly! Our cross-compilation work has been done, and the end is to build the APK and test it on the Android phone.  1, first open eclipse, then file----New-----project--Create a project from existing code---next 2, import HelloWorld project address, execute finish   3, create a new project directory structure like, you will find that he reported an error, said no import package org.cocos2dx!!!   4, the solution has two kinds: one is to copy the Org folder under \COCOS2D-X-2.2.2\COCOS2DX\PLATFORM\ANDROID\JAVA\SRC to \cocos2d-x-2.2.2\ PROJECTS\HELLOWORLD\PROJ.ANDROID\SRC, refresh the project, the error disappears   The other is to create a new project directly to add Java Library as Lib mode (COCOS2DX self-Configuring this way), after the completion of the addition , the error disappears   5, right HelloWorld---properties, modified as (Android target version), the same LIBCOCOS2DX---properties (but need to designate him as Lib)  6, OK, there is no error, right-HelloWorld the project, execute it, run as...  so you're in cocos2d-x-2.2.2\projects\helloworld\proj.android\. The bin generates an Android APK file that can be installed on Android phone!!!! (You can also use the AVD simulator test, but it generally slow response, recommended real-machine testing!!) )     ok! COCOS2DX cross-compiling to Android is done. Let's sum it up! 1, first you want to download install vs2012+ tomato plug-in----so you can do C + + development 2, download and install Python----to create a new project COCOS2DX need to use 3, Download cocos2d-x-2.2.2 version, unzip to---our game development 2d game open source Framework 4And then we introduced the use of the various tools above how to create a HelloWorld program 5, download install Cygwin and NDK (R7 will not need Cygwin, it comes with a compiler) 6, To cross-compile the project (a script under the Proj.android folder is executed under Cygwin: a variety of information is configured inside. Do not forget to configure the Mk file under the Jni folder before executing the script OH) A so file is generated after compiling. Tip: What is a Mk file? What is the so file? Android things, can go to understand, I did not know! 7, the final step is to import the project in Eclipse, compile and build APK!!!! When you're done, you can deploy to your phone!    advice: If you want to understand these principles, you can learn about the development of Android projects (JNI technology, the use of the NDK, the use of Cygwin, bash syntax ah), you should feel that cross-compiling to Android is actually not difficult   finally finished, with the last blog experience, this time try to write less verbose! But still a little verbose ... I hope you don't mind, just want to write to let people understand some. If you have any questions can reply to me, I will be in time to solve! At the same time, you are welcome to criticize.   This blog only introduce way one: CYGWIN+NDK, then the way two? The NDK R7 version does not need to be Cygwin, because it comes with a small compiler!!! Configure eclipse (to be continued)

[Previous] details on how to compile the COCOS2DX project onto the Android platform (mode one: CYWIN+NDK)

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.