1

According to Java JNI Specification Native Method Arguments:

The JNI interface pointer is the first argument to native methods. The JNI interface pointer is of type JNIEnv. The second argument differs depending on whether the native method is static or nonstatic. The second argument to a nonstatic native method is a reference to the object. The second argument to a static native method is a reference to its Java class.

So a native function implementation should contains at least two local argument:first is JNIEnv, second is jobject or jclass, but in android framework there are many codes conflits with these rule, for example:

android.googlesource.com/platform/frameworks/base/+/cd92588/media/java/android/media/MediaScanner.java#1822

private static native final void native_init();

android.googlesource.com/platform/frameworks/base/+/master/media/jni/android_media_MediaScanner.cpp#375

static void android_media_MediaScanner_native_init(JNIEnv *env)
{
    ALOGV("native_init");
    jclass clazz = env->FindClass(kClassMediaScanner);
    if (clazz == NULL) {
        return;
    }

  fields.context = env->GetFieldID(clazz, "mNativeContext", "J");
    if (fields.context == NULL) {
        return;
    }
}

function registe:

{
    "native_init",
    "()V",
    (void *)android_media_MediaScanner_native_init
},

android_media_MediaScanner_native_init only receive one argument, so is there any dalvik or art runtime tricks or other reason?

willyout
  • 11
  • 2
  • 3
    That looks like bad code to me. It appears to be relying on the platform's calling convention to get away with omitting the `jclass` argument to `android_media_MediaScanner_native_init()`. It also seems to make little sense - the code attempts to locate a class using `kClassMediaScanner`. Assuming that's the `MediaScanner` class that this code is a member of, extra code had to be written to cache a value *that the JVM passes*. – Andrew Henle Apr 11 '17 at 10:45
  • somebody left a comment, but the comment disappeared, i don't know why, original comment is: it much like this [link](https://pastebin.com/70Up22Hc) – willyout Apr 11 '17 at 13:48
  • @AndrewHenle thanks. – willyout Apr 11 '17 at 14:37

0 Answers0