├── .gitignore ├── README.md └── build-openssl-android.sh /.gitignore: -------------------------------------------------------------------------------- 1 | openssl-* 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # build-openssl-android 2 | Builds openssl library for android. Based on https://github.com/sqlcipher/android-database-sqlcipher/blob/gradle/android-database-sqlcipher/build-openssl-libraries.sh. 3 | 4 | ## Usage 5 | You must first install Android NDK and then build the openssl library with command like this: 6 | 7 | ``` 8 | ANDROID_NDK_ROOT=/Users/willyliu/Library/Android/sdk/ndk-bundle sh ./build-openssl-android.sh 16 21 9 | ``` 10 | 11 | The first number is the API_LEVEL for 32bit library, and the second number is the API_LEVEL for 64bit library. 12 | 13 | The result will be in the `openssl-lib` directory. -------------------------------------------------------------------------------- /build-openssl-android.sh: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env bash 2 | 3 | MINIMUM_ANDROID_SDK_VERSION=$1 4 | MINIMUM_ANDROID_64_BIT_SDK_VERSION=$2 5 | OPENSSL_FULL_VERSION="openssl-1.1.0h" 6 | 7 | if [ ! -f "$OPENSSL_FULL_VERSION.tar.gz" ]; then 8 | curl -O https://www.openssl.org/source/$OPENSSL_FULL_VERSION.tar.gz 9 | fi 10 | tar -xvzf $OPENSSL_FULL_VERSION.tar.gz 11 | 12 | (cd $OPENSSL_FULL_VERSION; 13 | 14 | if [ ! ${MINIMUM_ANDROID_SDK_VERSION} ]; then 15 | echo "MINIMUM_ANDROID_SDK_VERSION was not provided, include and rerun" 16 | exit 1 17 | fi 18 | 19 | if [ ! ${MINIMUM_ANDROID_64_BIT_SDK_VERSION} ]; then 20 | echo "MINIMUM_ANDROID_64_BIT_SDK_VERSION was not provided, include and rerun" 21 | exit 1 22 | fi 23 | 24 | if [ ! ${ANDROID_NDK_ROOT} ]; then 25 | echo "ANDROID_NDK_ROOT environment variable not set, set and rerun" 26 | exit 1 27 | fi 28 | 29 | ANDROID_LIB_ROOT=../openssl-lib 30 | ANDROID_TOOLCHAIN_DIR=/tmp/android-toolchain 31 | OPENSSL_CONFIGURE_OPTIONS="no-pic no-krb5 no-idea no-camellia \ 32 | no-seed no-bf no-cast no-rc2 no-rc4 no-rc5 no-md2 \ 33 | no-md4 no-ripemd no-rsa no-ecdh no-sock no-ssl2 no-ssl3 \ 34 | no-dsa no-dh no-ec no-ecdsa no-tls1 no-pbe no-pkcs \ 35 | no-tlsext no-pem no-rfc3779 no-whirlpool no-ui no-srp \ 36 | no-ssltrace no-tlsext no-mdc2 no-ecdh no-engine \ 37 | no-tls2 no-srtp -fPIC" 38 | 39 | HOST_INFO=`uname -a` 40 | case ${HOST_INFO} in 41 | Darwin*) 42 | TOOLCHAIN_SYSTEM=darwin-x86 43 | ;; 44 | Linux*) 45 | if [[ "${HOST_INFO}" == *i686* ]] 46 | then 47 | TOOLCHAIN_SYSTEM=linux-x86 48 | else 49 | TOOLCHAIN_SYSTEM=linux-x86_64 50 | fi 51 | ;; 52 | *) 53 | echo "Toolchain unknown for host system" 54 | exit 1 55 | ;; 56 | esac 57 | 58 | rm -rf ${ANDROID_LIB_ROOT} 59 | 60 | ./Configure dist 61 | 62 | for ANDROID_TARGET_PLATFORM in armeabi armeabi-v7a x86 x86_64 arm64-v8a 63 | do 64 | echo "Building for libcrypto.a and libssl.a for ${ANDROID_TARGET_PLATFORM}" 65 | case "${ANDROID_TARGET_PLATFORM}" in 66 | armeabi) 67 | TOOLCHAIN_ARCH=arm 68 | TOOLCHAIN_PREFIX=arm-linux-androideabi 69 | CONFIGURE_ARCH=android 70 | PLATFORM_OUTPUT_DIR=armeabi 71 | ANDROID_API_VERSION=${MINIMUM_ANDROID_SDK_VERSION} 72 | ;; 73 | armeabi-v7a) 74 | TOOLCHAIN_ARCH=arm 75 | TOOLCHAIN_PREFIX=arm-linux-androideabi 76 | CONFIGURE_ARCH=android -march=armv7-a 77 | PLATFORM_OUTPUT_DIR=armeabi-v7a 78 | ANDROID_API_VERSION=${MINIMUM_ANDROID_SDK_VERSION} 79 | ;; 80 | x86) 81 | TOOLCHAIN_ARCH=x86 82 | TOOLCHAIN_PREFIX=i686-linux-android 83 | CONFIGURE_ARCH=android-x86 84 | PLATFORM_OUTPUT_DIR=x86 85 | ANDROID_API_VERSION=${MINIMUM_ANDROID_SDK_VERSION} 86 | ;; 87 | x86_64) 88 | TOOLCHAIN_ARCH=x86_64 89 | TOOLCHAIN_PREFIX=x86_64-linux-android 90 | CONFIGURE_ARCH=android64 91 | PLATFORM_OUTPUT_DIR=x86_64 92 | ANDROID_API_VERSION=${MINIMUM_ANDROID_64_BIT_SDK_VERSION} 93 | ;; 94 | arm64-v8a) 95 | TOOLCHAIN_ARCH=arm64 96 | TOOLCHAIN_PREFIX=aarch64-linux-android 97 | CONFIGURE_ARCH=android64-aarch64 98 | PLATFORM_OUTPUT_DIR=arm64-v8a 99 | ANDROID_API_VERSION=${MINIMUM_ANDROID_64_BIT_SDK_VERSION} 100 | ;; 101 | *) 102 | echo "Unsupported build platform:${ANDROID_TARGET_PLATFORM}" 103 | exit 1 104 | esac 105 | 106 | rm -rf ${ANDROID_TOOLCHAIN_DIR} 107 | mkdir -p "${ANDROID_LIB_ROOT}/${ANDROID_TARGET_PLATFORM}" 108 | python ${ANDROID_NDK_ROOT}/build/tools/make_standalone_toolchain.py \ 109 | --arch ${TOOLCHAIN_ARCH} \ 110 | --api ${ANDROID_API_VERSION} \ 111 | --install-dir ${ANDROID_TOOLCHAIN_DIR} 112 | 113 | if [ $? -ne 0 ]; then 114 | echo "Error executing make_standalone_toolchain.py for ${TOOLCHAIN_ARCH}" 115 | exit 1 116 | fi 117 | 118 | export PATH=${ANDROID_TOOLCHAIN_DIR}/bin:$PATH 119 | export CROSS_SYSROOT=${ANDROID_TOOLCHAIN_DIR}/sysroot 120 | 121 | RANLIB=${TOOLCHAIN_PREFIX}-ranlib \ 122 | AR=${TOOLCHAIN_PREFIX}-ar \ 123 | CC=${TOOLCHAIN_PREFIX}-gcc \ 124 | ./Configure "${CONFIGURE_ARCH}" \ 125 | -D__ANDROID_API__=${ANDROID_API_VERSION} \ 126 | "${OPENSSL_CONFIGURE_OPTIONS}" 127 | 128 | if [ $? -ne 0 ]; then 129 | echo "Error executing:./Configure ${CONFIGURE_ARCH} ${OPENSSL_CONFIGURE_OPTIONS}" 130 | exit 1 131 | fi 132 | 133 | make clean 134 | make 135 | 136 | if [ $? -ne 0 ]; then 137 | echo "Error executing make for platform:${ANDROID_TARGET_PLATFORM}" 138 | exit 1 139 | fi 140 | 141 | mv libcrypto.a ${ANDROID_LIB_ROOT}/${PLATFORM_OUTPUT_DIR} 142 | mv libssl.a ${ANDROID_LIB_ROOT}/${PLATFORM_OUTPUT_DIR} 143 | 144 | # copy header 145 | mkdir -p "${ANDROID_LIB_ROOT}/${PLATFORM_OUTPUT_DIR}/include/openssl" 146 | cp -r "include/openssl" "${ANDROID_LIB_ROOT}/${PLATFORM_OUTPUT_DIR}/include/" 147 | done 148 | ) 149 | --------------------------------------------------------------------------------