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?
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.
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.
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.
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With