gnychis gnychis - 1 year ago 85
Android Question

What is "jobject this" in JNI and what is it used for?

I am having a hard time finding an answer to this. But, what is "jobject this" used for in JNI function calls? For example:

jobjectArray Java_com_gnychis_awmon_Test( JNIEnv* env, jobject this ) {

I use
to allocate objects often, but I've never used thiz and I'm not sure what it is for. Just for knowledge purposes.

Answer Source

The following is a JNI wrapper function which has two parameters, and returns a primitive array of objects:

jobjectArray Java_com_gnychis_awmon_Test( JNIEnv* env, jobject thiz );

From the function name you have given I don't think it is complete, that is, you haven't respected the obligatory function name convention which is:

  1. Start the function with Java_

  2. Append the package name separated by _ (undescores) i.e. com_company_awesomeapp. So far the function name is composed of: Java_com_company_awesomeapp

  3. Append the Java class name where the native method has been defined, followed by the actual function name. So at this point we should have the following function name: Java_com_company_awesomeapp_MainActivity_Test

The first parameter is a pointer to a structure storing all JNI function pointers, i.e. all the predefined functions you have available after you #include <jni.h>.

The second parameter is a reference to the Java object inside which this native method has been declared in. You can use it to call the other methods of the Java object from the current JNI function, i.e. Call Java instance methods from JNI code written in C or C++.

If for example you have the following Java class inside the file:

public class MainActivity extends Activity
        catch (UnsatisfiedLinkError ule)
            Log.e(TAG, "WARNING: Could not load native library: " + ule.getMessage());

    public static native Object[] Test();

Then, the jobject thiz parameter of the JNI function would be a reference to an object of type MainActivity.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download