How to run cpp-tests on Android
Environment Requirements
- 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.
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)
-
If not, you need to download and install Java before proceeding JDK
-
Download the Android SDK.
ADT Bundle For Mac
ADT Bundle For Windows (32-bit)
ADT Bundle For Windows (64-bit)
ADT Bundle For Linux (32-bit)
ADT Bundle For Linux (64-bit)
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 (orcmd
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
-
Install and verify
Apache Ant
Hombrew
brew install ant
-
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:
*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. (usuallycocos2d-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:
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.
Troubleshootings
-
After configuring the
setup.py
file, you still sufferNDK_ROOT
not found issue, you maybe missed executingsource ~/.bash_profile
. -
If you are on Windows pc, you should be careful about the difference of the filesystem conventions.