1. Download Android-NDK (i'm using
android-ndk-r8-crystax-1)
2. Create a simple Java wrapper file named
NativeLib.java
package com.pt;
/**
* @author khanh
*
*/
public class NativeLib {
static {
System.loadLibrary("ndk_sample");
}
/**
* Adds two integers, returning their sum
*/
public native int add(int v1, int v2);
/**
* Returns Hello World string
*/
public native String hello();
}
3. Create C header file by running
javah -jni
javah -jni com.pt.NativeLib
mv mv com_pt_NativeLib.h ../jni/
4. Write implement C file for
com_pt_NativeLib.h
#include "com_pt_NativeLib.h"
JNIEXPORT jstring JNICALL Java_com_marakana_NativeLib_hello
(JNIEnv * env, jobject obj) {
return (*env)->NewStringUTF(env, "Hello World!");
}
JNIEXPORT jint JNICALL Java_com_marakana_NativeLib_add
(JNIEnv * env, jobject obj, jint value1, jint value2) {
return (value1 + value2);
}
5. To build library, create file
Android.mk to lead compilers how to build
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := ndk_sample
LOCAL_SRC_FILES := ndk_sample.c
include $(BUILD_SHARED_LIBRARY)
6. Create folder in
/apps/ndk_sample/ and create Application.mk
APP_PROJECT_PATH := $(call my-dir)/project
APP_MODULES := ndk_sample
7. Create symbol link to the project by typing
ln -s ~/home/Android/NDKSampleProj /apps/ndk_demo/project
8. Finally, run
make APP=ndk_sample that will generate
libs/armeabi/libndk_sample.so
9. Enjoy the coding now with native code.