0

wanting to work with NDK, I had no luck with android studio(till now I don't get the point of indicating the NDK path since I do everything in terminal outside of IDE and no code completion), I switched to eclipse which makes it more easier to work with jni and ndk dev.

To begin, I created a project to sum a 2d array of integer in c and return the sum to java side. I can't get it to work. can you help?!!

my code in C is:

 #include <jni.h>
JNIEXPORT jint JNICALL Java_com_example_jninew_MainActivity_getNum(JNIEnv *env, jobject obj, jintArray arr)
{
  int i,j, sum = 0;
  jsize width = (*env)->GetArrayLength(env, arr);
  jintArray *line = (*env)->GetIntArrayElements(env, arr, 0);
   for (i=0; i<width; i++){

       jint *pos = (*env)->GetIntArrayElements(env, line, i);
       for (j=0; j<height; j++){
               sum += pos[i][j];
          }
   }
   (*env)->ReleaseIntArrayElements(env, arr, line, 0);
     return sum;
}

My java Code is:

package com.example.jninew;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView textView = (TextView) findViewById(R.id.tv);
        int[][] a = {{1,2},{3,4}};
        textView.setText("sum is: "+getNum(a));


    }
    static{
        System.loadLibrary("getNum");
    }
    native int getNum(int[][] a);
.
.
.}
yanisB
  • 89
  • 1
  • 11
  • Is `JNIEXPORT jint JNICALL Java_com_example_jninew_MainActivity_getNum(JNIEnv *env, jobject obj, jintArray arr)` really the output from `javah`? I'd expect to see `..., jobjectArray arr)` since a two-dimensional array is an array of array objects. See http://stackoverflow.com/a/6752105/4756299 – Andrew Henle Jan 29 '16 at 17:48
  • and how can I generate that file with javah? – yanisB Jan 29 '16 at 17:51
  • You need to use GetObjectArrayElement to get each `int[]` out of the `int[][]`, then GetIntArrayElements on that. – fadden Jan 29 '16 at 18:16
  • @yanisB *and how can I generate that file with javah?* How did you use `javah` to generate the proper signature for your native functions in the first place? – Andrew Henle Jan 29 '16 at 19:32
  • i think `sum += pos[i][j];` is wrong, it should be `sum += pos[j];` – milevyo Jan 29 '16 at 19:52

1 Answers1

0

i think it should be like this:

   #include <jni.h>
JNIEXPORT jint JNICALL Java_com_example_jninew_MainActivity_getNum(JNIEnv *env, jobject obj, jintArray arr)
{
  int i,j, sum = 0;
  jsize width = (*env)->GetArrayLength(env, arr);

   for (i=0; i<width; i++){

        jintArray *line =   (*env)->GetObjectArrayElement(env, arr, i);
        int height =       (*env)->GetArrayLength(env, line);
        jint *pos = (*env)->GetIntArrayElements(env, line, 0);

        for (j=0; j<height; j++){
               sum += pos[j];
          }
        (*env)->ReleaseIntArrayElements(env, arr, pos, 0);
        (*env)->ReleaseIntArrayElements(env, arr, line, 0);
   }


     return sum;
}
milevyo
  • 2,165
  • 1
  • 13
  • 18
  • 1
    Some sort of explanation of why you believe this code solves the OP's problem would be nice. That said, the third argument to `GetIntArrayElements` should be a `jboolean*`, not an `int`. And you should probably call `ReleaseIntArrayElements` on `pos` to make sure that it gets unpinned. – Michael Jan 29 '16 at 20:08
  • @Michael, i don't have much idea about jni, and ReleaseIntArrayElements was my guess too, but i am not so sure about it. you can reedit my code if you wish, that helps much – milevyo Jan 29 '16 at 20:14
  • thanks, I got to to work. I have a new question here: [link](http://stackoverflow.com/questions/35103879/jni-return-2d-array-c-to-java) – yanisB Jan 30 '16 at 16:06
  • @milevyo...can you help with my new question in the link above please? – yanisB Jan 30 '16 at 19:42