IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)

Adding OpenSSL Support for Android

How to package OpenSSL libraries with a Qt for Android application.

Article lu   fois.

L'auteur

Liens sociaux

Viadeo Twitter Facebook Share on Google+   

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:

 
Sélectionnez
#include <QSslSocket>

Then use the following line to check if SSL is supported:

 
Sélectionnez
qDebug() << "Device supports OpenSSL: " << QSslSocket::supportsSsl();

Check Qt Creator's Application Output section or the Android logcat for the result.

Building OpenSSL for Android

A convenient Github repository with a binary and a build script can be used without the need for a 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:

  1. Download OpenSSL sources.

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

  3. Add the Android LLVM toolchain to your path:

     
    Sélectionnez
    export PATH="<android_ndk_path>/toolchains/llvm/prebuilt/<host>/bin":$PATH
  4. Configure the OpenSSL sources to build for Android using the following command:

     
    Sélectionnez
    ./Configure shared android-<arch> -D__ANDROID_API__=XX

    Where:

    • <arch> can take a value of: arm, arm64, x86, x86_64.

    • XX is a two-digit number equal to the minimum API level for this Qt version: see Qt for Android support.

    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.

  5. Without a suffix, Android loads the system libraries 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:

     
    Sélectionnez
    make -j$(nproc) SHLIB_VERSION_NUMBER= build_libs
    
    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:

     
    Sélectionnez
    qputenv("ANDROID_OPENSSL_SUFFIX", "&lt;custom_suffix&gt;");

    Android does not load versioned libraries.

Using OpenSSL Libraries with Qt for Android

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 .pro file:

     
    Sélectionnez
    android: include(&lt;path/to/android_openssl/openssl.pri)

    If using CMake, add the following to your CMakeLists.txt:

     
    Sélectionnez
    if (ANDROID)
        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:

     
    Sélectionnez
    include(&lt;path/to/android_openssl&gt;/android_openssl.cmake)

    Then add:

     
    Sélectionnez
    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()

    Alternatively, you can use the following project variable to add extra libraries, such as libcrypto and libssl.

    For QMake use:

     
    Sélectionnez
    ANDROID_EXTRA_LIBS += \
        &lt;path_to_libs_dir&gt;/libcrypto_3.so \
        &lt;path_to_libs_dir&gt;/libssl_3.so

    For CMake:

     
    Sélectionnez
    set_property(TARGET &lt;target name&gt; PROPERTY QT_ANDROID_EXTRA_LIBS
        &lt;path_to_libs_dir&gt;/libcrypto_3.so
        &lt;path_to_libs_dir&gt;/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.

Vous avez aimé ce tutoriel ? Alors partagez-le en cliquant sur les boutons suivants : Viadeo Twitter Facebook Share on Google+