cocos2dx-3.0 (21) transplant Android platform It's a lot of tears.

Source: Internet
Author: User

~ ~ ~ ~ ~ My life, my little drip!

Online 3.0 tutorials are really few. Can say no, most of them are 2.x or 3.0 test version of the same, because my heart, did not follow the 2.x to engage, and then summed up after finishing, found in fact 3.0 of the transplant Android platform is very easy, do not need to follow the 2.x, the following record a "super-bitter Little idiot" In a situation where you don't know how to do this, you're going to have to transplant the Android platform.


1. First of all, your environment should be well-equipped

Take a look at the picture below, the picture is what you need


Look at the picture and talk

1.1 ADT

ADT (Android development Tools):
The development tool for Android development today is Eclipse, which is compiled in the IDE environment in Eclipse. Install ADT. The upgrade or change of development tools for Android development is simply understood as an upgrade download tool for developing tools under Eclipse.

1.2 SDK

SDK (software development Kit):
It is often a collection of development tools that are used by software project architects to build applications for specific software packages, software frameworks, hardware platforms, operating systems, and so on. In Android, he provides developers with library files and other tools for development.

A simple understanding of the Development Toolkit collection is the toolkit used in overall development, assuming you don't use eclipse as your development tool. You don't need to download ADT, just download the SDK and you can develop it.

1.3 JDK

May be at the time of development. People who are just beginning to learn will ask a question, since the SDK is a subset of the JDK, then why do they exist at the same time, and will it be possible to use the JDK? Other than that. The JDK path is not set in Eclispe. And Android is dependent on the SDK. Does the JDK mean it doesn't work at this point? In fact, it's easy to answer this question. The key point is to make sure that if eclipse is working properly, you must install the JDK. In an XP environment, although the reference path to the JDK is not set in the IDE, Eclipse will proactively look for it in system variables. This understands that the role of the JDK is to perform services for Eclipse.

1.4 ANT

Apache Ant is a tool that links software compilation, testing, deployment, and other steps to its own initiative, mostly for software development in the Java environment. Provided by the Apache Software Foundation. Under the circumstances, its buildfile (XML file) is named Build.xml. Each buildfile contains a <project> and at least one preset <target>, and these targets include many task elements. Each task element has an ID that is used for reference, and this ID must be unique.

Which is used for packing.


For how to configure the Android environment I do not say here, online a lot.


PS: Focus, please end the bench, sit down and watch slowly

2, cocos2dx-3.0 transplant to the Android platform2.1 First look at the folder structure


The first step:

Go to COCOS2DX Library folder path cocos2d-x-3.0\cocos\2d\platform\android\java\src\org\cocos2dx\lib under copy Lib to this folder src\ ORG\COCOS2DX next to the CPP folder

Then we can look at the androidmanifest.xml file under the current folder because I don't understand. So it's just a simple look. We generally do not need to change ^v^

Step Two:

Open the project.properties under the current folder we found there were just two lines of code inside.

# Project target.target=android-10android.library.reference.1=. /cocos2d/cocos/2d/platform/android/java

That target=android-10 represents your API level because I'm using the android4.0 library corresponding to API 14, so I'll change him to android-14. here is a warm little hint, the compiled Android version number is better than the execution of the real machine or simulator low point, he is compatible, but assume that the version number of the compiler than the real machine or the simulator is higher, it will be a lot of unexpected problems. Current folder We're done with a simple process. Go to the JNI folder below to see

Step Three:

After entering the JNI directory. Will find two files with a hellocpp directory, that directory we are too lazy to tube, mainly look at the two suffixes of. mk File

See Application.mk first.

App_stl: = gnustl_staticapp_cppflags: =-frtti-dcc_enable_chipmunk_integration=1-dcocos2d_debug=1-std=c++11- Fsigned-char

There are two lines of code inside. We'll add a line at the end.

App_abi: = Armeabi armeabi-v7a x86
This line of code is very important. He can solve the error message that you can't find cocos2dcpp.so. After adding this line of code, he compiles three versions of the. So file, albeit slowly, without error, assuming that you do not add the error to ignore it. When the mistake is reported, add him.

Coming to see ANDROID.MK.

Local_path: = $ (call My-dir) include $ (clear_vars) Local_module: = Cocos2dcpp_sharedlocal_module_filename: = Libcocos2dcpplocal_src_files: = Hellocpp/main.cpp                    . /.. /classes/appdelegate.cpp.                    /.. /classes/helloworldscene.cpplocal_c_includes: = $ (Local_path)/. /.. /classeslocal_whole_static_libraries: = cocos2dx_staticlocal_whole_static_libraries + = Cocosdenshion_staticLOCAL_ Whole_static_libraries + = Box2d_staticinclude $ (build_shared_library) $ (call import-module,2d) $ (call Import-module, Audio/android) $ (call import-module,box2d)

Let's start with a line to analyze:

Local_path: = $ (call My-dir) include $ (clear_vars) Local_module: = Cocos2dcpp_sharedlocal_module_filename: = Libcocos2dcpp
What do you mean by these four lines, I don't care anyway, just don't move

Local_src_files: = Hellocpp/main.cpp                    . /.. /classes/appdelegate.cpp.                    /.. /classes/helloworldscene.cpp
This is a look that you should be able to add all the. cpp files under the Classes folder here, of course you can choose to join manually. Here we use a script to join

# Traversing folders and subfolders functions define walk$ (wildcard $ (1)) $ (foreach E, $ (wildcard $ (1)/*), $ (call Walk, $ (e))) endef# Traverse Classes folder Allfile S = $ (call walk, $ (local_path)/. /.. /classes) File_list: = hellocpp/main.cpp# Extracts all the. cpp files from all files File_list + = $ (filter%.cpp, $ (allfiles)) Local_src_files: = $ (file_list:$ (Local_path)/%=%)

The. CPP has been added to this, the header file is very easy, we do not need to change

Local_c_includes: = $ (Local_path)/. /.. /classes

Let's look at the static library now.

Local_whole_static_libraries: = cocos2dx_staticlocal_whole_static_libraries + = Cocosdenshion_staticlocal_whole_ Static_libraries + = Box2d_static

These three lines are created by default for us, assuming we want to use the other function modules inside the COCOS2DX, we can join as follows

Local_whole_static_libraries: = cocos2dx_staticlocal_whole_static_libraries + = Cocosdenshion_staticlocal_whole_ Static_libraries + = box2d_staticlocal_whole_static_libraries + cocos_extension_static

Look at the last line when we are using the Cocos extension, we need to add in, anyway, according to the above format plus can be.

Finally, import.

Include $ (build_shared_library) $ (call import-module,2d) $ (call Import-module,audio/android) $ (call Import-module, box2d)

corresponding to the Local_whole_static_libraries module above. Assuming the above extension is added, it's going to change to this.

Include $ (build_shared_library) $ (call import-module,2d) $ (call Import-module,audio/android) $ (call Import-module, BOX2D) $ (call import-module,extensions)

Here is the topic of adding a dependent library to a VS, 3.0 Create project by default to us to join the Libaudio, Libchipmunk, libcocos2d these three modules, generally have these three modules we also enough. Suppose you want to use an extension, join by method



Select the "Workaround" right -click "Join"---> "Existing Project" to choose an expansion pack and then you are sure. Then check our project name for example my project named "Runping" right-click "Reference"


Then click on "New Reference" to add our new hook, OK.




This is the end of the join library under VS, and we continue to go back to our Android platform porting topic. Above for the complete contents of the Android.mk file such as the following:

Local_path: = $ (call My-dir) include $ (clear_vars) Local_module: = Cocos2dcpp_sharedlocal_module_filename: = libcocos2dcpp# functions that Traverse folders and subfolders define walk$ (wildcard $ (1)) $ (foreach E, $ (wildcard $ (1)/*), $ (call Walk, $ (e))) endef# traversal Clas Ses folder Allfiles = $ (call walk, $ (local_path)/. /.. /classes) File_list: = hellocpp/main.cpp# Extracts all the. cpp files from all files File_list + = $ (filter%.cpp, $ (allfiles)) Local_src_files: = $ (file_list:$ (Local_path)/%=%) Local_c_includes: = $ (local_path)/... /.. /classeslocal_whole_static_libraries: = cocos2dx_staticlocal_whole_static_libraries + = Cocosdenshion_staticLOCAL_ Whole_static_libraries + = Box2d_staticlocal_whole_static_libraries + cocos_extension_staticinclude $ (BUILD_SHARED_ LIBRARY) $ (call import-module,2d) $ (call Import-module,audio/android) $ (call import-module,box2d) $ (Call Import-module , extensions)

To this JNI directory is finished. We found another res directory. That was used to change the display image after we installed the APK, I don't care here temporarily

So this time, we need to go into the project folder in CMD and execute build_native.py to generate the so file we need.


After you perform OK, you will have several more folders, such as the following folder structure diagram:


More than one Gen directory (self-generated), Libs directory (used to store libcocos2dcpp.so), obj directory (see name is the intermediate file generated during compilation), Bin directory (last. apk package location), Assets directory (storage resource file)

This time. Suppose you don't want to use eclipse. Ability to execute commands directly

Cocos Run-p Android-j 4
Assuming no error, the APK file will be generated below the bin and you can copy it directly to the real machine. However, it is impossible to debug the problem, so we use eclipse. We open eclipse, let's say you're not Chinese. Please follow the link below to run, so easy so fast eclipse Han process click on "File"-----> "Import" Photo selection



Click "Next", select "Browse" in the interface that appears and select the Android layer of the project we want to open, then click "Finish"



Now that our project is open, he will compile on his own initiative, assuming that there are errors will be reported in the console or Logcat. Suppose you change the code or change something in the file browser below vs. Ability to right-click Project-----> Refresh, or

Tap Project form------> Clean up So far I don't know how to debug the project in eclipse, because I don't know where the Android program is. I hope you can tell me that at this time it is possible to have a small red fork in front of the main.cpp in the JNI, and we click in to find that he will prompt a bunch of

Unresolved inclusion: "Appdelegate.hunresolved inclusion:" Cocos2d.hunresolved inclusion: "CCEventType.h ...
Anyway, it means you can't find these header files. But these header files do exist and the paths are right, and we need to set the project----> Properties-----> C + + General in order to get the project compiled by the following diagram.


It would be nice to remove that hook. So that the project can be compiled and passed, then we need to put the generated APK package into the simulator or the real machine to perform the viewing effect

We right-click on the project-----> Execute Mode-----> android Application This assumes that your genymotion has opened an emulator. A selection box will appear to let you choose which top to install



From can see the label 1 is my real machine, the label 2 is my simulator, casually choose one can see the effect!

If something goes wrong, look at the error in Logcat and take the initiative to resolve the error yourself.

I have a bug, compile and execute no errors, but a run to the phone or emulator has been black screen, I tried all kinds of methods are fruitless, and finally found that my appdelegate inside the start function and the standard COCOS2DX in a little different

BOOL Appdelegate::applicationdidfinishlaunching () {    //Initialize Director    Auto Director = Director:: getinstance (); Auto Glview = Director->getopenglview (); if (!glview) {Glview = Glview::create ("Run Pig");//glview = Glview::create ("Run Pig"); Glview->setframesize (360,640);d Irector->setopenglview (Glview);glview-> Setdesignresolutionsize (720, 1280x720, Resolutionpolicy::show_all);}        I put the following code in addition to reutrn true to put all the above if inside, and then execute on Android has been black screen also no error, under win normal. Tortured for almost 2 days. Attention here, do not make mistakes like my SB//Turn on display fpsdirector->setdisplaystats (TRUE);//Set FPS. The default value is 1.0/60 if you don't call Thisdirector->setanimationinterval (1.0/60);//Create a scene. It's an autorelease objectauto scene = Welcomescene::scene ();//auto scene = Helloworld::createscene ();//rundirector- >runwithscene (scene);    return true;}
The article is written here, the eloquence is not good, the blog will persist in writing, just to record my little bit drops



cocos2dx-3.0 (21) transplant Android platform It's a lot of tears.

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.