Android SDK ! (Wow its great. Google is serving the best as per its habit 🙂 ). But how about some native code development.Yes google Android SDK also provides facility to let their developer develop code in native language. (Most of the cases C/C++).Android NDK (Native Development Kit) simplifies the task.
Well I will explain you how to create a simple application (I use the word simple so it must be Hello World 😉 ).Follow the below given steps to deploy your first native code in Android.
2) Get that things installed on your work station. Installation Guide
3) Create a project in Eclipse named HelloWorld.
4) Create a folder jni in your project directory. HelloWorld/jni
JNI (Java Native Interface) is the utility which allows us to write native code.
5) Replace your main.xml file to this.
<?xml version=”1.0″ encoding=”utf-8″?>
5) Open your .java file and write following lines of code. and build the project.
public class HelloWorld extends Activity
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState)
public native String messageFromNativeCode();
6) Create a header file.
Its time to do some stuff on terminal.
Go to your bin directory using cd command
run following commands
- javah com.testndk.HelloWorld and
- mv com_testndk_HelloWorld.h ../jni/
This will create a header file in jni folder of your project.There will be some auto generated code in your header file. having a close look at the content of header file, you can see that the method name which we have defined in our .java file as a native method is converted in a C language compatible form.
7) Write code in C
Now we need to write a body for the function which is declared in header file, which will be something like this.
JNIEXPORT jstring JNICALL Java_com_testndk_HelloWorld_messageFromNativeCode
(JNIEnv * env, jobject jObj)
return (*env)->NewStringUTF(env, “Hello World!”);
We need to return a string from the function so written type will be jstring.
- env is a JNIEnv kind of pointer and second argument is just a simple text which we want to return.
8) Create Android.mk file
Create it in your jni folder. <project-name>/jni/Android.mk
Android.mk file is written to describe your source to the build system. System internally parses this file for various number of times for its own purpose.
LOCAL_PATH := $(call my-dir)
LOCAL_MODULE := HelloWorld
LOCAL_SRC_FILES := helloworld.c
9) Compile the file.
Now if you have put your project in Android NDK’s repository than on terminal go to the project directory and type ../ndk-build
If you are maintaining separate workspace than copy your project to Android NDK repository. On terminal change your current directory to your project name folder.
If everything works fine you will get a following output.
You will get two folders generated named libs and obj you can explore that folder and you will find that <filename>.so file is created.This file is source file which System.loadLibrary(“”) will use.
10) Final Output
Build the project and run it on emulator.
You will have a below given output,and with this output we are done with our first NDK exmple.
Creating an HelloWorld Program in any technology is always an easy task 😉 😉 😉
Keep visiting for new tutorials on NDK.