Gaps between method signature and actual call

When using the Java interface on android, I made two stupid mistakes that cost me a lot of time.

The presence of this id method:

jmethodID myMethod_methodID = env->GetMethodID(hello_Cls, "myMethod", "(ILjava/lang/String;Ljava/lang/String;I)Z"); 

My first mistake caused it with

 env->CallVoidMethod 

and my second error caused it like this:

 jboolean rv = jenv->CallBooleanMethod(hello_obj, myMethod_methodID, myfirst_jstring, mysecond_jstring, 1); 

where there was clearly no jint argument between myMethod_methodID and myfirst_jstring .

It took me a long time to track these errors because there was no relevant output in logcat and the only behavior did nothing (it didn't even crash).

So the question is: How to get more meaningful errors for such errors?

+6
source share
1 answer

This page may be useful: http://www.netmite.com/android/mydroid/dalvik/docs/jni-tips.html , especially:

JNI does very little error checking. Calling SetFieldInt in the Object field will succeed. The goal is to minimize overhead, assuming that if you wrote it in your own code, you probably did it for performance reasons.

Some virtual machines support extended verification with the -Xcheck: jni flag. If the flag is set, the VM places another function table in the JavaVM and JNIEnv pointers. These functions perform an extended series of checks before calling the standard implementation.

Some things that can be checked:

  • Check for null pointers if they are not allowed.
  • Check the validity of the argument type (jclass is a class object, jfieldID points to the field data, jstring points to java.lang.String).
  • Field type validity, for example. do not save the HashMap in the String field.
  • Check if an exception is expected for calls where pending exceptions are not legal.
  • Checking calls for inappropriate functions between critical get / release calls.
  • Ensure that JNIEnv structures are not shared between threads.
  • Make sure that local links are not used beyond their lifetime.
  • UTF-8 lines contain valid "modified UTF-8 data."

The availability of methods and fields (i.e. public vs. private) is not checked.

Following the link: http://www.netmite.com/android/mydroid/dalvik/docs/embedded-vm-control.html

You can configure adb as follows:

 adb shell setprop dalvik.vm.checkjni true adb shell setprop dalvik.vm.execution-mode int:debug 
+4
source

Source: https://habr.com/ru/post/886386/


All Articles