Developers Manual > Cocos2d-x > Getting Started with Cocos2d-x > How to run cpp-tests on Android

How to run cpp-tests on Android

Environment Requirements

We will be using OS X in this example. The process on Win32 and Linux is more or less the same.

Prerequisite

Download Cocos2d-x

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.
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)

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: *sdk* and *eclipse*.

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 *~/AndroidDev* directory.

Verify Your Environment

  • verify that *Python 2.7* is installed and it is accessible:

    python --version in your Terminal (or cmd on 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.

    Hombrew brew install python

    Python.org

  • Install and verify Apache Ant

    Hombrew brew install ant

    Apache.org

  • verify that Ant is found, simply execute:

$ ant
Buildfile: build.xml does not exist!
Build failed

Setup Environment Variables

  • Run setup.py to 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.

  • In your cocos2d-x directory runpython setup.py and you will get the following results:

setuppy01

*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 adt-bundle. Example /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 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:

source ~/.bash_profile

on win32 system, you can just close the command line windows and restart it.

Use android-build.py to build cocos2d-x samples

  • Change your directory to the where the android-build.py script is located. (usually cocos2d-x/build)
cd build

and then

android list targets

Notice the 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:

buildsuccess

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:

cd ~/cocos2d-x/tests/cpp-tests/proj.android/bin

(Note:If your current directory is build, you could use some relative path like thiscd ../tests/cpp-tests/proj.android/bin)

  • Then you could use adb to install the apk to your android phone:
adb install TestsDemo-debug.apk
  • If it prompts you that adb is not a command, you must re-equery your ~/.bashrc file. Ensure it contains the 4 environment variables set above. You could also manually execute:
 export PATH=$PATH:$ANDROID_SDK_ROOT/tools:$ANDROID_SDK_ROOT/platform-tools
  • You should see the following screenshot! You are done.

runsuccess

Troubleshootings

  • After configuring the setup.py file, you still suffer NDK_ROOT not found issue, you maybe missed executing source ~/.bash_profile.

  • If you are on Windows pc, you should be careful about the difference of the filesystem conventions.

setuppy01.png (53.4 kB) owen, 2014-04-01 09:14

setuppy02.png (68.9 kB) owen, 2014-04-01 09:14

setuppy03.png (92.1 kB) owen, 2014-04-01 09:14

buildandroidpy.png (131.3 kB) owen, 2014-04-01 09:14

buildsuccess.png (56 kB) owen, 2014-04-01 09:14

cocos2dxdirectory.png (19.5 kB) owen, 2014-04-01 09:14

donwloadSDK.png (91.8 kB) owen, 2014-04-01 09:14

runsuccess.png (33.3 kB) owen, 2014-04-01 09:14

android-list-targets.png (40.3 kB) slackmoehrle, 2014-05-07 21:59

android-list-targets1.png (54.8 kB) slackmoehrle, 2014-05-07 22:22

Sign up for our newsletter to keep up with the latest developments, releases and updates for Cocos2d-x.