Build OpenSSL for Qt Android

11/02/2019 - 13:33
Average: 5 (2 votes)

OpenSSL is not bundled on Android anymore and when using Qt on Android you need to supply your own copies of libssl.so and libcrypto.so in case you need to use any secure networking functions, like https connections.  The instructions at Qt are unclear on NDK and OpenSSL versions and the instructions only work with older versions of the Android NDK. This article will clear up that confusions and explain how to build OpenSSL so that secure networking can be used in your Qt Android projects.

We will assume that you otherwise have an working Android SDK & NDK environment with Qt for Android installed!

Creating a custom toolchain

In NDK 18 you need to create a custom native toolchain so that all paths and things point to the correct files. Choose a suitable Android platform version and architecture (arm,arm64,x86,x86_64) according to your project needs.

Create a 32-bit ARM toolchain

This will build a 32-bit ARM toolchain for platform level 19, Android 4.4

$NDK/build/tools/make_standalone_toolchain.py --arch arm --api 19 --install-dir /opt/android-19-arm

Create a 64-bit ARM toolchain

This will build a 64-bit ARM toolchain for platform level 21, the first to support 64-bit ARM, Android 5.0

$NDK/build/tools/make_standalone_toolchain.py --arch arm64 --api 21 --install-dir /opt/android-21-arm64

Prepare OpenSSL 1.0.2q

Next, download OpenSSL source archive from the 1.0.2 series, latest at time of writing was 1.0.2q (Always use the latest version!):

wget https://www.openssl.org/source/openssl-1.0.2q.tar.gz

Check source archive checksum:

wget https://www.openssl.org/source/openssl-1.0.2q.tar.gz.sha256
sha256sum openssl-1.0.2q.tar.gz

And compare with the contents of openssl-1.0.2q.tar.gz.sha256,

when writing this, should be 5744cfcbcec2b1b48629f7354203bc1e5e9b5466998bbccc5b5fcde3b18eb684 but always check the original source!

Extract the source archive:

tar xf openssl-1.0.2q.tar.gz

And go into the source directory:

cd openssl-1.0.2q

Fix for clang toolchain

OpenSSL configure assumes that the C compile is gcc and sets the "-mandroid" flag. Android NDK is using clang now so we need to remove it first. Run:

sed -i -e 's/-mandroid//' Configure

Configure and build OpenSSL for Android

Choose one of the toolchain that you prepared above and configure and build OpenSSL with it

CROSS_COMPILE=/opt/android-19-arm/bin/arm-linux-androideabi- ./Configure android shared
make ANDROID_DEV=/opt/android-19-arm/sysroot/usr/ CALC_VERSIONS="SHLIB_COMPAT=; SHLIB_SOVER=" build_libs

For 64-bit ARM build

CROSS_COMPILE=/opt/android-21-arm64/bin/aarch64-linux-android- ./Configure android shared
make ANDROID_DEV=/opt/android-21-arm64/sysroot/usr CALC_VERSIONS="SHLIB_COMPAT=; SHLIB_SOVER=" build_libs

Verify the libraries are built for correct arch

file libssl.so libcrypto.so

Should respond with the following for 32-bit ARM

libssl.so:    ELF 32-bit LSB shared object, ARM, EABI5 version 1 (SYSV), dynamically linked, not stripped
libcrypto.so: ELF 32-bit LSB shared object, ARM, EABI5 version 1 (SYSV), dynamically linked, not stripped

And for 64-bit ARM with

libssl.so:    ELF 64-bit LSB shared object, ARM aarch64, version 1 (SYSV), dynamically linked, not stripped
libcrypto.so: ELF 64-bit LSB shared object, ARM aarch64, version 1 (SYSV), dynamically linked, not stripped

Add to libraries your Qt Android project

Copy libssl.so libcrypto.so to your project, for example in PROJECT-ROOT/3rdparty/openssl-armv7/

And add the reference to your Qt project file:

contains(ANDROID_TARGET_ARCH,armeabi-v7a) {
    ANDROID_EXTRA_LIBS = \
        $$PWD/3rdparty/openssl-armv7/libcrypto.so \
        $$PWD/3rdparty/openssl-armv7/libssl.so
}

Or use the Qt Creator UI to add the libraries to your project.

 

Changelog: 
14/01/2019 - 15:05
Keywords: 
OpenSSL, Qt, Android