NDK First Simple Program
Java Code to Call Native Method
step one : Load SystemLib:
static {
System.loadLibrary("manojsamplelibrary");
}
Step Two: Call below in your onCreate Method
int size =25;
int arr[] = new int[size];
for (int i = 0; i < size; i++) {
arr[i] = i+10;
// Log.i("***", ""+arr[i]);
}
int sum = getHelloSum(arr,size);
Step Three: Declare native getHelloSum Method
public native int getHelloSum(int[] val,int size);
Step Four: Write hello-jni.c
Java_com_manoj_hellojni_HelloJni_getHelloSum(JNIEnv *env, jobject obj,
jintArray arr,jint size) {
jint buf[size];
jint i, sum = 0;
(*env)->GetIntArrayRegion(env, arr, 0, size, buf);
for (i = 0; i < size; i++) {
sum += buf[i];
}
return sum;
}
Step Five : How to Debug NDK Code
1. __android_log_write(ANDROID_LOG_INFO, "TAG", "getHelloSum Method is Called");
or
2. LOGI("Array Size = (%d) ", size);
step one : Load SystemLib:
static {
System.loadLibrary("manojsamplelibrary");
}
Step Two: Call below in your onCreate Method
int size =25;
int arr[] = new int[size];
for (int i = 0; i < size; i++) {
arr[i] = i+10;
// Log.i("***", ""+arr[i]);
}
int sum = getHelloSum(arr,size);
Step Three: Declare native getHelloSum Method
public native int getHelloSum(int[] val,int size);
Step Four: Write hello-jni.c
Java_com_manoj_hellojni_HelloJni_getHelloSum(JNIEnv *env, jobject obj,
jintArray arr,jint size) {
jint buf[size];
jint i, sum = 0;
(*env)->GetIntArrayRegion(env, arr, 0, size, buf);
for (i = 0; i < size; i++) {
sum += buf[i];
}
return sum;
}
Step Five : How to Debug NDK Code
1. __android_log_write(ANDROID_LOG_INFO, "TAG", "getHelloSum Method is Called");
or
2. LOGI("Array Size = (%d) ", size);
http://www.ntu.edu.sg/home/ehchua/programming/android/Android_NDK.html
ReplyDelete