美文网首页
android NDK交叉编译静态openssl

android NDK交叉编译静态openssl

作者: 招金 | 来源:发表于2021-10-20 09:29 被阅读0次

由于项目需要android版本的内置openssl命令

下载openssl包openssl-1.1.1c.tar.gz

wget --no-check-certificate  https://www.openssl.org/source/openssl-1.1.1c.tar.gz

tar -xzvf openssl-1.1.1c.tar.gz

下载NDK交叉编译链

下载链接android-ndk-r16b-linux-x86_64.zip
解压unzip android-ndk-r16b-linux-x86_64.zip

把openssl_build.sh脚本放入openssl-1.1.1c

#!/bin/bash -e

ANDROID_TARGET_API=21
ANDROID_TARGET_ABI=armeabi-v7a
GCC_VERSION=4.9


WORK_PATH=$(cd "$(dirname "$0")";pwd)
ANDROID_NDK_PATH=${WORK_PATH}/../android-ndk-r16b
OPENSSL_SOURCES_PATH=${WORK_PATH}
OUTPUT_PATH=${WORK_PATH}/openssl_1.1.1c_${ANDROID_TARGET_ABI}

OPENSSL_TMP_FOLDER=/tmp/openssl_${ANDROID_TARGET_ABI}
rm -rf ${OPENSSL_TMP_FOLDER}
mkdir -p ${OPENSSL_TMP_FOLDER}
cp -r ${OPENSSL_SOURCES_PATH}/* ${OPENSSL_TMP_FOLDER}

function build_library {
    rm -rf ${OUTPUT_PATH}
    mkdir -p ${OUTPUT_PATH}
    make -j48 && make install
#    rm -rf ${OPENSSL_TMP_FOLDER}
#    rm -rf ${OUTPUT_PATH}/bin
#    rm -rf ${OUTPUT_PATH}/share
#    rm -rf ${OUTPUT_PATH}/ssl
#    rm -rf ${OUTPUT_PATH}/lib/engines*
#    rm -rf ${OUTPUT_PATH}/lib/pkgconfig
    echo "Build completed! Check output libraries in ${OUTPUT_PATH}"
}

if [ "$ANDROID_TARGET_ABI" == "armeabi" ]
then
    export ANDROID_NDK_HOME=${ANDROID_NDK_PATH}
    PATH=$ANDROID_NDK_HOME/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin:$PATH
    cd ${OPENSSL_TMP_FOLDER}
    ./Configure android-arm -DANDROID_API=${ANDROID_TARGET_API} no-shared no-tests --prefix=${OUTPUT_PATH}
    build_library

elif [ "$ANDROID_TARGET_ABI" == "armeabi-v7a" ]
then
    export ANDROID_NDK_HOME=${ANDROID_NDK_PATH}
    PATH=$ANDROID_NDK_HOME/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin:$PATH
    cd ${OPENSSL_TMP_FOLDER}
    ./Configure android-arm -DANDROID_API=${ANDROID_TARGET_API} no-shared no-tests --prefix=${OUTPUT_PATH}
    build_library

elif [ "$ANDROID_TARGET_ABI" == "arm64-v8a" ]
then
    export ANDROID_NDK_HOME=${ANDROID_NDK_PATH}
    PATH=$ANDROID_NDK_HOME/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/bin:$PATH
    cd ${OPENSSL_TMP_FOLDER}
    ./Configure android-arm64 -DANDROID_API=${ANDROID_TARGET_API} no-shared no-tests --prefix=${OUTPUT_PATH}
    build_library

elif [ "$ANDROID_TARGET_ABI" == "mips" ]
then
    export ANDROID_NDK_HOME=${ANDROID_NDK_PATH}
    PATH=$ANDROID_NDK_HOME/toolchains/mipsel-linux-android-4.9/prebuilt/linux-x86_64/bin:$PATH
    cd ${OPENSSL_TMP_FOLDER}
    ./Configure android-mips -DANDROID_API=${ANDROID_TARGET_API} no-shared no-tests --prefix=${OUTPUT_PATH}
    build_library

elif [ "$ANDROID_TARGET_ABI" == "mips64" ]
then
    export ANDROID_NDK_HOME=${ANDROID_NDK_PATH}
    PATH=$ANDROID_NDK_HOME/toolchains/mips64el-linux-android-4.9/prebuilt/linux-x86_64/bin:$PATH
    cd ${OPENSSL_TMP_FOLDER}
    ./Configure android-mips64 -DANDROID_API=${ANDROID_TARGET_API} no-shared no-tests --prefix=${OUTPUT_PATH}
    build_library

elif [ "$ANDROID_TARGET_ABI" == "x86" ]
then
    export ANDROID_NDK_HOME=${ANDROID_NDK_PATH}
    PATH=$ANDROID_NDK_HOME/toolchains/x86-4.9/prebuilt/linux-x86_64/bin:$PATH
    cd ${OPENSSL_TMP_FOLDER}
    ./Configure android-x86 -DANDROID_API=${ANDROID_TARGET_API} no-shared no-tests --prefix=${OUTPUT_PATH}
    build_library

elif [ "$ANDROID_TARGET_ABI" == "x86_64" ]
then
    export ANDROID_NDK_HOME=${ANDROID_NDK_PATH}
    PATH=$ANDROID_NDK_HOME/toolchains/x86_64-4.9/prebuilt/linux-x86_64/bin:$PATH
    cd ${OPENSSL_TMP_FOLDER}
    ./Configure android-x86_64 -DANDROID_API=${ANDROID_TARGET_API} no-shared no-tests --prefix=${OUTPUT_PATH}
    build_library

else
    echo "Unsupported target ABI: $ANDROID_TARGET_ABI"
    exit 1
fi

运行openssl_build.sh

  • openssl_1.1.1c_armeabi-v7a\bin
    openssl 就是我们需要的可执行程序

push openssl 到android,运行如下

1634693278(1).png

大功告成!

PS:如果想编译openssl动态库,也可以按照上面的脚本把 no-shared 参数去掉即可。

相关文章

网友评论

      本文标题:android NDK交叉编译静态openssl

      本文链接:https://www.haomeiwen.com/subject/ongaoltx.html