我想使用反射调用通过onCreate()
调用JNI
方法,也许是这样:
static {
System.loadLibrary("native-lib");
}
public native void onCreate(Bundle savedInstanceState);
现在我已经实现了原生cpp代码的一部分,如下所示:
Java_com_hack_nativeoc_MainActivity_onCreate(JNIEnv *env, jobject thiz, jobject bundle)
{
jclass thisClass = (*env).GetObjectClass(thiz);
jclass thisClassGlobal = reinterpret_cast<jclass>(env->NewGlobalRef(thisClass));
static jobject thizglobal = (*env).NewGlobalRef(thiz);
jclass superClass=(*env).GetSuperclass(thisClass);
jclass superClassGlobal = reinterpret_cast<jclass>(env->NewGlobalRef(superClass));
jmethodID superOnCreate = (*env).GetMethodID(superClassGlobal, "onCreate", "(Landroid/os/Bundle;)V");
(*env).CallNonvirtualVoidMethod(thizglobal,superClassGlobal,superOnCreate,bundle);
jclass rclass = env->FindClass("com/nisl/hack/nativeoc/R$layout");
jclass globalClass = reinterpret_cast<jclass>(env->NewGlobalRef(rclass));
jfieldID activityID = env->GetFieldID(globalClass,"activity_main","I");
jint activityMain = env->GetIntField(globalClass,activityID);
jmethodID setContentView = (*env).GetMethodID(thisClassGlobal,"setContentView","(Landroid/view/View;)V");
(*env).CallVoidMethod(thizglobal,setContentView,activityMain);}
但是当我实施setContentView(R.layout.activity_main)
时,它会显示以下错误:
E/art: JNI ERROR (app bug): attempt to use stale weak global reference 0x7f04001b (should be 0x10001b)
A/art: art/runtime/java_vm_ext.cc:410] JNI DETECTED ERROR IN APPLICATION: use of deleted weak global reference 0x7f04001b
如何解决此错误? 以下是详细错误消息的一小部分:
12-05 11:14:53.346 13617-13617/hackit.selina.com.nativeoc E/art: JNI ERROR (app bug): accessed stale weak global reference 0x7f09001b (index 16390 in a table of size 361)
12-05 11:14:53.361 13617-13617/hackit.selina.com.nativeoc A/art: art/runtime/java_vm_ext.cc:410] JNI DETECTED ERROR IN APPLICATION: use of deleted weak global reference 0x7f09001b
12-05 11:14:53.361 13617-13617/hackit.selina.com.nativeoc A/art: art/runtime/java_vm_ext.cc:410] from void hackit.selina.com.nativeoc.MainActivity.onCreate(android.os.Bundle)
12-05 11:14:53.361 13617-13617/hackit.selina.com.nativeoc A/art: art/runtime/java_vm_ext.cc:410] "main" prio=5 tid=1 Runnable
12-05 11:14:53.361 13617-13617/hackit.selina.com.nativeoc A/art: art/runtime/java_vm_ext.cc:410] | group="main" sCount=0 dsCount=0 obj=0x740452a0 self=0xb4db6500
12-05 11:14:53.361 13617-13617/hackit.selina.com.nativeoc A/art: art/runtime/java_vm_ext.cc:410] | sysTid=13617 nice=0 cgrp=default sched=0/0 handle=0xb6f8bb34
12-05 11:14:53.361 13617-13617/hackit.selina.com.nativeoc A/art: art/runtime/java_vm_ext.cc:410] | state=R schedstat=( 302916881 9052184 98 ) utm=17 stm=13 core=0 HZ=100
12-05 11:14:53.361 13617-13617/hackit.selina.com.nativeoc A/art: art/runtime/java_vm_ext.cc:410] | stack=0xbe176000-0xbe178000 stackSize=8MB
12-05 11:14:53.361 13617-13617/hackit.selina.com.nativeoc A/art: art/runtime/java_vm_ext.cc:410] | held mutexes= "mutator lock"(shared held)
12-05 11:14:53.361 13617-13617/hackit.selina.com.nativeoc A/art: art/runtime/java_vm_ext.cc:410] native: #00 pc 00370e01 /system/lib/libart.so (art::DumpNativeStack(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, int, char const*, art::ArtMethod*, void*)+160)
12-05 11:14:53.361 13617-13617/hackit.selina.com.nativeoc A/art: art/runtime/java_vm_ext.cc:410] native: #01 pc 0035046f /system/lib/libart.so (art::Thread::Dump(std::__1::basic_ostream<char, std::__1::char_traits<char> >&) const+150)
12-05 11:14:53.361 13617-13617/hackit.selina.com.nativeoc A/art: art/runtime/java_vm_ext.cc:410] native: #02 pc 0025a725 /system/lib/libart.so (art::JavaVMExt::JniAbort(char const*, char const*)+740)
12-05 11:14:53.361 13617-13617/hackit.selina.com.nativeoc A/art: art/runtime/java_vm_ext.cc:410] native: #03 pc 0025ae7b /system/lib/libart.so (art::JavaVMExt::JniAbortF(char const*, char const*, ...)+74)
12-05 11:14:53.361 13617-13617/hackit.selina.com.nativeoc A/art: art/runtime/java_vm_ext.cc:410] native: #04 pc 00351231 /system/lib/libart.so (art::Thread::DecodeJObject(_jobject*) const+708)
12-05 11:14:53.361 13617-13617/hackit.selina.com.nativeoc A/art: art/runtime/java_vm_ext.cc:410] native: #05 pc 0032cd0d /system/lib/libart.so (art::InvokeVirtualOrInterfaceWithVarArgs(art::ScopedObjectAccessAlreadyRunnable const&, _jobject*, _jmethodID*, std::__va_list)+412)
12-05 11:14:53.361 13617-13617/hackit.selina.com.nativeoc A/art: art/runtime/java_vm_ext.cc:410] native: #06 pc 00278619 /system/lib/libart.so (art::JNI::CallVoidMethodV(_JNIEnv*, _jobject*, _jmethodID*, std::__va_list)+344)
12-05 11:14:53.361 13617-13617/hackit.selina.com.nativeoc A/art: art/runtime/java_vm_ext.cc:410] native: #07 pc 00110cfd /system/lib/libart.so (art::CheckJNI::CallMethodV(char const*, _JNIEnv*, _jobject*, _jclass*, _jmethodID*, std::__va_list, art::Primitive::Type, art::InvokeType)+928)
12-05 11:14:53.361 13617-13617/hackit.selina.com.nativeoc A/art: art/runtime/java_vm_ext.cc:410] native: #08 pc 00111f95 /system/lib/libart.so (art::CheckJNI::CallVoidMethodV(_JNIEnv*, _jobject*, _jmethodID*, std::__va_list)+36)
12-05 11:14:53.361 13617-13617/hackit.selina.com.nativeoc A/art: art/runtime/java_vm_ext.cc:410] native: #09 pc 00000bcb /data/app/hackit.selina.com.nativeoc-2/lib/arm/libnativeit.so (_JNIEnv::CallVoidMethod(_jobject*, _jmethodID*, ...)+82)
12-05 11:14:53.361 13617-13617/hackit.selina.com.nativeoc A/art: art/runtime/java_vm_ext.cc:410] native: #10 pc 00000989 /data/app/hackit.selina.com.nativeoc-2/lib/arm/libnativeit.so (Java_hackit_selina_com_nativeoc_MainActivity_onCreate+156)
12-05 11:14:53.361 13617-13617/hackit.selina.com.nativeoc A/art: art/runtime/java_vm_ext.cc:410] native: #11 pc 000eaa29 /system/lib/libart.so (art_quick_generic_jni_trampoline+40)
12-05 11:14:53.361 13617-13617/hackit.selina.com.nativeoc A/art: art/runtime/java_vm_ext.cc:410] native: #12 pc 00774d41 /data/dalvik-cache/arm/system@framework@boot.oat (???)
12-05 11:14:53.361 13617-13617/hackit.selina.com.nativeoc A/art: art/runtime/java_vm_ext.cc:410] at hackit.selina.com.nativeoc.MainActivity.onCreate(Native method)
12-05 11:14:53.361 13617-13617/hackit.selina.com.nativeoc A/art: art/runtime/java_vm_ext.cc:410] at android.app.Activity.performCreate(Activity.java:6251)
12-05 11:14:53.361 13617-13617/hackit.selina.com.nativeoc A/art: art/runtime/java_vm_ext.cc:410] at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
12-05 11:14:53.361 13617-13617/hackit.selina.com.nativeoc A/art: art/runtime/java_vm_ext.cc:410] at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
12-05 11:14:53.361 13617-13617/hackit.selina.com.nativeoc A/art: art/runtime/java_vm_ext.cc:410] at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
12-05 11:14:53.361 13617-13617/hackit.selina.com.nativeoc A/art: art/runtime/java_vm_ext.cc:410] at android.app.ActivityThread.-wrap11(ActivityThread.java:-1)
12-05 11:14:53.361 13617-13617/hackit.selina.com.nativeoc A/art: art/runtime/java_vm_ext.cc:410] at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
12-05 11:14:53.361 13617-13617/hackit.selina.com.nativeoc A/art: art/runtime/java_vm_ext.cc:410] at android.os.Handler.dispatchMessage(Handler.java:102)
12-05 11:14:53.361 13617-13617/hackit.selina.com.nativeoc A/art: art/runtime/java_vm_ext.cc:410] at android.os.Looper.loop(Looper.java:148)
12-05 11:14:53.361 13617-13617/hackit.selina.com.nativeoc A/art: art/runtime/java_vm_ext.cc:410] at android.app.ActivityThread.main(ActivityThread.java:5417)
12-05 11:14:53.361 13617-13617/hackit.selina.com.nativeoc A/art: art/runtime/java_vm_ext.cc:410] at java.lang.reflect.Method.invoke!(Native method)
12-05 11:14:53.361 13617-13617/hackit.selina.com.nativeoc A/art: art/runtime/java_vm_ext.cc:410] at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
12-05 11:14:53.361 13617-13617/hackit.selina.com.nativeoc A/art: art/runtime/java_vm_ext.cc:410] at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
12-05 11:14:53.361 13617-13617/hackit.selina.com.nativeoc A/art: art/runtime/java_vm_ext.cc:410]
12-05 11:14:53.515 13617-13617/? A/art: art/runtime/runtime.cc:366] Runtime aborting...
12-05 11:14:53.516 13617-13617/? A/art: art/runtime/runtime.cc:366] Aborting thread:
12-05 11:14:53.516 13617-13617/? A/art: art/runtime/runtime.cc:366] "main" prio=5 tid=1 Native
12-05 11:14:53.516 13617-13617/?
在JNI Tips中,我发现有一个 NewGlobalRef 可以解决这个问题,我试过但它没有用,有人可以帮助我吗?