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

Using Qt codes in Android application



  • I have an Android application in Android Studio and I want to use Qt C++ codes in my app. I have used pure C++ codes in my android app using cmake and NDK recently and I could interact with my pure C++ and now I want to use Qt C++ codes.

    To do this, I have installed Qt with Android capability. After that I copied Qt5.12.7/5.12.7/android_arm64_v8a/lib/*.so and includes file to my Android App /etc/ folder and I created a simple main.cpp and CMakeLists.txt files as follow:

    main.cpp

    #include <QString>
    #include <QDebug>
    int main(int argc, char *argv[])
    {
        QString test="HELLO WORLD!";
        qDebug()<<test;
        return 0;
    }
    

    CMakeLists.txt

    cmake_minimum_required(VERSION 3.4.1)
    include_directories(etc/headerFile)
    link_directories(etc/libs)
    add_library(po-lib SHARED main.cpp)
    target_link_libraries(po-lib -lQt5Core)
    

    As seen in the above files, I want to compile a simple Qt C++ code with dependency on libQtCore.so in my Android App, but after compiling the App, the following errors occur:

    Build command failed.
    Error while executing process C:\Users\User1\AppData\Local\Android\Sdk\cmake\3.10.2.4988404\bin\ninja.exe with arguments {-C D:\WorkProjects\Horizon\Ga\po\.cxx\cmake\debug\armeabi-v7a po-lib}
    ninja: Entering directory `D:\WorkProjects\Horizon\Ga\po\.cxx\cmake\debug\armeabi-v7a'
    [1/2] Building CXX object CMakeFiles/po-lib.dir/main.cpp.o
    [2/2] Linking CXX shared library D:\WorkProjects\Horizon\Ga\po\build\intermediates\cmake\debug\obj\armeabi-v7a\libpo-lib.so
    FAILED: D:/WorkProjects/Horizon/Ga/po/build/intermediates/cmake/debug/obj/armeabi-v7a/libpo-lib.so 
    cmd.exe /C "cd . && C:\Users\User1\AppData\Local\Android\Sdk\ndk\21.0.6113669\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe --target=armv7-none-linux-androideabi21 --gcc-toolchain=C:/Users/User1/AppData/Local/Android/Sdk/ndk/21.0.6113669/toolchains/llvm/prebuilt/windows-x86_64 --sysroot=C:/Users/User1/AppData/Local/Android/Sdk/ndk/21.0.6113669/toolchains/llvm/prebuilt/windows-x86_64/sysroot -fPIC -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -march=armv7-a -mthumb -Wformat -Werror=format-security   -O0 -fno-limit-debug-info  -Wl,--exclude-libs,libgcc_real.a -Wl,--exclude-libs,libatomic.a -static-libstdc++ -Wl,--build-id -Wl,--fatal-warnings -Wl,--exclude-libs,libunwind.a -Wl,--no-undefined -Qunused-arguments -shared -Wl,-soname,libpo-lib.so -o D:\WorkProjects\Horizon\Ga\po\build\intermediates\cmake\debug\obj\armeabi-v7a\libpo-lib.so CMakeFiles/po-lib.dir/main.cpp.o -LD:/WorkProjects/Horizon/Ga/po/src/main/cpp/etc/libs_a  -LD:/WorkProjects/Horizon/Ga/po/src/main/cpp/etc/libs_so -lQt5Core -latomic -lm && cd ."
    C:/Users/User1/AppData/Local/Android/Sdk/ndk/21.0.6113669/toolchains/llvm/prebuilt/windows-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin\ld: warning: skipping incompatible D:/WorkProjects/Horizon/Ga/po/src/main/cpp/etc/libs_so/libQt5Core.so while searching for Qt5Core
    C:/Users/User1/AppData/Local/Android/Sdk/ndk/21.0.6113669/toolchains/llvm/prebuilt/windows-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin\ld: error: cannot find -lQt5Core
    D:/WorkProjects/Horizon/Ga/po/src/main/cpp/main.cpp:25: error: undefined reference to 'QMessageLogger::debug() const'
    D:/WorkProjects/Horizon/Ga/po/src/main/cpp/main.cpp:25: error: undefined reference to 'QDebug::~QDebug()'
    D:/WorkProjects/Horizon/Ga/po/src/main/cpp/main.cpp:25: error: undefined reference to 'QDebug::~QDebug()'
    D:/WorkProjects/Horizon/Ga/po/src/main/cpp/etc/headerFile\QtCore/qstring.h:700: error: undefined reference to 'QString::fromAscii_helper(char const*, int)'
    D:/WorkProjects/Horizon/Ga/po/src/main/cpp/etc/headerFile\QtCore/qdebug.h:155: error: undefined reference to 'QDebug::putString(QChar const*, unsigned int)'
    D:/WorkProjects/Horizon/Ga/po/src/main/cpp/etc/headerFile\QtCore/qdebug.h:125: error: undefined reference to 'QTextStream::operator<<(char)'
    D:/WorkProjects/Horizon/Ga/po/src/main/cpp/etc/headerFile\QtCore/qarraydata.h:59: error: undefined reference to 'qt_assert(char const*, char const*, int)'
    D:/WorkProjects/Horizon/Ga/po/src/main/cpp/etc/headerFile\QtCore/qarraydata.h:239: error: undefined reference to 'QArrayData::deallocate(QArrayData*, unsigned int, unsigned int)'
    clang++: error: linker command failed with exit code 1 (use -v to see invocation)
    ninja: build stopped: subcommand failed.
    

    I have used both arm64_v8a and armv7 libs in Qt folders but the same errors appear. How can I solve this linker error (incompatible lib)?