Showing posts from April, 2013

NDK : how to return String, Int From Native Code

Step one : Return String from your code

jstring Java_com_manoj_hellojni_HelloJni_stringFromJNI(JNIEnv* env,
        jobject thiz) {
    return (*env)->NewStringUTF(env, "stringFromJNI !");

Step Two : Return integer from your code

Java_com_manoj_hellojni_HelloJni_getTwoNumMul(JNIEnv *env, jobject obj,
        jint first, jint second) {
     jint mul = first*second;
    return mul;

NDK First Simple Program

Java Code to Call Native Method
step one : Load SystemLib:  
   static {

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");
   2.  LOGI("…