USE OF NDK:
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
link:http://developer.android.com/sdk/ndk/index.html
Download:android-ndk-r7-linux-x86.tar.bz2
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/NativeCode.java
package com.srikanth;
public class NativeCode {
static {
System.loadLibrary("ndk_demo");
}
/**
* Adds two integers, returning their sum
*/
public native int add( int v1, int v2 );
/**
* Returns Hello World string
*/
public native String hello();
}
step5: 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 */
#include
/* Header for class com_srikanth_NativeLib */
#ifndef _Included_com_srikanth_NativeCode
#define _Included_com_srikanth_NativeCode
#ifdef __cplusplus
extern "C" {
#endif
/*
* 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
}
#endif
#endif
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/Android.mk)
Android.mk code
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := ndk_demo
LOCAL_SRC_FILES := ndk_demo.c
include $(BUILD_SHARED_LIBRARY)
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
NDK>/home/android/android-ndk-r7/ndk-build
you will get the follwing file in your project
Compile thumb : ndk_demo <= ndk_demo.c
SharedLibrary : libndk_demo.so
Install : libndk_demo.so => libs/armeabi/libndk_demo.so
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:NDKAcitive.java (/NDK/src/com/srikanth/NDKActivity.java)
package com.srikanth;
import android.app.Activity;
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;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
nativeLib = new NativeCode();
String helloText = nativeLib.hello();
// Update the UI
TextView outText = (TextView) findViewById(R.id.textOut);
outText.setText(helloText);
// Setup the UI
Button buttonCalc = (Button) findViewById(R.id.buttonCalc);
buttonCalc.setOnClickListener(new OnClickListener() {
TextView result = (TextView) findViewById(R.id.result);
EditText value1 = (EditText) findViewById(R.id.value1);
EditText value2 = (EditText) findViewById(R.id.value2);
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
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
link:http://developer.android.com/sdk/ndk/index.html
Download:android-ndk-r7-linux-x86.tar.bz2
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/NativeCode.java
package com.srikanth;
public class NativeCode {
static {
System.loadLibrary("ndk_demo");
}
/**
* Adds two integers, returning their sum
*/
public native int add( int v1, int v2 );
/**
* Returns Hello World string
*/
public native String hello();
}
step5: 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 */
#include
/* Header for class com_srikanth_NativeLib */
#ifndef _Included_com_srikanth_NativeCode
#define _Included_com_srikanth_NativeCode
#ifdef __cplusplus
extern "C" {
#endif
/*
* 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
}
#endif
#endif
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/Android.mk)
Android.mk code
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := ndk_demo
LOCAL_SRC_FILES := ndk_demo.c
include $(BUILD_SHARED_LIBRARY)
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
NDK>/home/android/android-ndk-r7/ndk-build
you will get the follwing file in your project
Compile thumb : ndk_demo <= ndk_demo.c
SharedLibrary : libndk_demo.so
Install : libndk_demo.so => libs/armeabi/libndk_demo.so
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:NDKAcitive.java (/NDK/src/com/srikanth/NDKActivity.java)
package com.srikanth;
import android.app.Activity;
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;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
nativeLib = new NativeCode();
String helloText = nativeLib.hello();
// Update the UI
TextView outText = (TextView) findViewById(R.id.textOut);
outText.setText(helloText);
// Setup the UI
Button buttonCalc = (Button) findViewById(R.id.buttonCalc);
buttonCalc.setOnClickListener(new OnClickListener() {
TextView result = (TextView) findViewById(R.id.result);
EditText value1 = (EditText) findViewById(R.id.value1);
EditText value2 = (EditText) findViewById(R.id.value2);
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:
Post a Comment