Android NDK Step 1: Setup Environment and Write HelloWorld app

1. Introduction

“Android NDK(Nativ Development Kit) is a toolset that allows you to implement parts of your app using native-code languages such as C and C++ “(See: The first question pelople  ask about NDK is:  why do we need NDK?

Well, Java is obviously a simple and elegant OO programming beauty. It is also powerful enough for most of Android application development. However, Java is not perfect when it comes to performance and memory management. If your application requires extreme performance , such as games, you might need to consider using NDK because sometimes native code still runs faster. In addition, there are a lot of well-written legacy code in C/C++ that people want to keep using.

The technology behind Android NDK is JNI(Java Native Interface). JNI is designed to enable Java and native code communicating with each other. We can either use JNI to write native methods implemented in languages like C/C++ and then callled by Java, or use JNI’s invocation interface to invoke Java methods in native application. Android NDK obviously takes advantage of the former feature of JNI.

2. Building development environment

It’s time to get our hands dirty. The first thing is installing all the software tools we need. Since I already have Android Eclipse environment on my Windows 8 laptop, let’s start with installing C/C++ support first.

2.1 Install C/C++ support for Eclipse

The easiest way to setup C/C++ environment is installing Eclipse CDT (C/C++ Development Tooling). In case you don’t know how to do it, please read this tutorial:

2.2 Install Android NDK

– Go to this site: , download right version of NDK package for your OS platform. In my case,

– After uncompressing the .zip file, we put the file folder to somewhere we like. For example, D:Android_NDK

– Add the directory of NDK folder to enviroment variable PATH.

3. Creating Android NDK Hello World project

It’s time to write our first NDK application

3.1  Create a new Android  Application Project called HelloNDK with a auto-generated Activity called


3.2 Modify the into the following one:

package com.example.hellondk;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {

	static {
		// load our native module written in C/C++ code

	// The native method that returns a Java String
	public native String getMessage();

	protected void onCreate(Bundle savedInstanceState) {

		// Create a TextView and show the String
		TextView textView = new TextView(this);


3.3 Generate C/C++ header using “Javah”

– Create a new folder under project root directory called jni (NOTE: the name must be jni. Otherwise, it doen’t work) and subfolder called include under jni directory.

– Open Windows command prompt, change to the HelloNDK project directory and then type the command:

javah -classpath bin/classes;D:Android_SDKplatformsandroid-16android.jar -o jni/include/HelloNdk.h com.example.hellondk.MainActivity

We can see how to use  javah command by simply typing javah in the command prompt.


Here -classpath needs two parameters: bin/classes where .class file of is kept and  its superclass which is kept in android.jar under the Android SDK (you can choose an appropriate Android version depending on your android device).

After successfully generating the header file, go to eclipse and refresh the project. You will see HelloNdk.h file under include directory.

In this file, we can see following generated native method declaration.

 * Class:     com_example_hellondk_MainActivity
 * Method:    getMessage
 * Signature: ()Ljava/lang/String;
JNIEXPORT jstring JNICALL Java_com_example_hellondk_MainActivity_getMessage
  (JNIEnv *, jobject);

3.4 Implementing native method in C/C++

Here I create a new .c file under jni directory and implement our simple native method.

#include "include/HelloNdk.h"

JNIEXPORT jstring JNICALL Java_com_example_hellondk_MainActivity_getMessage
          (JNIEnv *env, jobject thisObj) {
   // simply create a new string and return it
   return (*env)->NewStringUTF(env, "Hello from native code!");

3.5 Create an Android make file called (The name must be

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := myModule


In this .mk file, we mainly look at the module name (e.g., myModule) and corresponding c code file (e.g., HelloNdk.c).

4. Test our HelloNdk application

Open the windows command prompt and make sure current directory is the HelloNDK project directory.

Type following command to build the project


After build is successful, we can run Android project in Eclipse as usual.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s