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

QNetwork error when use Qt lib with native JNI Android application



  • I have the simple Qt lib that makes http request like this:

    QNetworkAccessManager networkAccessManager;
    QNetworkRequest request{QUrl("http://www.google.ru")};
    QNetworkReply * reply = networkAccessManager.get(request);
    ...
    

    All works fine when i run it on Android devices (armeabi-v7a and x86) diractly from Qt Creator. But i want to use it inside android native application with NDK and JNI. In this case it works only on armeabi-v7a devices and with Android from version 6.0. If i run android application with this lib on x86 device it crash with next message:

    Fatal signal 11 (SIGSEGV) at 0x00000000 (code=1), thread 11830 (itekllc.testjni)
    

    And it occurs on this line of code inside my Qt lib:

    QNetworkReply * reply = networkAccessManager.get(request);
    

    This is my communication.cpp file in native android project:

    #include <jni.h>
    #include "QCoreApplication"
    #include "testandroidthreadslib.h"
    
    #ifndef _Included_com_navitekllc_testjni_CppWrapper
    #define _Included_com_navitekllc_testjni_CppWrapper
    
    #ifdef __cplusplus
    extern "C" {
    #endif
    
    JNIEXPORT void JNICALL
    Java_com_navitekllc_testjni_CppWrapper_testNetwork(JNIEnv *env, jclass type) {
        TestAndroidThreadsLib testLib;
        testLib.run();
    }
    
    #ifdef __cplusplus
    }
    #endif
    #endif
    

    This is my .java file to call .cpp function:

    public class CppWrapper {
        public static native void testNetwork();
    
        static {
            System.loadLibrary("communication");
        }
    }
    

    This is my build.gradle experemental gradle file:

    apply plugin: 'com.android.model.application'
    
    model {
    
        repositories {
            libs(PrebuiltLibraries) {
                QtStaticLibrary {
                    headers.srcDir file("..PathToLib/TestAndroidThreadsLib").absolutePath
                    binaries.withType(StaticLibraryBinary) {
                        def myStaticLibPath = "src/main/jniLibs/${targetPlatform.getName()}/libTestAndroidThreadsLib.a"
                        staticLibraryFile = file("${myStaticLibPath}")
                    }
                }
                QCoreSharedLibrary {
                    binaries.withType(SharedLibraryBinary) {
                        def coreLibPath = "src/main/jniLibs/${targetPlatform.getName()}/libQt5Core.so"
                        sharedLibraryFile = file("${coreLibPath}")
                    }
                }
                QNetSharedLibrary {
                    binaries.withType(SharedLibraryBinary) {
                        def networkLibPath = "src/main/jniLibs/${targetPlatform.getName()}/libQt5Network.so"
                        sharedLibraryFile = file("${networkLibPath}")
                    }
                }
            }
        }
    
        android {
            compileSdkVersion 23
            buildToolsVersion "23.0.3"
    
            defaultConfig {
                minSdkVersion.apiLevel 18
                targetSdkVersion.apiLevel 21
                versionCode 1
                versionName "1.0"
            }
            buildTypes {
                release {
                    minifyEnabled false
                    proguardFiles.add(file('proguard-rules.pro'))
                }
            }
    
            sources {
                main {
                    jni {
                        dependencies {
                            library "QtStaticLibrary" linkage "static"
                            library "QCoreSharedLibrary" linkage "shared"
                            library "QNetSharedLibrary" linkage "shared"
                        }
                    }
                }
            }
    
            ndk {
                moduleName = "communication"
                cppFlags.add("-std=c++11")
                stl "gnustl_shared"
    
                cppFlags.add('-I' + file("/Volumes/Transcend/Qt/5.7/android_armv7/include").absolutePath)
                cppFlags.add('-I' + file("/Volumes/Transcend/Qt/5.7/android_armv7/include/QtCore").absolutePath)
    
                abiFilters.addAll([
                        "x86",
                        "armeabi-v7a"
                ])
            }
        }
    }
    
    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        testCompile 'junit:junit:4.12'
    
        compile 'com.android.support:appcompat-v7:24.2.1'
    }
    

    It would be great if somebody tell what I'm doing wrong and why the error in this line of code:

    QNetworkReply * reply = networkAccessManager.get(request);
    

    only occurs on x86 devices and on devices with Android version less then 6.0 and only from native Android project (from QtCreator all works without errors). Thanks for help.