Adding OpenSSL Support for Android
The Qt installation package comes with OpenSSL support but the OpenSSL libraries are not part of the package due to legal restrictions in some countries. If your application depends on OpenSSL, consider packaging the SSL libraries with your Application Package (APK) as the target device may or may not have them.
You can use the QSslSocket::supportsSsl() static function to check for SSL support on the target device. First include the header:
Then use the following line to check if SSL is supported:
Check Qt Creator's
Application Output section or the Android
logcat for the result.
A convenient Github repository with prebuilt and a build script can be used without the need for manual step-by-step build. For more information, see OpenSSL for Android. If you download the repository, you can then skip to Using OpenSSL Libraries with Qt for Android.
The following instructions guide you to build the OpenSSL libraries manually:
- Download OpenSSL sources.
- Extract the sources to a folder and navigate to that folder using the CLI.
Note: If your development platform is Windows, you need
perlv5.14 or later to build OpenSSL.
- Add the Android LLVM toolchain to your path:
- Configure the OpenSSL sources to build for Android using the following command:
./Configure shared android-<arch> -D__ANDROID_API__=XX
- <arch> can take a value of:
XXis a two-digit number equal to the minimum API level for this Qt version: see Qt for Android support.
Note: You must consider enabling or disabling the SSL features based on the legal restrictions in the region where your application is available. For more information about the configurable features, see OpenSSL Configure Options.
- <arch> can take a value of:
- To build
libsslshared libraries that are not versioned, but with an _3 suffix, run:
make -j$(nproc) SHLIB_VERSION_NUMBER= SHLIB_EXT=_3.so build_libs
If you want to use a different suffix, you must change
SHLIB_EXTin the previous command, and set the
ANDROID_OPENSSL_SUFFIXenvironment variable before you access the Qt Network API.
Note: Without a suffix, Android loads the system libraries
libssl.so. These may be different versions from your libraries or from what Qt expects.
make -j$(nproc) SHLIB_VERSION_NUMBER= SHLIB_EXT=<custom_suffix>.so build_libs
Then set the environment variable in your main.cpp file:
Note: Android does not load versioned libraries.
Depending on the method you obtained the OpenSSL libraries, you can use one of the following step to include those libraries in your project:
- Using the project files:
Using the convenience OpenSSL for Android repository, you can directly add the include projects into your own project, by adding the following to your
Or if using CMake, add the following to your
if (ANDROID) include(<path/to/android_openssl/CMakeLists.txt) endif()
Alternatively, you can use the following project variable to add extra libraries, such as
libssl. For QMake use:
ANDROID_EXTRA_LIBS += \ <path_to_libs_dir>/libcrypto_3.so \ <path_to_libs_dir>/libssl_3.so
set_property(TARGET <target name> PROPERTY QT_ANDROID_EXTRA_LIBS <path_to_libs_dir>/libcrypto_3.so <path_to_libs_dir>/libssl_3.so)
Note: When targeting multiple architectures, include OpenSSL libraries for all the targeted architectures.
- Using Qt Creator, it is possible to add extra libraries. For more information, see Qt Creator: Adding Libraries to Projects.
© 2023 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.