Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android JNI method - Is the second parameter jclass or jobject?

Tags:

android-ndk

Folks,

In my Android Java code, I have a declaration as follows:

public class SurfacePanelNative extends SurfaceView implements SurfaceHolder.Callback {
   ...
   private static native void native_render();
}

In my native code, I have the function declared as:

void native_render(JNIEnv *env, jobject javaSurface) {
   ANativeWindow* window = ANativeWindow_fromSurface(env, javaSurface);
   ...
}

Looking at some examples on the net, it appears that the function should be declared as:

void native_render(JNIEnv *env, jclass clazz) {
   ...
}

I am wondering which declaration is the right one.

I am thinking the first one is the right one. Otherwise, I don't have enough information to obtain javaSurface.

I would appreciate it if someone can shed some light on this.

Thank you in advance for your help.

Regards,
Peter

like image 948
Peter Avatar asked Sep 18 '13 00:09

Peter


People also ask

What is Jobject JNI?

jobject thiz means the this in java class. Sometimes if you create a static native method like this. void Java_MyClass_method1 (JNIEnv *, jclass); jclass means the class itself. Follow this answer to receive notifications.

What is Jclass?

jclass is a reference to the current class. if the JNI function arguments have jobject , then this JNI function corresponds to the Java side instance method (the native methods declared without "static").

What is the JNI in android?

JNI is the Java Native Interface. It defines a way for the bytecode that Android compiles from managed code (written in the Java or Kotlin programming languages) to interact with native code (written in C/C++).

What is Jclass in Java?

JClass is typically implemented in one of two ways: by wrapping a java. lang. Class or by parsing a source file directly with a tool such as javadoc or javelin. If a JClass represents an inner class, its getParent() method returns the outer class. Otherwise, it returns the containing package.


1 Answers

It is jclass if the method is static, otherwise jobject. If you use javah, as the JNI designers intended, you will always get the right answer.

like image 129
user207421 Avatar answered Sep 25 '22 00:09

user207421



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!