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
or
AAB
) as the target device may or may not have them.
可以使用 QSslSocket::supportsSsl () static function to check for SSL support on the target device. First, include the header:
#include <QSslSocket>
Then use the following line to check if SSL is supported:
qDebug() << "Device supports OpenSSL: " << QSslSocket::supportsSsl();
Check Qt Creator's
Application Output
section or the Android
logcat
for that log message.
Using the convenience OpenSSL for Android repository, you can directly include OpenSSL libraries in your own project.
With CMake, add the following to your
CMakeLists.txt
:
if (ANDROID) include(FetchContent) FetchContent_Declare( android_openssl DOWNLOAD_EXTRACT_TIMESTAMP true URL https://github.com/KDAB/android_openssl/archive/refs/heads/master.zip ) FetchContent_MakeAvailable(android_openssl) include(${android_openssl_SOURCE_DIR}/android_openssl.cmake) endif()
Or if you cloned the repository into a subdirectory:
include(<path/to/android_openssl>/android_openssl.cmake)
Then, add the libraries to your targets:
qt_add_executable(your_target_name ..) qt_add_executable(your_second_target_name ..) if (ANDROID) add_android_openssl_libraries(your_target_name your_second_target_name) endif()
For
qmake
, add the following to your
.pro
文件:
android: include(<path/to/android_openssl/openssl.pri)
Alternatively, to add extra libraries, such as
libcrypto
and
libssl
. For CMake, use:
set_target_properties(<target_name> PROPERTIES QT_ANDROID_EXTRA_LIBS "<path_to_libs_dir>/libcrypto_3.so" "<path_to_libs_dir>/libssl_3.so" )
Or for
qmake
use:
ANDROID_EXTRA_LIBS += \ <path_to_libs_dir>/libcrypto_3.so \ <path_to_libs_dir>/libssl_3.so
注意: 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 .
The following instructions guide you to build the OpenSSL libraries manually:
Extract the sources to a folder and navigate to that folder using the CLI.
注意:
If your development platform is Windows, you need
msys
with
perl
5.14 or later to build OpenSSL.
export PATH=~/Android/Sdk/ndk/26.1.10909125/toolchains/llvm/prebuilt/<host>/bin:$PATH
The Android SDK is commonly installed by Qt Creator or Android Studio in the following locations:
arm
,
arm64
,
x86
or
x86_64
:
./Configure shared android-<arch> -D__ANDROID_API__=26
注意: 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 .
libcrypto.so
and
libssl.so
. These may be different versions from your libraries or from what Qt expects. To ensure that Qt apps can load the manually built OpenSSL libraries, run the following commands:
make -j$(nproc) SHLIB_VERSION_NUMBER= build_libs mkdir -p ${out_path} cp libcrypto.so ${out_path}/libcrypto_3.so cp libssl.so ${out_path}/libssl_3.so cd ${out_path} patchelf --set-soname libcrypto_3.so libcrypto_3.so patchelf --set-soname libssl_3.so libssl_3.so patchelf --replace-needed libcrypto.so libcrypto_3.so libssl_3.so
注意:
Though the
libcrypto
and
libssl
shared libraries that are not versioned, they will have a
_3
suffix.
Then set the environment variable in your main.cpp file:
qputenv("ANDROID_OPENSSL_SUFFIX", "<custom_suffix>");
注意: Android does not load versioned libraries.