[英]JNI. different behavior between different Android OS versions
my application runs following code : 我的应用运行以下代码:
void BmrDeviceInfo_convertToC(JNIEnv *pEnv, jobject jBmrDeviceInfo, BmrDeviceInfo& cBmrDeviceInfo){
__android_log_print(ANDROID_LOG_INFO, "BEAMER_JNI", "g_classBmrDeviceInfo is = %s", (g_classBmrDeviceInfo == NULL) ? "NULL" : "OK"); //g_classBmrDeviceInfo is initialize on JNI_OnLoad
jfieldID fieldName = pEnv->GetFieldID(g_classBmrDeviceInfo, "m_strName", "Ljava/lang/String;"); // OK for Android 4.2 and crash for Android 3.1 or less
..................................
}
And LogCat output for crash case: 和崩溃案例的LogCat输出:
04-17 16:28:44.118: I/BEAMER_JNI(446): g_classBmrDeviceInfo is = OK
04-17 16:28:44.118: W/dalvikvm(446): JNI WARNING: 0x4053dc70 is not a valid JNI reference
04-17 16:28:44.118: W/dalvikvm(446): in Lcom/xxxxxxx/xxxxxx/controller/CoreController;.Init (Lcom/xxxxxxx/xxxxxx/listviews/DeviceInfo;Ljava/lang/String;)I (GetFieldID)
04-17 16:28:44.118: I/dalvikvm(446): "main" prio=5 tid=1 RUNNABLE
04-17 16:28:44.118: I/dalvikvm(446): | group="main" sCount=0 dsCount=0 obj=0x4001f1a8 self=0xce48
04-17 16:28:44.118: I/dalvikvm(446): | sysTid=446 nice=0 sched=0/0 cgrp=default handle=-1345006528
04-17 16:28:44.118: I/dalvikvm(446): | schedstat=( 276745040 468907344 84 )
Thanks for the help! 谢谢您的帮助!
JNI functions that accept objects require local or global refs. 接受对象的JNI函数需要本地或全局引用。 Pre-ICS these were raw pointers, but in ICS that changed to a table index system. 前ICS这些是原始指针,但在ICS中改为表索引系统。
You don't say what version was used to generate the output in the question. 您没有说明在问题中使用了什么版本来生成输出。 The hex value 0x4053dc70 looks like a raw pointer, so I'm assuming this is pre-ICS. 十六进制值0x4053dc70看起来像一个原始指针,所以我假设这是pre-ICS。 Looking at the error message, it appears that g_classBmrDeviceInfo
is invalid; 查看错误消息,似乎g_classBmrDeviceInfo
无效; a common way to get this wrong is to fail to use NewGlobalRef
to convert a local reference to a global reference. 解决这个问题的常见方法是无法使用NewGlobalRef
将本地引用转换为全局引用。
Generally speaking, JNI became more strict in ICS, so it's peculiar that this would succeed in 4.x but fail in 3.x, unless you're playing around with weak globals. 一般来说,JNI在ICS中变得更加严格,所以它在4.x中成功但在3.x中失败是特殊的,除非你正在玩弱全局变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.