Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android static jni method with first jint argument received wrong value

I have declared a static native method in this way:

public native static void test(int w, int h);

And then I declared it in JNI in the way:

void testJni(JNIEnv* env, jint w, jint h)

Strangely, the w always received a value looks like a pointer, seems the jclass/jobject is passed to the first argument w.

Instead if the first argument is not a jint, say a jstring, things would be working fine.

Can somebody explain why this is happening? Thanks.

like image 386
Bill Randerson Avatar asked Nov 17 '25 07:11

Bill Randerson


1 Answers

Your native declaration is incorrect. The second argument for static method is jclass, that corresponds to java class where method is placed.

Java:

public native static void test(int w, int h);

Native:

void testJni(JNIEnv* env, jclass clazz, jint w, jint h)

For native instance methods second arg is jobject, that corresponds to java's this

Java:

public native void test(int w, int h);

Native:

void testJni(JNIEnv* env, jobject thiz, jint w, jint h)
like image 74
Sergio Avatar answered Nov 19 '25 21:11

Sergio



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!