Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Unable to call the c++ method from android native



  • Hi,
    I want to call c/c++ function from android java code, when i register the c methods using QAndroidJniEnvironment
    I am not able to call the java method from c++. if dont register, then i can able to call java methods from c++. But communication should happened both the ways. below it the sample code i have used for registering native methods.
    @
    void registerNativeMethods() {
    JNINativeMethod methods[] = {{"callNativeOne", "(I)V", reinterpret_cast<void *>(fromJavaOne)},
    {"callNativeTwo", "(I)V", reinterpret_cast<void *>(fromJavaTwo)}};

    QAndroidJniObject javaClass("org/qtproject/example/notification/NotificationClient");
    qDebug()<<"java class is valid? <<"<<javaClass.isValid();
    QAndroidJniEnvironment env;
    jclass objectClass = env->GetObjectClass(javaClass.object<jobject>());
    

    qDebug()<<"registernative returned ::"<< env->RegisterNatives(objectClass,
    methods,2
    /sizeof(methods) / sizeof(methods[0]/);
    env->DeleteLocalRef(objectClass);
    }
    @

    Please provide the solution for this problem

    Thanks and Regards
    Kranthi Talluri

    [edit: Added missing coding tags @ SGaist]



  • Hello!

    I had the same issue. I've spent my whole day trying to figure out what can the problem be. Now that I figured it out, I felt it important to register to this forum and share the solution with you.

    So... At the end of your registerNativeMethods() method add the following code:
    @ if (env->ExceptionCheck()) {
    // Handle exception here.
    env->ExceptionClear();
    }@

    You should also check "this part":http://qt-project.org/doc/qt-5/qandroidjniobject.html#handling-java-exception of the documentation.

    Let me know if it helped for you as well.


Log in to reply