使用Qt lib与本机JNI Android应用程序时出现QNetwork错误

时间:2016-12-21 12:07:34

标签: java android c++ qt java-native-interface

我有一个简单的Qt lib,可以像这样发出http请求:

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

当我在Android设备(armeabi-v7ax86)上从Qt Creator中删除时,一切正常。但是我希望在NDKJNI的Android原生应用程序中使用它。在这种情况下,它仅适用于armeabi-v7a设备,适用于6.0版本的Android。如果我在x86设备上使用此lib运行android应用程序,则会崩溃并显示下一条消息:

  

致命信号11(SIGSEGV)在0x00000000(代码= 1),线程11830   (itekllc.testjni)

它出现在我的Qt lib中的这行代码中:

QNetworkReply * reply = networkAccessManager.get(request);

这是本机android项目中的communication.cpp文件:

#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

这是我的.java文件,用于调用.cpp函数:

public class CppWrapper {
    public static native void testNetwork();

    static {
        System.loadLibrary("communication");
    }
}

这是我的build.gradle预备格式文件:

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'
}

如果有人告诉我做错了什么以及为什么这行代码中的错误会很好:

QNetworkReply * reply = networkAccessManager.get(request);

仅发生在x86设备和Android版本低于6.0的设备上,并且仅来自原生Android项目(来自QtCreator,所有设备均无误)。谢谢你的帮助。

0 个答案:

没有答案