Skip to content

Instantly share code, notes, and snippets.

@ospfranco
Last active March 13, 2025 12:04
Show Gist options
  • Save ospfranco/7b691ddc8f2fdba66dde5f67564a7c69 to your computer and use it in GitHub Desktop.
Save ospfranco/7b691ddc8f2fdba66dde5f67564a7c69 to your computer and use it in GitHub Desktop.
Cross-compile OpenSSL 3.X for Android
#!/bin/bash
# This script builds openSSL from source for the Android platform
# while patching some hand-written assembly that fails on Android
# It's useful when trying to use OpenSSL in Rust which tries to
# from the official sources and will therefore fail when launching
# your crate on Android
# Link to the main PR that fixes the sources:
# https://github.com/openssl/openssl/pull/22181
# Set the ANDROID_NDK_HOME environment variable for this script to work
set -ex
export OPENSSL_VERSION="openssl-3.1.4"
rm -rf ${OPENSSL_VERSION}
# Uncommment this lines to try to download the file, however this URL might have changed, you can just download it yourself
# curl -O "https://www.openssl.org/source/${OPENSSL_VERSION}.tar.gz"
# tar xfz "${OPENSSL_VERSION}.tar.gz"
PROJECT_HOME=`pwd`
PATH_ORG=$PATH
OUTPUT_DIR="libs"
# Clean output
rm -rf $OUTPUT_DIR
mkdir $OUTPUT_DIR
build_android_clang() {
echo ""
echo "----- Build libcrypto & libssl.so for "$1" -----"
echo ""
ARCHITECTURE=$1
TOOLCHAIN=$2
stl="libc++"
# Set toolchain
export TOOLCHAIN_ROOT=$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/darwin-x86_64
export SYSROOT=$TOOLCHAIN_ROOT/sysroot
export CC=${TOOLCHAIN}21-clang
export CXX=${TOOLCHAIN}21-clang++
export CXXFLAGS="-fPIC"
export CPPFLAGS="-DANDROID -fPIC"
export PATH=$TOOLCHAIN_ROOT/bin:$SYSROOT/usr/local/bin:$PATH
cd "${OPENSSL_VERSION}"
./Configure $ARCHITECTURE no-asm no-shared -D__ANDROID_API__=21
make clean
# Apply patch that fixes the armcap instruction
# Linux version
# sed -e '/[.]hidden.*OPENSSL_armcap_P/d; /[.]extern.*OPENSSL_armcap_P/ {p; s/extern/hidden/ }' -i -- crypto/*arm*pl crypto/*/asm/*arm*pl
# macOS version
sed -E -i '' -e '/[.]hidden.*OPENSSL_armcap_P/d' -e '/[.]extern.*OPENSSL_armcap_P/ {p; s/extern/hidden/; }' crypto/*arm*pl crypto/*/asm/*arm*pl
make
mkdir -p ../$OUTPUT_DIR/${ARCHITECTURE}/lib
mkdir -p ../$OUTPUT_DIR/${ARCHITECTURE}/include
cp libcrypto.a ../$OUTPUT_DIR/${ARCHITECTURE}/lib/libcrypto.a
cp libssl.a ../$OUTPUT_DIR/${ARCHITECTURE}/lib/libssl.a
cp -R include/openssl ../$OUTPUT_DIR/${ARCHITECTURE}/include
cd ..
}
build_android_clang "android-arm" "armv7a-linux-androideabi"
build_android_clang "android-x86" "i686-linux-android"
build_android_clang "android-x86_64" "x86_64-linux-android"
build_android_clang "android-arm64" "aarch64-linux-android"
export PATH=$PATH_ORG
@juha-h
Copy link

juha-h commented Oct 31, 2024

Thanks for the explanation.

@juha-h
Copy link

juha-h commented Oct 31, 2024

Forgot to mention that your script is still needed with openssl branch 3.4.

@ospfranco
Copy link
Author

ospfranco commented Oct 31, 2024

Yeah, as long as the hand-crafted assembly code is not patched and released it will be necessary.

Main discussion here:

openssl/openssl#22181

@kamari-swami
Copy link

Spent entire day to build openssl v3.4.1 with NDK r29-beta but kept failing.
This script worked like a charm!

Thanks a zillion ton!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment