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

Why creating QAndroidJniObject from jobject fails without error?



  • Hello,
    I have simple example where I try to call Java function from C++ library in android application. First I am building: libMyLib.so, then I am creating AAR library which includes the libMyLib.so and then I am testing the AAR in new test android app project.

    Inside my Qt library project for libMyLib.so, I have jniinterface.cpp and this is the function:

    JNIEXPORT void JNICALL Java_com_example_MyLib_LibraryClass_personTest(JNIEnv*, jobject, jobject person)
    {
        qDebug() << "C++ lib _________ Creating now QAndroidJniObject";
        QAndroidJniObject p(person);
        p.callMethod<void>("printPersonInfo", "(I)V", 29);
    }
    

    After libMyLib.so is built successfully, I am copying it in my AAR android project in this location My_AAR_project\app\src\main\jniLibs\x86\libMyLib.so

    In this My_AAR_project I have two simple classes: LibraryClass.java (used to call native function)

    public class LibraryClass {
    
        public native void personTest(Person person);
    
        public void testThePerson() {
            Person p = new Person();
            personTest(p);
        }
    
        static {
            System.loadLibrary("MyLib");
        }
    }
    

    And the other class is simple Person.java class:

    public class Person
    {
        public Person() {}
    
        public static void printPersonInfo(int n)
        {
            System.out.println("Called printPersonInfo " + " Name: John" + " age: " + n);
        }
    }
    

    And now after I built successfully AAR lib I am including it in my test android app and I am calling:

    LibraryClass test = new LibraryClass();
    test.testThePerson();
    

    And the app crash and the last output is before creation of QAndroidJniObject:

    D/: C++ lib _________ Creating now QAndroidJniObject
    
    A/libc: Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x0 in tid 19897 (ampleandroidapp), pid 19897 (ampleandroidapp)
    

    Any help or hint what may be the problem? Or I am doing something wrong. Thanks in advance.

    To simplify what I am trying to do:

    1. Create Person object on java side
    2. Pass the Person object to C++ lirary
    3. Call printPersonInfo function from C++ on the Person object that is received from java

    UPDATE:

    I got error also when I try to create QAndroidJniObject from jclass:

    jclass cls = env->FindClass("com/example/MyLib/Person");
    QAndroidJniObject p(cls);
    

    Simple string also fails:

    QAndroidJniObject string = QAndroidJniObject::fromString("TESTSTRING");
    

    When I try to use anything related to QAndroidJni it crashes.



  • I don't understand this well:

    From looking at the documentation, it seems that the QAndroidJniObject constructor and methods have to be attaching and detaching the current thread to and from the JVM, which isn't going to work in a JNI thread that's already attached to the JVM.

    Does this mean that, if I call C++ function(that uses QAndroidJniObject) from java JNI class, it is not going to work?
    Because as I understand, QAndroidJniObject is attaching and detaching the current thread to and from the JVM, and the JNI java object that is calling native functions, is already attached to the JVM.
    Please correct me if I am wrong.