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.
可以使用 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 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:
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
v5.14 or later to build OpenSSL.
export PATH="<android_ndk_path>/toolchains/llvm/prebuilt/<host>/bin":$PATH
./Configure shared android-<arch> -D__ANDROID_API__=21
Where <arch> can take a value of:
arm
,
arm64
,
x86
,
x86_64
.
注意: 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
and
libssl
shared libraries that are not versioned, but with an
_1_1
suffix, run:
make -j$(nproc) SHLIB_VERSION_NUMBER= SHLIB_EXT=_1_1.so build_libs
Without a suffix, Android 5 (API 21) will load the system libraries libcrypto.so and libssl.so , which are OpenSSL 1.0, rather than your libraries.
If you want to use a different suffix, you must change
SHLIB_EXT
in the previous command, and set the
ANDROID_OPENSSL_SUFFIX
environment variable before you access the Qt Network API.
make -j$(nproc) SHLIB_VERSION_NUMBER= SHLIB_EXT=<custom_suffix>.so build_libs
Then set the environment variable in your main.ccp file:
qputenv("ANDROID_OPENSSL_SUFFIX", "<custom_suffix>");
注意: 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 convenience
OpenSSL for Android
repository, you can directly add the include projects into your own project, by adding the following to your
.pro
文件:
android: include(<path/to/android_openssl/openssl.pri)
Or if using CMake, add the following to your
CMakeLists.txt
:
if (ANDROID) include(<path/to/android_openssl/CMakeLists.txt) endif()
Alternatively, you can either use the Qt for Android variable
ANDROID_EXTRA_LIBS
to add extra libraries, mainly
libcrypto
and
libssl
。对于
QMake
use:
ANDROID_EXTRA_LIBS += \ <path_to_libs_dir>/libcrypto_1_1.so \ <path_to_libs_dir>/libssl_1_1.so
For CMake:
set(ANDROID_EXTRA_LIBS <path_to_libs_dir>/ibcrypto_1_1.so <path_to_libs_dir>/libssl_1_1.so CACHE INTERNAL "")
注意: When targeting multiple architectures, include OpenSSL libraries for all the targeted architectures.