Package Cocos2d-x games into Android apps

Source: Internet
Author: User

1. Open Eclipse (CDT, ADT, and NDK installed) and import the Android project of the cocos2d-x.

2. after the import, the source code of java will have a compilation error, open the cocos2d-x engine root directory \ cocos2dx \ platform \ android \ java \ src, copy the org file under src to the src folder of the project, the java file is compiled



3. Configure environment variables and resource paths. Right-click the project name and press Alt + Enter to Enter Linked Resources in Resource. Click new. The following dialog box is displayed.



Name input COCOS2DX, Location input cocos2d-x engine root directory, after entering click OK again click new,

Name input PROJECT_LOC, Location input cocos2d-x engine root directory \ projects \ your project \ proj. android, then click OK, configure the resource path binding



Select C/C ++ Build and see Build command: change the content in this text box to the installation directory \ ndk-build.cmd of your native ndk (this directory cannot contain spaces)



Next, select Environment under C/C ++ Build and Add three variables.


The names and values of the three variables are

COCOS2DX cocos2d-x engine root directory

NDK_MODULE_PATH root directory of the cocos2d-x engine; cocos2d-x engine root directory \ cocos2dx \ platform \ third_party \ android \ prebuilt

NDK_ROOT ndk installation directory



4. Copy the resource files under Resources to the cocos2d-x engine's root directory \ projects \ your project \ proj. android \ assets

5. Compile the jni \ Android. mk file.



Add the cpp file name of your project to LOCAL_SRC_FILES.



If there are more than one project file, it will be difficult to compile it, and it is easy to write errors when you need to change it. In the next article, use the py script to avoid this situation.

After compiling LOCAL_SRC_FILES, if any third-party library is introduced, you need to modify the following two items (here, the iconv library is used as an example)

Download the iconv library and copy it to the cocos2d-x root directory


Add the following two places:



Why do you want to write this code here? You can find it from the iconv root directory \ android. mk



In this way, you can

#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)#include "../iconv/iconv.h"#pragma comment(lib,"libiconv.lib") #elif (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)#include "../../../libiconv/include/iconv.h"#endif

When iconv is used, const char ** must be converted into char ** on the android platform, where the pin type is const char,

#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)if(-1 == iconv(iconvH,pin,&strLength,&outbuf,&outLength)){#elif (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)if(-1 == iconv(iconvH,(char **)pin,&strLength,&outbuf,&outLength)){#endif


6. compile the project. If you see the console output



Indicates that the compilation is successful.


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.