5

I am getting the following error:

JNI DETECTED ERROR IN APPLICATION: JNI GetMethodID called with pending exception java.lang.ClassNotFoundException: Didn't find class "package.name.class" on path: DexPathList[[zip file "/system/framework/sample.jar"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]

JNI DETECTED ERROR IN APPLICATION: JNI GetMethodID called with pending exception java.lang.ClassNotFoundException: Didn't find class "package.name.class" on path: DexPathList[[zip file "/system/framework/XposedBridge.jar"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
26771-26918/package.name.class A/art: art/runtime/java_vm_ext.cc:410]   at java.lang.Class dalvik.system.BaseDexClassLoader.findClass(java.lang.String) (BaseDexClassLoader.java:56)
26771-26918/package.name.class A/art: art/runtime/java_vm_ext.cc:410]   at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String, boolean) (ClassLoader.java:511)
26771-26918/package.name.class A/art: art/runtime/java_vm_ext.cc:410]   at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String) (ClassLoader.java:469)

Here is more about the error:

`jmethodID MethodId;
    JNIEnv * env;
    int nStatus = gVM->GetEnv((void**)&env, JNI_VERSION_1_6);
    int nResult = attachStatus(nStatus, &env);
    LOG_HM("::attach status = %d::\n", nResult);
    if (nResult == ATTACH_FAILED) {
        LOG_HM("Attach failed");
        return ;
    }

jclass classClass = env->GetObjectClass(gTotalUsageClass);
    auto classLoaderClass = env->FindClass("java/lang/ClassLoader");
    auto getClassLoaderMethod = env->GetMethodID(classClass, "getClassLoader",
                                             "()Ljava/lang/ClassLoader;");
    gClassLoader = env->CallObjectMethod(gTotalUsageClass, getClassLoaderMethod);
    gFindClassMethod = env->GetMethodID(classLoaderClass, "findClass",
                                    "(Ljava/lang/String;)Ljava/lang/Class;");

    jclass totalUsage = static_cast<jclass>(env->CallObjectMethod(gClassLoader, gFindClassMethod, env->NewStringUTF("com/a/a/TotalUsageInfo")));

    TotalUsageInfo *info = NULL;
    jobject jUsageInfo = NULL;

        jlong noOfProc = 0;
        jlong memTotal = 0;
        jlong memFree = 0;
        jlong cache = 0;
        jlong buffer = 0;
        jlong cpuUtil = 0;
        //jclass totalUsage = env->FindClass("com/a/a/TotalUsageInfo");
        if (totalUsage == NULL) {
            LOG_HM("Class TotalUsageInfo not Found \n");
            nResult = FAIL;
            detachThread(nResult);
            return;
        }
        else {
             LOG_HM("Class TotalUsageInfo Found \n");
        }

        jmethodID constructor = env->GetMethodID(totalUsage, "<init>", "(JJJJJJ)V");
        if (NULL == constructor) {
            LOG_HM("JNIGetTotalUsageInfo::TotalUsageInfo constructor not found\n");
            nResult = FAIL;
            detachThread(nResult);
            return ;
        }`
IgorGanapolsky
  • 26,189
  • 23
  • 116
  • 147
andy
  • 65
  • 1
  • 8
  • This is the crash log. I am trying to send a callback from a native C++ thread to the Java module. I have attached to the Main thread by calling attachCurrenThread but still getting this crash when i call GetMethodID function from the native thread. The jclass passed to GetMEthodID is not null. – andy Aug 09 '16 at 10:11
  • 1
    it would really help if you can provide the exact line which breaks, some things here look fishy. I assume your issue is with the methods exact signature. but not 100% sure. It would help if you provide a more accurate break point.. – Tancho Jun 08 '17 at 13:36
  • Your native library is not yet build it seems from log. Look this link to build native library - https://codelabs.developers.google.com/codelabs/android-studio-jni/index.html?index=..%2F..%2Findex#5 – Keyur Thumar Jun 08 '17 at 13:37
  • @KeyurThumar I have a similar issue, and my native library is built. The real cause is C++ thread not being attached to Java main thread appropriately... – IgorGanapolsky Jun 08 '17 at 14:31
  • You have to mentioned package name of you activity in JNI. See this link https://github.com/swankjesse/jni-example/blob/master/jniexample/src/main/jni/hello-jni.c. In this link see the method how it is written. Method with activity name(includes package name) – Keyur Thumar Jun 09 '17 at 03:47

1 Answers1

2

In order to call GetMethodID, you need to properly attach your C++ thread to the main Java thread. Like this:

bool attachThreadToJVM(JNIEnv **env)
{

    bool ret = false;

    if(globalJavaVm != NULL) {
        JavaVMAttachArgs vmAttachArgs;
        vmAttachArgs.version = JNI_VERSION_1_6;
        vmAttachArgs.name = NULL;
        vmAttachArgs.group = NULL;
        jint attachRet = globalJavaVm->AttachCurrentThread(env, &vmAttachArgs);

        if(attachRet == 0)
        {
            ret = true;
        }
    }

    return ret;
}
IgorGanapolsky
  • 26,189
  • 23
  • 116
  • 147