0

I'm working on an Android Studio project that involves JNI to run some signal processing functions on arrays created in Java. I need to be able to feed C an array of double from Java, run the filter, and send Java back the array. The filtering function works fine in the Android environment via CMake. Conducting tests, I am able to get arrays of type double from C to Java with no issue. I have a function to send the array from Java to C. My problem is, I am not quite sure how to get that array to my filtering function to send it back to Java.

The method, public static native double addArray(double []arr) in java, is coded accordingly in my algorithm.c file:

jdouble JNICALL
Java_com_example_covid19_MainActivity_addArray(JNIEnv *env, jclass clazz, jdoubleArray jarr) {

static double dv[2890080];
static double y_fil[2890080];

jdouble *arr = (*env)->GetDoubleArrayElements(env, jarr, NULL);
double res=0;
int size = (*env)->GetArrayLength(env, jarr);
for(int i=0;i<size;i++)
    dv[i] = arr[i];


(*env)->ReleaseDoubleArrayElements(env, jarr, arr, 0);

filtering(dv, y_fil);

   return res;

}

At the point in the function above when I call filtering(dv, y_fil), if I was able to send the dv array back to Java, I would be golden. But sending an array back to Java requires a separate JNICALL. I'm perplexed from here and though I have seen many, many examples online of handling data from C to Java, I've not seen one that involves getting data from Java first. Any insight would be greatly appreciated.

wnjl
  • 162
  • 1
  • 10
  • 1
    Does this answer your question? [How to return an array from JNI to Java?](https://stackoverflow.com/questions/1610045/how-to-return-an-array-from-jni-to-java) – Botje Oct 03 '20 at 17:34
  • For me, it was the other way...I wanted to return an array from Java to JNI to use in my C code. For now, setting the contents of the array returned in JNI to the global variable I created in C seems to work. – wnjl Oct 04 '20 at 16:38
  • Now I'm really confused. Your code demonstrates you know how to read Java arrays from C, yet you "want to return an array from Java to JNI" now, while your question wants to "to send the dv array back to Java". Which is it? – Botje Oct 04 '20 at 17:09
  • And I thought `filtering` was "your code". Is there other code that needed access to the Java data? That was not clear from your post. – Botje Oct 04 '20 at 17:12
  • Also note that any change you make to `arr` are copied back to the Java array once you release it. – Botje Oct 04 '20 at 17:23
  • I'm sorry for explaining it poorly. So, what I want to be able to do is bring an array from Java to C, use the filtering function, and send the array back. However, I had no idea that by releasing the array, you send it back to java with any change you make to it, so that may simplify my code quite a bit. I'll try implementing that now. – wnjl Oct 05 '20 at 20:43
  • 1
    You can also share a direct `ByteBuffer` between Java and C++ to avoid the (potential) copy entirely – Botje Oct 05 '20 at 20:59
  • Is there a computational advantage (or is it just better practice) to share a ByteBuffer back and forth versus sending the array? – wnjl Oct 05 '20 at 23:58
  • It avoids copies entirely. But your Java code must use a `ByteBuffer` and not a `byte[]` – Botje Oct 06 '20 at 04:57

0 Answers0