2. Create a simple Java wrapper file named NativeLib.java
package com.pt;3. Create C header file by running javah -jni
/**
* @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();
}
javah -jni com.pt.NativeLib4. Write implement C file for com_pt_NativeLib.h
mv mv com_pt_NativeLib.h ../jni/
#include "com_pt_NativeLib.h"5. To build library, create file Android.mk to lead compilers how to build
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);
}
6. Create folder inLOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := ndk_sample LOCAL_SRC_FILES := ndk_sample.c include $(BUILD_SHARED_LIBRARY)
APP_PROJECT_PATH := $(call my-dir)/project7. Create symbol link to the project by typing
APP_MODULES := ndk_sample
ln -s ~/home/Android/NDKSampleProj /apps/ndk_demo/project
8. Finally, run make APP=ndk_sample that will generate libs/armeabi/libndk_sample.so9. Enjoy the coding now with native code.