Qt 5.11 | Adding OpenSSL Support for Android

Qt 5.11.3 Reference Documentation

# 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.

The following instructions guide you to build and add the OpenSSL libraries to the APK:

1. Download the setup script to configure the build environment.

   Note: Ensure that you save the script with Unix line-endings if your build host is Unix or Linux.

2. Make the following changes in the setup script:
   - Set appropriate values to the `_ANDROID_EABI` and `_ANDROID_ARCH` variables. For example, to build for 64-bit ARM architecture using toolchain version v4.9, set `aarch64-linux-android-4.9` and `arch-arm64` values to `_ANDROID_EABI` and `_ANDROID_ARCH` respectively.
   - Add the `windows-x86` or `windows-x86_64` to the `hosts` list on line `107`, if your build host is Windows.
   - Add the following after line `128` to build for 64-bit ARM architecture:

             arch-arm64)
           ANDROID_TOOLS="aarch64-linux-android-gcc aarch64-linux-android-ranlib aarch64-linux-android-ld"
               ;;

   - Add the following after line `213` if `_ANDROID_ARCH` is set to `arch-arm64`:

           if [ "$_ANDROID_ARCH" == "arch-arm64" ]; then
                   export MACHINE=armv8
                   export RELEASE=2.6.37
                   export SYSTEM=android64
                   export ARCH=arm
                   export CROSS_COMPILE="aarch64-linux-android-"
           fi

   - Comment out the `FIPS` section if it is not used or define the `FIPS_SIG` environment variable with the FIPS signature location.
3. Define `ANDROID_NDK_ROOT` environment variable with the NDK location.
4. Provide executable rights for the setup script and run it:

         chmod a+x Setenv-android.sh
         . ./Setenv-android.sh

   The script prints the following configuration details to the prompt when it completes:

         ANDROID_NDK_ROOT: /home/user1/android-ndk-r10d
         ANDROID_ARCH: arch-arm64
         ANDROID_EABI: aarch64-linux-android-4.9
         ANDROID_API: android-21
         ANDROID_SYSROOT: /home/user1/android-ndk-r10d/platforms/android-21/arch-arm64
         ANDROID_TOOLCHAIN: /home/user1/android-ndk-r10d/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/bin
         FIPS_SIG:
         CROSS_COMPILE: aarch64-linux-androideabi-
         ANDROID_DEV: /home/user1/android-ndk-r10d/platforms/android-21/arch-arm64/usr

5. Download the latest OpenSSL sources from http://www.openssl.org/source.
6. Extract the sources to a folder and navigate to that folder using the CLI.

   Note: If your development platform is Windows, you need `msys` with `perl` v5.14 or later to build OpenSSL.

7. Configure the OpenSSL sources to build for Android using the following command:

         ./Configure shared android

   Note: You must consider enabling/disabling the SSL features based on the legal restrictions in the region where your application is available. See the SSL configure options for details about the configurable features.

8. Run `make CALC_VERSIONS="SHLIB_COMPAT=; SHLIB_SOVER=" build_libs` to build the `libcrypto` and `libssl` shared libraries that are not versioned.

   Note: Android does not load versioned libraries.

9. Open your Qt project using Qt Creator and update the "Build Android APK" settings to add libcrypto and libssl as additional libraries for your project.
10. Run your application to see it running on the device.

Qt Creator builds your application and creates an application package (APK) with the OpenSSL libraries bundled in it. Once the APK is ready, it uses `adb` to deploy the APK on the target you chose and launch the application.