Outdated Docs > How to build and run HelloWorld on Android NDK r5 and above

How to build and run HelloWorld on Android NDK r5 and above

This document describes how to run a simple ‘HelloWorld’ sample under NDK with Windows (using Cygwin), or Linux.
Your NDK version must be r5 or above.

Setting Up Development Environment

NOTE:

This document doesn’t describe how to set up NDK. Please refer to the following website if you wish to learn more about the Android NDK, and how to download and install it:
http://developer.android.com/sdk/ndk/overview.html

Your Cygwin’s version should be 1.7 or above

This is described in the NDK documentation.

NOTE

We recommend you to use Eclipse as your IDE when programming for Android, not only for cocos2d-x projects, but all other apps as well, because it is the IDE that Google supports. The Android tutorials on this site will also be using Eclipse for its examples, making it easier to follow. Other IDEs are fine, but we do not discuss them here.

2. Compiling

You should define environment variable NDK_ROOT before compiling.

“NDK_ROOT” means the path of NDK you installed in.

Linux

To compile the project, simply run build_native.sh in your shell. In Linux, this simply means opening up a shell prompt such as bash.

Here is an example:

cd /usr/workspace/cocos2d-x/samples/HelloWorld/proj.android/
./build_native.sh

This assumes that you put cocos2d-x in /usr/workspace, and left HelloWorld in its default location.

Windows using Cygwin

The same applies for Windows, but you will need to use Cygwin’s bash, rather than the command prompt. This is because build_native.sh is a shell script meant for Linux, which calls a Linux-based tool included in Android-NDK. If you were to create an NDK app without using cocos2d-x, you would still have to call the tool, ndk_build, from Cygwin. build_native simply automates the process of building the project.

Here is an example:

bash
cd /cygdrive/c/cocos2d-x/samples/HelloWorld/proj.android
./build_native.sh

This assumes that you put cocos2d-x in C:/, and left HelloWorld in its default location.

3. Running

Running Helloworld is the same as other ndk samples.

3.1 Build Project

This step will generate R.java automatically, it will also compile the java code and generate the .apk.

3.2 Clean Project

If you recompile the native code and want to pack the .so to .apk, you should do this step. Step 1 will not pack the .so to .apk except the first time.

3.3 Run Project

Right click the project, select “run as”, then select “Android Application”. Don’t forget to start your emulator before running.

4. Warning

You should do “clean project” after recompiling native code. If not, the latest .so will not be
packed into .apk.

cocos2d-android-ndk-helloworld.PNG (111.9 kB) zhangxm, 2011-12-20 22:03

Copyright © 2010 - 2014 Cocos2d-x.org Support Email: support@cocos.org