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

Using QtLibrary on Android application.



  • I'm using native Qt Lib that is doing several network calls and send/recv signals into Android Studio app. I'm not being able to work with it because I'm not receiving any signal in slot. I can imagine that this is sth related with the EventLoop so I have tried to start a QCoreApplication into JNI_OnLoad method but I'm getting a crash. This is the code:

    // C++ code
    #include <jni.h>
    #include <android/log.h>
    #include <QDebug>
    #include "mytimer.h"
    #include <QCoreApplication>
    #include <QtConcurrentRun>
    #include <thread>
    
    
    class S {
    
    public:
        static S& getInstance()
        {
            static S instance;
            return instance;
        }
    
    private:
        S() {}                    // Constructor? (the {} brackets) are needed here.
    
        jobject * m_ref;
        JNIEnv * m_jniEnv;
    
    public:
        S(S const&)               = delete;
        void operator=(S const&)  = delete;
        void setRef(jobject * obj) {
            m_ref = obj;
        }
        void setEnv(JNIEnv * env) {
            m_jniEnv = env;
        }
        void met() {}
    
        void callJava(JNIEnv *env, jobject obj) {
    
            __android_log_print(ANDROID_LOG_VERBOSE, "S", "Object instance");
    
    
            // Construct a String
            jstring jstr = env->NewStringUTF("This string comes from JNI");
            // First get the class that contains the method you need to call
            jclass clazz = env->FindClass("com/example/mylibrary3/MyClass");
            // Get the method that you want to call
            jmethodID messageMe = env->GetMethodID(clazz, "messageMe", "(Ljava/lang/String;)Ljava/lang/String;");
            // Call the method on the object
            jobject result = env->CallObjectMethod(obj, messageMe, jstr);
            // Get a C-style string
            const char* str = env->GetStringUTFChars((jstring) result, NULL);
    
            // Clean up
            env->ReleaseStringUTFChars(jstr, str);
        }
    
    };
    
    void runQCoreApplication()
    {
       // Instantiate QCoreApplication.
       int i = 1;
       char* c[1] = {"MyLib"};
       QCoreApplication a(i, c);
    
    //   // Start the QTimer.
       __android_log_print(ANDROID_LOG_INFO, "LibTag", "Starting QTimer!");
       MyTimer timer;
    
       // Start the event loop.
       a.exec();
    }
    
    // define our native method
    static void printHello(JNIEnv *env, jobject obj)
    {
        __android_log_print(ANDROID_LOG_VERBOSE, "MyApp", "Hello world"); 
    
        S::getInstance().callJava(env, obj);
    //    MyTimer timer;
    }
    
    static void callCallback(JNIEnv */*env*/, jobject /*obj*/, jobject paradisoObject)
    {
        __android_log_print(ANDROID_LOG_VERBOSE, "MyApp", "Hello world");
    }
    
    
    static JNINativeMethod methods[] = {
        { "printHello", "()V", (void *)printHello }
    };
    
    JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* /*reserved*/)
    {
        JNIEnv* env;
        if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6)
               != JNI_OK) {
            return JNI_ERR;
        }
    
        jclass javaClass = env->FindClass("com/example/mylibrary3/MyClass");
        if (!javaClass)
            return JNI_ERR;
    
        if (env->RegisterNatives(javaClass, methods,
                                sizeof(methods) / sizeof(methods[0])) < 0) {
            return JNI_ERR;
        }
    
        QtConcurrent::run(runQCoreApplication);
    
    
        return JNI_VERSION_1_6;
    }
    

    This is the Java part:

    package com.example.mylibrary3;
    
    interface ICallback {
        void Result(String url);
    }
    
    class MyCallback implements ICallback {
    
        @Override
        public void Result(String url) {
            System.out.println("Callback" + url);
        }
    }
    
    public class MyClass {
    
        private ICallback callback;
    
        static {
            System.out.println("HOLA 2");
            System.loadLibrary("QtLibExample");
        }
    
        public MyClass() {
            this.callback = new MyCallback();
        }
    
        public native void printHello();
    
        public String messageMe(String text) {
            System.out.println("This is the text" + text);
    
            callback.Result(text);
            return text;
        }
    }
    

    And I'm getting this when app starts:

    V/S: Object instance
    I/System.out: This is the textThis string comes from JNI
        CallbackThis string comes from JNI
    W/: WARNING: QApplication was not created in the main() thread.
    A/libc: Fatal signal 11 (SIGSEGV), code 1, fault addr 0x0 in tid 26361 (Thread (pooled))
    

    Any idea on how to solve it?


Log in to reply