What is the Android NDK?
The Android NDK is a companion tool used only in conjunction with Android SDK which allows application developers to build performance-critical portions of their apps by use of native (C/C++) code.
This provide benefits in form of reuse of existing code and increased speed.
Contents of NDK:
- A set of tools and build files for generating native code libraries from C and C++ sources.
- A way to embed the corresponding native libraries into application package files (.apks) that can be deployed on Android devices.
- A set of native system headers and libraries supported starting from Android 1.5 .
- Documentation, samples, and tutorials.
Download NDK from http://developer.android.com/sdk/ndk/1.6_r1/index.html and uncompress it.Now run the script in <NDK>/build/host-setup.sh.This creates <NDK>/out/host/config.mk which configure NDK for system.
How to write an application using NDK?
Follow underneath steps to create our own application using NDK and make it running on emulator.
Write an Android Activity
You need to include following lines of extra code in our java file.
- Call native method inside on create method.
- Declare native method outside oncreate method.
- Call to “System.loadLibrary†method inside a static initializer block to load our shared library object.
package com.example.FileLoaderActivity;
public class FileLoaderActivity extends Activity {
private static String TAG = “FileLoaderActivity”;
// The name of a test file to write
private static String FILENAME = “hello”;
// The full path to the test file
private static String PATH = “/data/data/com.example.FileLoaderActivity/” + FILENAME;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
……………
// Call the native method to read the file just written
byte[] bytes = loadFile(PATH);
…………..
}
public native byte[] loadFile(String fileName); static {
System.loadLibrary(“fileloader”);
}
}
Generating C/C++ header files For generating C/C++ header file for our shared library:
- Build project which generates Java.class files under <project_home>/bin
- Use javah tool to generate a C/C++ header file.
Issue the following command at the top of <project_home>, my command looks like:
javah -o fileloader.h -classpath bin com.example.FileLoaderActivity.FileLoaderActivity
The above command will generate a file called fileloader.h .
Creating our Shared Library
Create two folders which will contain source code and make files:
- mkdir -p ~/workspace/<project_home>/jni folder – contain the source code for our shared library.
- mkdir -p $NDK_HOME/apps/fileloader folder- “project definition” folder for our shared library
Now copy header file ‘hellondk.h’ from Eclipse project folder to ~/workspace/<project_home>/jni/
For building our NDK project, we need to create two Makefiles which will specify how and from where to pick source code and library file.
- Android.mk
- Application.mk
“~/workspace/<project_home>/jni/Android.mk”
Android.mk file describes source file to build system and its contents are :
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := fileloader
LOCAL_SRC_FILES := fileloader.c
LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog
include $(BUILD_SHARED_LIBRARY)
“$NDK_HOME/apps/fileloader/Application.mk“
Application.mk file describes modules that are needed by your application and it looks like:
APP_PROJECT_PATH := ~/workspace/FileLoaderActivity
APP_MODULES := fileloader
Creating Shared Library Source
Now create C source file for shared library which defines JNI method called by Java file.Signature of this Jni method should match with fileloader.h file generated by eclipse build system.
~/workspace/FileLoaderActivity/jni/fileloader.c
jbyteArray Java_com_example_FileLoaderActivity_FileLoaderActivity_loadFile
(JNIEnv* env, jobject thiz, jstring filename)
{
……………………..
}
Compiling our Shared Library
Change current working directory to $NDK_HOME and run command “make APP=fileloaderâ€.You can find your shared library into folder $NDK_HOME/out/apps/fileloader/ directory.You can now run our FileLoaderActivity project from within Eclipse. Dont forget to push file “hello†mentioned in java code before running project.Here’s a screenshot of what you should see in the emulator.

November 25th, 2010 on 9:23 PM
merci pour le tuto
possible d’avoir les sources en zip ?
March 10th, 2011 on 6:03 PM
Hi!
There is one thing that I does not understand with this NDK, as I understand it, it would not run directly on the CPU on the device, but rather in some kind of emulated mode.
They (Android Team) says “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” on their page http://developer.android.com/sdk/ndk/index.html
The reason I wounder is that I’m really not an Java-programmer, Last time I wrote some Java code I was 10yrs old
May 21st, 2011 on 7:23 PM
I believe this just means that your native code is not a standalone program that runs in its own process. It still runs “inside” the Java virtual machine, which means that it is called from the JVM’s code, but once called it should run directly on the processor, not emulated.
July 4th, 2011 on 5:14 PM
Hi, i am not able to load external stlport library function.Will u plz guide or give me some links to do it.
July 26th, 2011 on 4:09 PM
hi..
i do not play videos in his program.
my code is–
package ashu.exm;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;
public class Video extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
VideoView videoView1 = (VideoView) findViewById(R.id.videoView1);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView1);
String viewSource = “http://v5.cache1.c.youtube.com…”;
// String viewSource=”http://www.youtube.com/watch?v…”;
//Uri video = Uri.parse(viewSource);
videoView1.setVideoURI(Uri.parse(viewSource));
videoView1.setMediaController(new MediaController(this));
videoView1.requestFocus();
videoView1.start();
}
}
plz help me .
August 24th, 2011 on 10:58 AM
Hi my loved one! I wish to say that this post is amazing, nice written and come with approximately all vital infos. I would like to look extra posts like this.
September 28th, 2011 on 10:55 AM
hi,
I am writing JNI code using NDK, for prebuilt .so file, I have cpp
file and header file for this I created the .so file using cygwin on
windows then build the shared library but it is showing unsatisfied
linker error. creation of prebuilt .so file and shared .so file was
proper is wasn’t showing any error. my make file is
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := test
LOCAL_SRC_FILES := libtest.so
include $(PREBUILT_SHARED_LIBRARY)
LOCAL_C_INCLUDES := $(LOCAL_PATH)
LOCAL_MODULE := jni_test
LOCAL_CPP_EXTENSION := .cpp
LOCAL_SRC_FILES := jni_test.cpp
LOCAL_SHARED_LIBRARY := test
include $(BUILD_SHARED_LIBRARY)
please help…
December 27th, 2011 on 6:20 PM
Straight to the point – that’s the way I like it.
December 30th, 2011 on 7:54 PM
Hi, I found your site by mistake when i was searching yahoo for this issue, I have to say your content is in actuality helpful I also love the theme, its amazing!. I dont have that much time to read all your post at the moment but I have bookmarked it and also add your RSS feeds. I will be back in a day or two. thanks for a great website.