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

Import C++/Qt libary into Java Android



  • I created a shared library in C++ with Qt Creator and build for Android. This library works when I import it into an Android application created in Qt Creator.

    I defined MYLIBSHARED_EXPORT in mylib_global.h and include mylib_global.h in mylib.h

    MYLIBSHARED_EXPORT const char* info();    // in mylib.h
    
    const char* info()                        // in mylib.cpp
    {
        qDebug() << "You call info";
        const char* name = "MyLib";
        return name;
    }
    

    I can call the info() from an Android application created in Qt Creator. Here is the part I add in the .pro to import the .so file:

    INCLUDEPATH += home/myname/Projects/Qt/MyLib
    LIBS += -L/home/myname/Projects/Qt/build-MyLib/android-build/libs/armeabi-v7a -lMyLib_armeabi-v7a
    ANDROID_EXTRA_LIBS += /home/myname/Projects/Qt/build-MyLib/android-build/libs/armeabi-v7a/libMyLib_armeabi-v7a.so
    

    Next, I added a function with jni interface:

    JNIEXPORT void JNICALL                    // in mylib.h
    Java_com_mylib_sendIntToClib(JNIEnv *env, jobject thisObj, jint n);
    
    JNIEXPORT void JNICALL                    // in mylib.cpp
    Java_com_mylib_sendIntToClib(JNIEnv *env, jobject thisObj, jint n) {
        __android_log_print(ANDROID_LOG_INFO, "mylib", "%d received by clib", n);
    }
    

    My first question is how can I call this function from an Android application created in Qt Creator?

    For the very convenient, my second question is if I could import this library into an Android application created in Android Studio? (I made some tries but it seems that the QtCore library does not work on Android.)

    Can you help me ?


Log in to reply