How to run/execute native C/C++ code in android using NDK in linux operating system

Step 1 : Install NDK (nothing but just extract the zip file)

Step 2 : Create jni folder inside the project root folder

Step 3 : Create file inside jni folder which should contain the following content

/****************** starts here **************************************************/

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := hellojni
LOCAL_SRC_FILES := hellojni.c


/****************** ends here **************************************************/

where hellojni is the name of the your c file.

Step 4 : Create a hellojni.c file using JNI syntax.

For a simple hellojni.c file Please refer the file below

	/****************** hellojni.c starts here **************************************************/

	#include <string.h>
	#include <jni.h>

	jstring Java_com_example_MainActivity_stringFromJNI(JNIEnv* env,jobject thiz)
	    return (*env)->NewStringUTF(env, "Hello from JNI !");

	/****************** hellojni.c ends here **************************************************/

Please note the follwing things

1) stringFromJNI is the name of the native method say function in C language.
2) MainActivity is the activity name in which you have declared the above method.
3) com.example represents the package name of the activity

Note: The above method if executed will return “Hello from JNI !” string.

Step 5 : Declare the method stringFromJNI in MainActivity as native as shown below

         public native String stringFromJNI();

Step 6 : Load the library “hellojni” in your MainActivity inside a static block as shown below


Step 7 : Open Terminal

Go to the project path
To know the project path —-> Right Click On Project –> Properties –> Location
Copy it and execute the following command

Eg: cd <project path>Press Enter

Now Extract the ndk zip file which you have downloaded before and open it. You should find a file named ndk-build.
Now copy the path of your ndk and execute the following command

Eg: <ndk path>/ndk-build Press Enter

The above command will compile your hellojni.c and generates a shared library in the jni folder of your project
Output should be as follows —->

Compile thumb : hellojni <= hellojni.c

SharedLibrary :

Install : => libs/armeabi/

Step 8: Right Click On Project –> Refresh
You should observe the following

1) obj folder which contains local –> armeabi
armeabi folder –> objs –>
objs folder –> hellojni
hellojni folder –> hellojni.o and hellojni.o.d files.

2) libs folder(will be created if not exists before) –> armeabi –>

The above scenario indicates that your hellojni.c file is being compiled to library file ie by CPU architecture(armeabi) of your device and it is being added to your build path.

Step 9 : Call the native method inside your activity as shown below

        String message = stringFromJNI();

Step 10: Run the project

The  stringFromJNI method will return “Hello from JNI !” .

Note: You should require min SDK Version 1.5 to use NDK 


About krishjlk

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: