Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JNI error when trying to access the EXTRA_ADDRESS_BOOK_INDEX field

I am trying to access the EXTRA_ADDRESS_BOOK_INDEX constant using JNI:

JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved) {
    JNIEnv* env = nullptr;
    vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6);
    jclass clazz = env->FindClass("android/provider/ContactsContract$Data");
    jfieldID fieldID = env->GetStaticFieldID(clazz, "EXTRA_ADDRESS_BOOK_INDEX", "Ljava/lang/String;");
    jobject field = env->GetStaticObjectField(clazz, fieldID);
}

The GetStaticObjectField method crashes with an error:

java_vm_ext.cc:534] JNI DETECTED ERROR IN APPLICATION: static jfieldID 0x6fd191b0 not valid for class java.lang.Class<android.provider.ContactsContract$Data>

On the other hand if I try to access the VISIBLE_CONTACTS_ONLY field on the same class, everything works as expected:

jfieldID fieldID = env->GetStaticFieldID(clazz, "VISIBLE_CONTACTS_ONLY", "Ljava/lang/String;");

Any clues what I might be doing wrong?

like image 684
Darin Dimitrov Avatar asked May 25 '18 07:05

Darin Dimitrov


People also ask

How do you fix a JNI error has occurred Please check your installation and try again?

JNI Error in Windows CMD To fix the error, you need to adjust the environment variables for Java. To do this, type “environment variable” in your Windows search and open the “Edit System Environment Variables” item.

What is JNI error in selenium?

This typically happens when your JDK version and JRE version are not compatible or when the required JRE version is not installed. To correct the error use Window -> preferences -> compiler -> compiler compliance level and change the JRE version to existing JRE version.

What is JNA vs JNI?

Java Native Access (JNA) is a community-developed library that provides Java programs easy access to native shared libraries without using the Java Native Interface (JNI). JNA's design aims to provide native access in a natural way with a minimum of effort. Unlike JNI, no boilerplate or generated glue code is required.


1 Answers

So I tried to solve it and getting the same error, then I searched the source code of the final class ContactsContract and found that the field EXTRA_ADDRESS_BOOK_INDEX is not declared in the nested class Data but in a nested interface called ContactCounts.

So the solution is to replace "android/provider/ContactsContract$Data" by "android/provider/ContactsContract$ContactCounts"

like image 54
E.Abdel Avatar answered Sep 30 '22 15:09

E.Abdel