How to run cpp-tests on Android¶
- A Supported OS (Ubuntu 12.10+, OS X 10.7+, Windows 7+)
- Cocos2d-x v3.0 (https://github.com/cocos2d/cocos2d-x/)
- JDK/SDK 1.6+
- NDK r9d+
- Apache Ant
- Python 2.7.5
We will be using OS X in this example. The process on Win32 and Linux is more or less the same.
Download cocos2d-x and unzip it. We could simply unzip it on the root directory of your home folder. After unzipping, double click the cocos2d-x folder and you should have a structure like this:
Download JDK, SDK and NDK¶
For developing Android games Java is a must have toolkit.
- check your Java version.
- Hopefully you see results similar to: (although your version may be slightly different.)
java version "1.8.0_05" Java(TM) SE Runtime Environment (build 1.8.0_05-b13) Java HotSpot(TM) 64-Bit Server VM (build 25.5-b02, mixed mode)
If not, you need to download and install Java before proceeding JDK
The bundle includes the newest Android SDK version plus an Eclipse version with Android Development Tool included. This is all you need.
After downloading it, unzip the package at
~/AndroidDev directory. The folder contains two folders:
You can launch Eclipse and install other SDK versions, if needed. Here is an example:
- Download NDK. Always use the latest version. This article version is r9d.
After downloading it, unzip the package at the same location of the Android SDK. In our case, it is under the
Verify Your Environment¶
*Python 2.7*is installed and it is accessible:
python --versionin your Terminal (or
cmdon Win32) and it will give you the following result:
$ python --version Python 2.7.5
If not, you need to download and install Python before proceeding.
brew install python
Install and verify
brew install ant
verify that Ant is found, simply execute:
$ ant Buildfile: build.xml does not exist! Build failed
Setup Environment Variables¶
setup.pyto configure your Android development environment. This will set the necessary environment variables needed. If you haven't configured this environment before, you will be prompted to enter paths for variables that are not found.
Caution: You must not use the
~sign. Use the full path to your home directory. Otherwise, the scripts will fail due to error path value.
python setup.pyand you will get the following results:
*COCOS2D_CONSOLE_ROOT* environment variable to point to the bin directory under ~/cocos2d-x/tools/cocos2d-console directory.
*NDK_ROOT* environment variable to point to the location of where you put the NDK. (i.e android-ndk-r9d/)
*ANDROID_SDK_ROOT* environment variable to point to the location of where you put the
/Users/guanghui/AndroidDev/adt-bundle-mac-x86_64-20130522/sdk/. The adt-bundle-mac-x86_64-xxxx, the xxxx number maybe different. So please note this non-trival difference.
*ANT_ROOT* environment variable to point to the location of where you put
apache-ant-x.x.x, the x.x.x number maybe different. So please note this non-trival difference.
When all environment variables are correctly configured, you should let them take effect.¶
On *nix systems, you could issue the following commands:
on win32 system, you can just close the command line windows and restart it.
android-build.py to build cocos2d-x samples
- Change your directory to the where the
android-build.pyscript is located. (usually
android list targets
id in this next screen shot.
Now I can execute:
python android-build.py -p 19 cpp-tests
That's it! The script will handle everything else that you need. When finished you should get the following message:
How to deploy it on your Android phone via command line¶
Enable USB Debugging on your phone and then connect your phone via USB.
Change your directory to the the bin directory of testcpp android project:
(Note:If your current directory is build, you could use some relative path like this
- Then you could use adb to install the apk to your android phone:
adb install TestsDemo-debug.apk
- If it prompts you that
adbis not a command, you must re-equery your
~/.bashrcfile. Ensure it contains the 4 environment variables set above. You could also manually execute:
- You should see the following screenshot! You are done.
After configuring the
setup.pyfile, you still suffer
NDK_ROOTnot found issue, you maybe missed executing
If you are on Windows pc, you should be careful about the difference of the filesystem conventions.