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 foldercontain 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.

device.png