From dd79d9bbc2aef026489b04ffc79030b6fcab0e0f Mon Sep 17 00:00:00 2001 From: folecr Date: Fri, 19 Jul 2013 16:26:02 -0700 Subject: [PATCH] Use default class loader to obtain the ClassLoader for this NativeActivity --- cocos2dx/platform/android/jni/JniHelper.cpp | 51 +++++++++++++++++---- cocos2dx/platform/android/jni/JniHelper.h | 5 ++ 2 files changed, 47 insertions(+), 9 deletions(-) diff --git a/cocos2dx/platform/android/jni/JniHelper.cpp b/cocos2dx/platform/android/jni/JniHelper.cpp index 9bf0fbf91f..30d95e42d4 100644 --- a/cocos2dx/platform/android/jni/JniHelper.cpp +++ b/cocos2dx/platform/android/jni/JniHelper.cpp @@ -38,7 +38,6 @@ jclass _getClassID(const char *className) { jstring _jstrClassName = env->NewStringUTF(className); - // explicit cast to jclass, since the returned object jobject _clazzObject = env->CallObjectMethod(cocos2d::JniHelper::classloader, cocos2d::JniHelper::loadclassMethod_methodID, _jstrClassName); @@ -121,10 +120,10 @@ namespace cocos2d { bool JniHelper::setClassLoaderFrom(jobject nativeactivityinstance) { JniMethodInfo _getclassloaderMethod; - if (!JniHelper::getMethodInfo(_getclassloaderMethod, - "android/app/NativeActivity", - "getClassLoader", - "()Ljava/lang/ClassLoader;")) { + if (!JniHelper::getMethodInfo_DefaultClassLoader(_getclassloaderMethod, + "android/app/NativeActivity", + "getClassLoader", + "()Ljava/lang/ClassLoader;")) { return false; } @@ -136,10 +135,10 @@ namespace cocos2d { } JniMethodInfo _m; - if (!JniHelper::getMethodInfo(_m, - "java/lang/ClassLoader", - "loadClass", - "(Ljava/lang/String;)Ljava/lang/Class;")) { + if (!JniHelper::getMethodInfo_DefaultClassLoader(_m, + "java/lang/ClassLoader", + "loadClass", + "(Ljava/lang/String;)Ljava/lang/Class;")) { return false; } @@ -183,6 +182,40 @@ namespace cocos2d { return true; } + bool JniHelper::getMethodInfo_DefaultClassLoader(JniMethodInfo &methodinfo, + const char *className, + const char *methodName, + const char *paramCode) { + if ((NULL == className) || + (NULL == methodName) || + (NULL == paramCode)) { + return false; + } + + JNIEnv *pEnv = JniHelper::getEnv(); + if (!pEnv) { + return false; + } + + jclass classID = pEnv->FindClass(className); + if (! classID) { + LOGD("Failed to find class %s", className); + return false; + } + + jmethodID methodID = pEnv->GetMethodID(classID, methodName, paramCode); + if (! methodID) { + LOGD("Failed to find method id of %s", methodName); + return false; + } + + methodinfo.classID = classID; + methodinfo.env = pEnv; + methodinfo.methodID = methodID; + + return true; + } + bool JniHelper::getMethodInfo(JniMethodInfo &methodinfo, const char *className, const char *methodName, diff --git a/cocos2dx/platform/android/jni/JniHelper.h b/cocos2dx/platform/android/jni/JniHelper.h index 3a4cd74250..01405d9559 100644 --- a/cocos2dx/platform/android/jni/JniHelper.h +++ b/cocos2dx/platform/android/jni/JniHelper.h @@ -62,6 +62,11 @@ public: private: static bool cacheEnv(JavaVM* jvm); + static bool getMethodInfo_DefaultClassLoader(JniMethodInfo &methodinfo, + const char *className, + const char *methodName, + const char *paramCode); + static JavaVM* _psJavaVM; static JNIEnv* env; };