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);
.
.
.}