Cocos2d JSB Pack Android APK

Source: Internet
Author: User

1. First, the normal CPP will be packaged into Android APK

The following is said in Cocos2d-x 2.2.2 or version 2.3. This article compiles cocos2d-x with the NDK in Eclipse.

Cliché Cocos2d-x JSB is not a simple JS code, involving C + + code, if it is Android, but also related to Java code, a little more complex, if you have been under the control of the Android JNI will be familiar with. You can read this article: "Android Jni example Hello jni,ndk"

Android to improve the speed of developers to develop applications, reduce the difficulty, choose Java, but also can be used in C or C + +, in a version of the NDK introduced. Cocos2d-x is developed in C + +, so it can also be used in Android, but it involves the ndk. In fact, if there is a Linux environment, Android full source code, NDK compilation is still relatively easy. But most cocos2d-x are developed with Windows, and the NDK is more complex under Windows.


2. First, some software preparation


JDK installation, fix environment variables, slightly.

Go to www.android.com Download ADT

Google search ndk Download the latest NDK, set environment variables

Go to www.cocos2d-x.org download version 2.3 of cocos2d-x, unzip.

Google search Python 27, set the environment variables (the old Cocos2d-x version is not used in Python to create the project, VS and Xcode have a template, just starting to be more useful, but cross-platform is more troublesome, later instead of Python, only need to create, cross-platform more useful )

Add C:\Python27 to the path.


Here is still the role of environmental variables, many people are not familiar with the environment variables, look at the example of Python, suppose I was installed on the C drive, open C:\Python27, is not to see an EXE called Python.exe, it can parse Python code, but each time to get the full path to parse will be very troublesome, so the C:\Python27 added to the path, This allows direct access to the Phthon.exe in any directory.


3. First create a JavaScript cocos2d-x project

To enter the Cocos2d-x/tools/project-creator directory, Win7, you can press SHIFT, right-click the mouse "Open Command Window", enter Python create_project.py-project Testjs- Package Com.test.example-language JavaScript

Basic can. You can open create_project.py to view the source code, there are examples of the creation. You can also save the following code to txt, suffix to bat, double-click Run, but the essence is to execute create_project.py.


@echo off        set/p projectname= Enter the project name (for example: test):        if "%projectname%" = = "goto inputerror        set/p packagename= Please enter the project package name (for example: org.cocos2dx.test):        if "%packagename%" = = "goto inputerror        set/p projectlanguage= Please enter a programming language (e.g.: Optional CPP, LUA, JavaScript):       if "%projectlanguage%" = = "goto inputerror       python create_project.py- Project%projectname%-package%packagename%-language%projectlanguage%     pause                : Inputerror        @echo Project name or package name cannot be empty!        Pause  


Copy the JS code to the assets directory. Here to rip, Android development generally put the picture, XML and other resources in the Res directory, and so packaged as an APK, will be packaged into a file. It is also easy to access in Java code. But in addition to the standard resources, such as JS, music, etc. can only be placed under the assets, packaged into an APK will not be a file, decompression apk,assets inside the resources or the original, will not do any processing. These JSB at the beginning of JS as if to download Cocos2d-js only.



Notice here is a file called Cocos2d-jsb.js, this is the start JS, to correspond with Classes/appdelegate.cpp, Applicationdidfinishlaunching has a call Scriptingcore::getinstance ()->runscript ("Cocos2d-jsb.js");


import two items in 3.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 the Build property


Open the menu window->preferences->android->ndk edit the NDK path to your NDK's path, note that I have modified the default NDK name here. is generally relatively long.



Right-click the Testjs project, select Properties->builders->new->program,

Under the Main tab, under Location, select Browse File Systerm ... and select the Ndk-build.cmd files below the Android NDK.

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


Under the Environment tab, 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 (note own actual directory, here defines two values, 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 tick in front of the CDT builder, and move the builder you just created to the top, all configured OK to confirm.

Connect the phone and run it should be ready. The APK will be generated in the bin directory, and all C + + files will be packaged into a libcocos2djs.so placed in the Libs/armeabi directory.


5. Common errors


1. "Bash"is not a found in PATH, this error I do not know how to solve, with eclipse compile sometimes will be convulsions, more project->clean several times, or delete the project re-import test , or you can only download Cygwin , and add the Cygwin Bin directory to the system environment variable.


2. Syntax and Semantic Errors This error is generated by clicking on the main.cpp in the JNI directory and can be turned off in eclipse: 右击你的项目 Properties-> C/C++ General -> Code Analysis -> Syntax and Semantic Errors , remove the tick.


3. Unable to process assets while packaging XXX, this error is because assets's permissions are not enough, like I set up a project in the C drive of Win7, there will be this problem, In the Proj.android directory, press Shift right-click to open the Command window and give assets higher permissions: chmod 777-r assets/


4. There are some errors, remember to right-click the project properties, open the Builder to delete the error builder.

5. There's another mistake. The simulator does not support OpenglES2.0, and can be used with Bluestacks software, which is much more powerful than the emulator in ADT. Of course, it's better to use a real machine.

6.TestJs apk Download:

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

Reference:

Cocos2d JSB Android

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.