Creating a SSL socket in Qt for Android
-
Hello,
I am creating a project that communicates a PC app and an Android app through SSL.
In the PC side, I don't have any problems yet. But in the Android implementation, I have a problem and it is thatQSslSocket
is not recognized.#ifndef SSLCLIENT_H #define SSLCLIENT_H #include <QObject> #include <QtNetwork> #include <QSslSocket> class SslClient : public QObject { Q_OBJECT public: SslClient(QObject *parent = 0); ~SslClient(); QSslSocket client_socket; private slots: void tcpReady(); void sslError(QList<QSslError> errors); void TCPError(QAbstractSocket::SocketError error); void connectToServer(); }; #endif // SSLCLIENT_H
Output is:
/home/xxx/AndroidDisplay/sslclient.h:16: erreur : unknown type name 'QSslSocket'
I have followed the instructions in https://doc.qt.io/qt-5/android-openssl-support.html, downloaded the Github OpenSSL for Android repository and added these lines at the end of my CMakeLists as requested :
if (ANDROID) include(~/android_openssl/CMakeLists.txt) endif()
But this didn't solve the problem.
I am using Qt 5.15.3, Ubuntu 22.04
-
@diego-qt said in Creating a SSL socket in Qt for Android:
I have followed the instructions in https://doc.qt.io/qt-5/android-openssl-support.html, downloaded the Github OpenSSL for Android
This has nothing to do with the error you get. QSslSocket is part of Qt. Did you add
find_package(Qt6 REQUIRED COMPONENTS Network) target_link_libraries(mytarget PRIVATE Qt6::Network)
to your CMakeLists.txt file?
How did you install Qt for Android? -
@jsulm you're right, I didn't add the lines to my CMakeLists file.
But I just added them and the error persists.I installed Qt for Android from source from this page https://doc.qt.io/qt-5/android-building.html
I have already compiled a test app (from Qt examples) and installed it in my phone, but I'm having trouble in this next step with the SSL connection. -
@diego-qt said in Creating a SSL socket in Qt for Android:
I installed Qt for Android from source
Including QtSerialPort module?
"But I just added them and the error persists" - did you run cmake in an empty build folder?
-
-
@diego-qt said in Creating a SSL socket in Qt for Android:
And the so called qsslsocket.h does exist
And does it define the QSslSocket class?
-
@jsulm I see something interesting now that you mention it.
#ifndef QT_NO_SSL
When I activate the Android kit, the code under this
#ifndef
directive is grayed out (so most of the code, actually). I'm guessing I have to activate it, but putting#ifndef QT_NO_SSL #define QT_NO_SSL #endif
in my header file before calling
#include <QSslSocket>
does not do the trick. How could it be done ?By the way, I see that when I activate the Desktop kit, the definition of QSslSocket in
qsslsocket.h
is not grayed out. -
These bug reports suggest that I'm not the only one with this problem:
https://bugreports.qt.io/browse/QTBUG-88238
https://bugreports.qt.io/browse/QTBUG-65531But there haven't been answers on the issues.
-
@jsulm I found that the
qsslsocket.h
file starts with:#include <QtNetwork/qtnetworkglobal.h> #include <QtCore/qlist.h> #include <QtCore/qregexp.h> #include <QtCore/qvector.h>
And the
QtNetwork/qtnetworkglobal.h
file starts with:#include <QtCore/qglobal.h> #include <QtNetwork/qtnetwork-config.h>
And finally, in the
QtNetwork/qtnetwork-config.h
file I found (among other defines):#define QT_NO_SSL
I have the impression then that QSslSocket can't be defined when in Android kit. However, when I activate the Desktop kit, the
#define QT_NO_SSL
is not found in the last file I mentioned, that's why my project compiles with this kit.
I don't understand why this happens. I might need to rerun a configuration file but I don't know what exactly to do. -
Hello @jsulm I have looked deeper into my problem and found some things. Could you help me by confirming as far as you can? Or recommend someone that may know the answer?
I found a new documentation page I hadn't read: https://doc.qt.io/qt-5/ssl.html#enabling-and-disabling-ssl-support
When I launched the configure program with theopenssl-linked
option, I finally got aQtNetwork/qtnetwork-config.h
file without the definition that caused trouble (see above). However, when I tried to save the newqmake
version and its Android kit, I get an error saying that the Android Clang compiler "can't produce code for the Qt version "Qt 5.15.13 (Qt-5.15.13)" (x86-linux-generic-elf-64bit)."What I understand is that I didn't activate the target for cross-compilation in the initial configuration step.
So I went back and launched thisOPENSSL_LIBS='-L~/android-sdk/android_openssl/ssl_1.1/arm64-v8a/include -lssl -lcrypto' \ ~/qt5/configure -openssl-linked -prefix /opt/Qt5.15 -xplatform android-clang \ -disable-rpath -nomake tests -nomake examples -android-ndk ~/android-sdk/ndk/21.3.6528147 \ -android-sdk ~/android-sdk -no-warnings-are-errors -opensource -confirm-license
But I got the following error:
ERROR: Feature 'openssl-linked' was enabled, but the pre-condition '!features.securetransport && !features.schannel && libs.openssl' failed. Check config.log for details.
Please note that when I launch the mentioned command without
-xplatform android-clang
, it seems to work and I am asked to launchgmake
. So for the time being I have found myself in an incompatibility of installing an SSL-supported Qt and cross-compiling for Android.Regarding the config log file that the error indicates me, I don't know what exactly to look at.
What I find interesting is that at some point there is this SSL-related error (with the -xplatform option):+ cd /home/diego/qt-build/config.tests/openssl && MAKEFLAGS= /usr/bin/gmake clean && MAKEFLAGS= /usr/bin/gmake > rm -rf /home/diego/qt-build/config.tests/openssl/android-build > rm -f main.o > rm -f *~ core *.core > /home/diego/android-sdk/ndk/21.3.6528147/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ -c -target aarch64-linux-android21 -fno-limit-debug-info -fPIC -fstack-protector-strong -DANDROID -O2 -fPIC -I. -I/home/diego/qt5/qtbase/mkspecs/android-clang -o main.o main.cpp > main.cpp:2:10: fatal error: 'openssl/ssl.h' file not found > #include <openssl/ssl.h> > ^~~~~~~~~~~~~~~ > 1 error generated. > gmake: *** [Makefile:193 : main.o] Erreur 1
The weird thing is that without the -xplatform option, I don't see this error, and yet the two command lines are otherwise the same one.
Apart from that, there are many other errors, but as I said I don't know exactly what to look at.