Friday 28 March 2014

Step by Step Executing android NDK Programs in ubuntu 11.40(Linux)

The Android NDK is a companion tool to the Android SDK that lets you build performance-critical portions of your apps in native code. It provides headers and libraries that allow you to build activities, handle user input, use hardware sensors, access application resources, and more, when programming in C or C++. If you write native code, your applications are still packaged into an .apk file and they still run inside of a virtual machine on the device. The fundamental Android application model does not change.
Using native code does not result in an automatic performance increase, but always increases application complexity. If you have not run into any limitations using the Android framework APIs, you probably do not need the NDK.

Requirement to Execute the NDK Programs:
1. android sdk
2. android ndk-r7
You can download ndk from
3.setup the sdk in eclipse.

Steps to excecute the NDK:

Step1: Crete a android project in eclipse Eg:NDK

Step 2: Create the folder in our project name jni .and don't create the libs and obj folder in project those are automatically generated by ndk tool
Step 3: create a package in src in my case /NDK/src/com.srikanth

Step 4:creating a NativeCode class writing native methods: /NDK/src/com/srikanth/

package com.srikanth;

public class NativeCode {

static {

* Adds two integers, returning their sum
public native int add( int v1, int v2 );

* Returns Hello World string
public native String hello();


native method C file generator
using javah tool

Eecuting the NativeCode class using javah.Go to Terminal>
>cd goto your project direcotry
NDK>cd bin/classes
classes>javah -jni com.srNativeCode
the javah toll genrate the com_srikanth_NativeCode.h in classes folder copy the file in project jni folder. (NDK/jni/com_srikanth_NativeCode.h)

it look like below:
/* DO NOT EDIT THIS FILE - it is machine generated */
/* Header for class com_srikanth_NativeLib */

#ifndef _Included_com_srikanth_NativeCode
#define _Included_com_srikanth_NativeCode
#ifdef __cplusplus
extern "C" {
* Class: com_srikanth_NativeLib
* Method: add
* Signature: (II)I
JNIEXPORT jint JNICALL Java_com_srikanth_NativeCode_add
(JNIEnv *, jobject, jint, jint);

* Class: com_srikanth_NativeLib
* Method: hello
* Signature: ()Ljava/lang/String;
JNIEXPORT jstring JNICALL Java_com_srikanth_NativeCode_hello
(JNIEnv *, jobject);

#ifdef __cplusplus

step 6: Providing the native methods definition in ndk_demo.c file under NDK/jni/ndk_demo.c

#include "com_srikanth_NativeCode.h"

JNIEXPORT jstring JNICALL Java_com_srikanth_NativeCode_hello
(JNIEnv * env, jobject obj) {
return (*env)->NewStringUTF(env, "Hello World!");

JNIEXPORT jint JNICALL Java_com_srikanth_NativeCode_add
(JNIEnv * env, jobject obj, jint value1, jint value2) {
return (value1 + value2);

Step 7: Creating making file under jni dir (ie.NDK/jni/ code

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := ndk_demo
LOCAL_SRC_FILES := ndk_demo.c


Step 8:Building the ndk. Downloaded ndk extrace in any directory .My case ie in /home/android/android-ndk-r7. Goto the android-ndk-r7/prebuild/linu-x86/bin
under you can find file awk cheange the name to awk_

Step 9:
open terminal

> cd Projectdir

you will get the follwing file in your project

Compile thumb : ndk_demo <= ndk_demo.c

SharedLibrary :

Install : => libs/armeabi/

Refresh the project one ndk tool buld the folling folder and file (libs,obj)

step 10:creting ui for test this ndk. main.xml (/NDK/res/layout/main.xml)

Step 11: (/NDK/src/com/srikanth/
package com.srikanth;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class NDKActivity extends Activity {
/** Called when the activity is first created. */
NativeCode nativeLib;
public void onCreate(Bundle savedInstanceState) {
nativeLib = new NativeCode();
String helloText = nativeLib.hello();

// Update the UI
TextView outText = (TextView) findViewById(;

// Setup the UI
Button buttonCalc = (Button) findViewById(;

buttonCalc.setOnClickListener(new OnClickListener() {
TextView result = (TextView) findViewById(;
EditText value1 = (EditText) findViewById(;
EditText value2 = (EditText) findViewById(;

public void onClick(View v) {
int v1, v2, res = -1;
v1 = Integer.parseInt(value1.getText().toString().trim());
v2 = Integer.parseInt(value2.getText().toString().trim());

res = nativeLib.add(v1, v2);
result.setText(new Integer(res).toString());


Step 12:
Run the NDK Project

No comments: