问题描述
Android 的 JNI 提示页面提到了这个 FAQ:为什么 FindClass 没有找到我的课程?他们提到了多种解决方案,最后一个选项是这个:
Android's JNI tips page mentions this FAQ: Why didn't FindClass find my class? They mention multiple solutions and the last option there is this one:
在方便的地方缓存对 ClassLoader 对象的引用,然后发出loadClass 直接调用.这需要一些努力.
Cache a reference to the ClassLoader object somewhere handy, and issue loadClass calls directly. This requires some effort.
所以,我试图让它工作,但似乎无论如何,这种方法对我来说根本不起作用.最终,我想出了如何使用 ClassLoader 但如果我从本机线程尝试加载尚未触摸/加载的类,它将无法工作.本质上,它在从本机线程调用时的行为与 env->FindClass 相同,但它不会为已在应用程序中使用的类返回 0.任何想法,如果我没有做对,或者不可能从尚未使用/加载的本机线程访问类.
So, I tried to get it working and it seems that no matter what, this method simply does not work for me. Eventually, I figured how to use ClassLoader but it won't work if from a native thread I try to loadClass that hasn't been touched/loaded yet. Essentially, it's the identical to env->FindClass in behavior when called from a native thread, with the exception that it won't return 0 for classes that were already use in the app. Any idea if I didn't get it right, or it's impossible to access classes from a native thread that weren't used/loaded yet.
我将提供更多信息来解释我的意思.有常规的 JNI env->FindClass(className)
,还有一个我写的 myFindClass(env, className)
使用缓存的 ClassLoader->loadClass
.
I'll give more info to explain what exactly I mean. There is regular JNI env->FindClass(className)
, and another one that I wrote myFindClass(env, className)
that uses cached ClassLoader->loadClass
.
我试图从本机 c/c++ 访问的类是com/noname/TestClient".在 myFindClass 中,我还使用 env->FindClass 并记录它返回的值:
The class that I'm trying to access from native c/c++ is "com/noname/TestClient". Inside myFindClass I also use env->FindClass and log value that it returns:
jclass myFindClass(JNIEnv * env, const char* name)
{
...
jclass c0 = env->FindClass(name);
jclass c1 = (jclass)env->CallObjectMethod(ClassLoader,
MID_loadClass, envNewStringUTF(name));
dlog("myFindClass("%s") => c0:%p, c1:%p, c0 and c1 are same: %d",
name, c0, c1, env->IsSameObject(c0, c1));
...
}
那么,我有这 3 个组合来解释这个问题.
Then, I have these 3 combinations to explain the issue.
1)
//inside JNI_OnLoad thread
myFindClass(env, "com/noname/TestClient");
...
//inside native thread created by pthread_create
myFindClass(env, "com/noname/TestClient");
我得到了这个 logcat:
I get this logcat:
myFindClass("com/noname/TestClent") => c0:0x41b64558, c1:0x41b64558,c0 和 c1 相同:1
...
myFindClass("com/noname/TestClent") => c0:0,c1:0x41b64558,c0和c1相同:0
myFindClass("com/noname/TestClent") => c0:0x41b64558, c1:0x41b64558, c0 and c1 are same: 1
...
myFindClass("com/noname/TestClent") => c0:0, c1:0x41b64558, c0 and c1 are same: 0
2)
//inside JNI_OnLoad thread
env->FindClass("com/noname/TestClient");
...
//inside native thread created by pthread_create
myFindClass("com/noname/TestClient");
我得到了这个 logcat:
I get this logcat:
myFindClass("com/noname/TestClent") => c0:0, c1:0x41b64558, c0 和 c1 相同:0
myFindClass("com/noname/TestClent") => c0:0, c1:0x41b64558, c0 and c1 are same: 0
3)
//inside JNI_OnLoad thread
//"com/noname/TestClient" isn't touched from JNI_OnLoad.
...
//inside native thread created by pthread_create
myFindClass(env, "com/noname/TestClient");
我得到了这个 logcat:
I get this logcat:
myFindClass("com/noname/TestClent") => c0:0, c1:0, c0 和 c1 相同:1
myFindClass("com/noname/TestClent") => c0:0, c1:0, c0 and c1 are same: 1
基本上,我的问题是 ClassLoader 在第三种情况下找不到我的课程.它是一个错误吗?有什么办法可以解决这个问题?
Basically, my issue is that ClassLoader doesn't find my class in the 3rd case. Is it a bug? What can be done to fix the problem?
最重要的是,似乎 ClassLoader::loadClass 显然是错误的.如果我问 myFindClass("noname/TestClent") 那么它会返回一些垃圾,当我以任何方式使用返回的 jclass 时,应用程序就会崩溃.
On top of that, it seems that ClassLoader::loadClass is plainly buggy. If I ask myFindClass("noname/TestClent") then it returns some garbage, and when I use that returned jclass in any way the app crashes.
推荐答案
经过多次尝试和我的应用程序崩溃后,我和一位同事设法在另一个本地线程中缓存并成功使用类加载器.我们使用的代码如下所示(C++11,但很容易转换为 C++2003),发布在这里,因为我们找不到上述在方便的地方缓存对 ClassLoader 对象的引用,并发出 loadClass直接调用.这需要一些努力.".当从与 JNI_OnLoad 线程不同的线程调用时,调用 findClass 可以完美运行.我希望这会有所帮助.
After much trying and crashing of my app, a colleague and I managed to cache and succesfully use the class loader in another, native, thread. The code we used is shown below (C++11, but easily converted to C++2003), posted here since we couldn't find any examples of the aforementioned "Cache a reference to the ClassLoader object somewhere handy, and issue loadClass calls directly. This requires some effort.". Calling findClass worked perfectly when called from a thread different from the one of JNI_OnLoad. I hope this helps.
JavaVM* gJvm = nullptr;
static jobject gClassLoader;
static jmethodID gFindClassMethod;
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *pjvm, void *reserved) {
gJvm = pjvm; // cache the JavaVM pointer
auto env = getEnv();
//replace with one of your classes in the line below
auto randomClass = env->FindClass("com/example/RandomClass");
jclass classClass = env->GetObjectClass(randomClass);
auto classLoaderClass = env->FindClass("java/lang/ClassLoader");
auto getClassLoaderMethod = env->GetMethodID(classClass, "getClassLoader",
"()Ljava/lang/ClassLoader;");
gClassLoader = env->CallObjectMethod(randomClass, getClassLoaderMethod);
gFindClassMethod = env->GetMethodID(classLoaderClass, "findClass",
"(Ljava/lang/String;)Ljava/lang/Class;");
return JNI_VERSION_1_6;
}
jclass findClass(const char* name) {
return static_cast<jclass>(getEnv()->CallObjectMethod(gClassLoader, gFindClassMethod, getEnv()->NewStringUTF(name)));
}
JNIEnv* getEnv() {
JNIEnv *env;
int status = gJvm->GetEnv((void**)&env, JNI_VERSION_1_6);
if(status < 0) {
status = gJvm->AttachCurrentThread(&env, NULL);
if(status < 0) {
return nullptr;
}
}
return env;
}
这篇关于来自 Android JNI 中任何线程的 FindClass的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!