Cocos2d jsb package Android APK

Source: Internet
Author: User

1. First, we need to package normal cpp into Android APK.

The following is in cocos2d-x 2.2.2 or 2.3. This article in Eclipse always uses ndk to compile cocos2d-x.

Often talking about cocos2d-x JSB is not a simple js Code, involving C ++ code, if it is Android and Java code, a bit complex, if engaged in Android under the Jni will be familiar with some. Read this article: "Android Jni example Hello JNI, ndk"

To speed up app development and reduce the difficulty, Android selects Java, but can also use c or c ++ to introduce ndk in a specific version. Cocos2d-x is developed with C ++, so it can also be used in Android, but it involves ndk. In fact, if you have a Linux environment and complete Android source code, it is easier to compile ndk. But most cocos2d-x is developed with windows, and in windows ndk is more complex.


2. First, some software preparation


Install JDK and set environment variables.

Download ADT from www.android.com

Google search for ndk to download the latest ndk and set Environment Variables

Download the 2.3 www.cocos2d-x.org from the cocos2d-x and unzip it.

Google search python 27, set the environment variables (the old cocos2d-x version is not to use python to create a project, vs and xcode have templates, at the beginning is relatively easy to use, but cross-platform is more troublesome, later I switched to python, and I only needed to create it once, which is easier to use across platforms)

Add C: \ Python27; to PATH.


Here we still talk about the role of environment variables. Many people are not familiar with the environment variables. Let's look at the Python example. If I installed it on drive C and opened C: \ Python27, I would not see an execalled python.exe, it can parse python code, but it is very troublesome to get the complete path every time to parse it. Therefore, it is impossible to add C: \ python27to path. in this case, you can directly export the directory to phthon.exe.


3. first create a javascript cocos2d-x Project

Enter the cocos2d-x/tools/project-creator directory, win7, you can press Shift, right-click the mouse "Open command window here", enter python create_project.py-project TestJs-package com. test. example-language javascript

Basically. You can open create_project.py to view the source code, which contains the example of creation. You can also save the following code to txt, change the suffix to bat, and double-click it to run it, but in essence it still runs create_project.py.


@ Echo off set/p projectName = enter the project name (for example, test ): if "% projectName %" = "" goto inputError set/p packageName = enter the project package name (for example, org. cocos2dx. test): if "% packageName %" = "" goto inputError set/p projectLanguage = enter the programming language (for example, cpp, lua, and javascript ): if "% projectLanguage %" = "" goto inputError python create_project.py-project % projectName %-package % packageName %-language % projectLanguage % pause: inputError @ echo item The object name or package name cannot be blank! Pause


Copy the JS Code to the assets Directory. Here, Android development usually stores images, xml, and other resources in the res directory. when packaged into an APK, It is packaged into a file. Java code is also very easy to access. However, apart from standard resources, such as js and music, they can only be placed under assets. packaging them into APK is not a file. decompress the APK and the resources in assets are still original, no processing is performed. These jsb start with js seems to have to download Cocos2d-JS.



Note that there is a file called cocos2d-jsb.js, which is to start js, to be associated with Classes/AppDelegate. in cpp, applicationDidFinishLaunching has a call to ScriptingCore: getInstance ()-> runScript ("cocos2d-jsb.js ");


3. Import two projects in ADT



On the left, right-click Import

The location is the following two places:

Cocos2d-x-2.3 \ cocos2dx \ platform \ android \ java

Cocos2d-x-2.3 \ projects \ TestJs \ proj. android



4. Set Build properties


Choose Window> Preferences> Android> NDK to edit the NDK path. Note that the default NDK name is changed here. It is generally relatively long.



Right-click the TestJs project and choose Properties> Builders> New> Program,

In the Location under the Main tag, select Browse File Systerm... and select the ndk-build.cmd File under android NDK.

Working Directory, select Browse Workspace..., and select this project.


Under the Environment tag, click new,

Name: NDK_MODULE_PATH

Value:
C: \ D4 \ cocos2d-x-2.3; C: \ D4 \ cocos2d-x-2.3 \ cocos2dx \ platform \ third_party \ android \ prebuilt (pay attention to your actual directory, here two values are defined, one is the root directory of the cocos2d engine, and the other is .... prebuilt)

Select OK to confirm, return to the Builders interface, remove the check box before CDT Builder, and move the Builder you just created to the top. After all the configurations are complete, OK.

Connect to your phone and run it. APK is generated in the Bin directory. All the C ++ files are packaged into a libcocos2djs. so file in the libs/armeabi directory.


5. Common Errors


1. "bash" is not found in PATH. I don't know how to solve this error. Compiling with Eclipse is sometimes a breeze. Multiple projects-> Clean several times, you can also try to delete the project and re-Import it, or you can only download cygwin and add the bin directory of cygwin to the system environment variable.


2.Syntax and Semantic ErrorsThis error is generated by clicking main. cpp in the jni directory. You can disable this error in Eclipse:Right-click your project Properties-> C/C ++ General-> Code Analysis-> Syntax and Semantic Errors, Remove the check box.


3. unable to process assets while packaging xxx. This error is caused by insufficient assets permissions. For example, if I create a project on the C drive of Win7, this problem occurs in proj. in the android directory, right-click to openIn the command window, Grant High permissions to assets: chmod 777-R assets/


4. There are still some errors. Remember to right-click the project attribute and open Builder to delete the Builder with the error.

5. Another error is that the simulator does not support OpenglES2.0. You can use the BlueStacks software, which is much more powerful than the simulator in the ADT. Of course, it is best to use a real machine.

6. Download TestJs APK:

Http://www.waitingfy.com/archives/1126/testjs

Refer:

Cocos2d jsb Android

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.