Reputation: 31
I am trying to call System.getProperty(String) from native code. but I got this error while trying to run on android 4.1.2:
JNI ERROR: (app bug): accessed stale local reference ......... (index .... in a table of size 0)
The source code is as below:
JNIEXPORT jstring JNICALL Java_org_morphone_sense_device_DeviceSense_getArchNative
(JNIEnv *pEnv, jobject pObj) {
jclass SCls = (*pEnv)->FindClass(pEnv, "java/lang/System");
if (!SCls)
return NULL;
jmethodID getPropertyMID = (*pEnv)->GetStaticMethodID(pEnv, SCls, "getProperty", "(Ljava/lang/String;)Ljava/lang/String;");
jobject property = (*pEnv)->CallStaticObjectMethod(pEnv, SCls, getPropertyMID, "os.version");
}
Anyone knows how to fix this error?
Upvotes: 0
Views: 2388
Reputation: 31
I found the problem. Instead of passing the string to CallStaticObjectMethod, I need to construct a jstring instance then pass it instead. The working code should be:
JNIEXPORT jstring JNICALL Java_org_morphone_sense_device_DeviceSense_getArchNative
(JNIEnv *pEnv, jobject pObj) {
jclass SystemCls = (*pEnv)->FindClass(pEnv, "java/lang/System");
jstring propertyName = (*pEnv)->NewStringUTF(pEnv, "os.arch");
jmethodID getPropertyMID = (*pEnv)->GetStaticMethodID(pEnv, SystemCls, "getProperty", "(Ljava/lang/String;)Ljava/lang/String;");
jstring property = (*pEnv)->CallStaticObjectMethod(pEnv, SystemCls, getPropertyMID, propertyName);
if (property!=NULL) {
__android_log_print(ANDROID_LOG_INFO, "Morphone", "Got property");
return property;
}
else {
__android_log_print(ANDROID_LOG_INFO, "Morphone", "Error: Cannot get property");
return "";
}
}
Upvotes: 2
Reputation: 310860
Change
public native String getArchNative();
to
public String getArchNative()
{
return System.getProperty("os.version");
}
and throw the JNI code away. Don't create work for yourself, or extra source files that need to be maintaned.
I would remove 'Native' from the method name too if it is possible at this stage.
Upvotes: 1