且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Qt的C ++库在Android的Eclipse项目:QSQLITE驱动程序未加载

更新时间:2022-03-24 22:23:26

最后,我走进了Qt的来源看SQLite的插件怎么装(用于桌面版本至少)。

Eventually I stepped into the Qt sources to see how the SQLITE plugin is loaded (for the desktop build at least).

相关功能是 QFactoryLoader ::更新()。在这里面我注意到,它遍历所有的目录中 QCoreApplication :: libraryPaths()

The relevant function was QFactoryLoader::update(). In it I noticed that it iterates all the directories in QCoreApplication::libraryPaths():

QStringList paths = QCoreApplication::libraryPaths();
for (int i = 0; i < paths.count(); ++i) {

如果任何人有一个名为sqldrivers的子目录,它走了进去,并尝试加载所有的动态库中的子目录。

If any of them has a sub-directory named "sqldrivers", it goes inside it and tries to load all the dynamic libraries in that sub-directory.

然后我打印出来的测试项目中,我直接从Qt Creator的运行库的路径 - qDebug()&LT;&LT; a.libraryPaths(); ,我看到了这条道路 - /data/data/org.qtproject.example.untitled/qt-reserved-files/plugins 。在我的Andr​​oid手机在这个目录有一个叫 sqldrivers 子目录,包含一个文件 - libqsqlite.so

I then printed out the library paths in a test project I ran directly from Qt Creator - qDebug() << a.libraryPaths();, and I saw this path - /data/data/org.qtproject.example.untitled/qt-reserved-files/plugins. In this directory on my android phone there was a subdirectory named sqldrivers, that contained a single file - libqsqlite.so.

然后我检查了.java文件,而事实上 QtActivity ::的startApp()将库路径:

I then checked the .java files, and indeed QtActivity::startApp() adds the library path:

boolean bundlingQtLibs = false;
if (m_activityInfo.metaData.containsKey("android.app.bundle_local_qt_libs")
    && m_activityInfo.metaData.getInt("android.app.bundle_local_qt_libs") == 1) {
    localPrefix = getApplicationInfo().dataDir + "/";
    pluginsPrefix = localPrefix + "qt-reserved-files/";
    cleanOldCacheIfNecessary(localPrefix, pluginsPrefix);
    extractBundledPluginsAndImports(pluginsPrefix);
    bundlingQtLibs = true;
}

解决方案则是确保有一个 sqldrivers / libqsqlite.so 在手机上的某个地方,然后添加的父文件夹sqldrivers 使用库路径 QCoreApplication :: addLibraryPath()

The solution then would be to ensure that there is a sqldrivers/libqsqlite.so somewhere on the phone, and then add the parent folder of sqldrivers to the library path using QCoreApplication::addLibraryPath().