Deploy Cocos2dx Project to Native Android on Mac
Deploying a game to different platforms always causes various issues. Even though Cocos2dx platform already helps us make those steps easy, sometimes unexpected errors still happen. In this blog, I summarize the steps of deploying Cocos2dx project onto native Android platform.
Step one: Download ADT or Eclipse which is used to run Android project (https://developer.android.com/sdk/index.html?hl=i)
Step two: Download NDK which is used to implement part of your app in native languages such as C or C++ (https://developer.android.com/tools/sdk/ndk/index.html)
Step three: Download Apache Ant which is a Java library and command-line tool that help building software. (http://ant.apache.org/bindownload.cgi)
Step four: Download Cocos2dx if you have not downloaded it yet. (http://www.cocos2d-x.org/download)
Step five: Navigate to Cocos2dx folder (I am using Cocos2dx 3.3rc0 for this blog)
Step six: Step up Cocos2dx by typing ./setup.py
Step seven: Start setup NDK_ROOT path and Apache ANT_ROOT path
(Notify: try to use NDK version r10c instead of r10, because r10 probably have bugs, so it cannot deploy successfully every time. )
Step eight: Execute command: "source .." to make added system variables take effect. (Just type whatever terminal displayed in quotation)
Step nine: Create a new Cocos2dx project by typing following command line in your terminal. (-p is package name, -l is language, -d is project destination folder)
Step ten: Open your created Cocos2dxAndroid project. Then use terminal to navigate to proj.android. And build android native project by typing ./build_native.py
Step eleven: Open native Android project in ADT, and build and run. That's it!!!
However, that is the way you setup and run HelloWorld cocos2dx project. When you start writing your own classes in Xcode and rebuild it, you will probably get errors when you try to run it in ADT or Eclipse. In order to solve this, you need to do some extra steps.
Step one: right click your Android project in ADT or Eclipse -> properties -> c/c++ build -> Environment -> Add..
You need to add NDK_ROOT into your environment.
Step two: After you generate your own classes, you need to tell ADT where are they. Expend your Android project -> jni -> Android.mk. Then add those new classes under LOCAL_SRC_FILES: by following its format
Step three: Then you should be good to build and run. But you need to make sure to run on real device instead of emulator since emulator does not support OpenGL ES.
... Still not end ...
When you next time open ADT or Eclipse, you may get errors like "null pointer.." or "c/c++ index.." that makes you cannot even build the project. In that case, open proj.android file and delete .cproject file (that is a hidden file, so if you cannot find it, just make sure you show the hidden file by typing "defaults write com.apple.finder AppleShowAllFiles YES" in terminal and relaunch the finder).
If you still getting some errors, try the following things:
1. Make sure update "Android Support Repository" and "Android Support Library"
2. Make sure cocos2dx.lib file set to the correct path. The default may wrong.
Right click Android project -> properties -> Java build path -> source -> PROJECTNAME/libcocos2d and then change the path to the correct one.