Linux 下 Android NDK 编译 openssl

准备工作

openssl 代码仓库 openssl 建议去这里下载源码,release版本比较稳定下载地址
安装Android NDK,可以去这里直接去下载对应于你操作系统的NDK、如果不能访问,修改hosts或者那啥啊(访问google)
我用的是linux下的zsh,将NDK的安装路径添加到环境变量,很多脚本和项目其实都会用到类似于NDK或者ANDROID_NDK之类的变量名称比如react native所以我多写了几个, 我写到了$HOME/.zshrc下
export ANDROID_NDK_ROOT=$HOME/Android/Sdk/ndk-bundle
export ANDROID_NDK=$ANDROID_NDK_ROOT
export NDK=$ANDROID_NDK
然后
source $HOME/.zshrc
添加环境变量。
下载openssl源码,我下载的版本是1.0.2k, 然后解压并进入解压目录
wget https://github.com/openssl/openssl/archive/OpenSSL_1_0_2k.tar.gz -O openssl.tar.gz
tar xvf openssl.tar.gz
cd openssl

编写shell脚本编译源码

vim build_android.sh
这里我以armv7-a架构为例子 建议platform为14,这个其实是参考 openssl wiki,arm64-v8a的话 platform调整到21,
OPENSSL_TMP_FOLDER=$PWD
OPENSSL_TARGET_API=android-14
OPENSSL_GCC_VERSION=4.9
OPENSSL_OUTPUT_PATH=$PWD/openssl_build
rm -rf $OPENSSL_OUTPUT_PATH
mkdir -p $OPENSSL_OUTPUT_PATH
mkdir -p $OPENSSL_TMP_FOLDER/android-toolchain-arm
NDK_MAKE_TOOLCHAIN=$NDK/build/tools/make-standalone-toolchain.sh
${NDK_MAKE_TOOLCHAIN} \
    --platform=${OPENSSL_TARGET_API} \
    --arch=arm \
    --toolchain=arm-linux-androideabi-${OPENSSL_GCC_VERSION} \
    --install-dir="${OPENSSL_TMP_FOLDER}/android-toolchain-arm" \
    --force
这里参考了 NDK 独立工具链的高级方法,其实网上很多资料也有提供,总之这样写可以不用写sysroot这个环境变量,方便点。更换cpu架构的时候要注意修改以下几行。目的就是为了安装NDK工具链
--toolchain=arm-linux-androideabi-${OPENSSL_GCC_VERSION}
--arch=arm
OPENSSL_TARGET_API=android-14

配置环境变量

export TOOLCHAIN_PATH="${OPENSSL_TMP_FOLDER}/android-toolchain-arm/bin"
export TOOL=arm-linux-androideabi
export NDK_TOOLCHAIN_BASENAME=${TOOLCHAIN_PATH}/${TOOL}
export CC=${NDK_TOOLCHAIN_BASENAME}-gcc
export CXX=${NDK_TOOLCHAIN_BASENAME}-g++
export LINK=${CXX}
export LD=${NDK_TOOLCHAIN_BASENAME}-ld
export AR=${NDK_TOOLCHAIN_BASENAME}-ar
export RANLIB=${NDK_TOOLCHAIN_BASENAME}-ranlib
export STRIP=${NDK_TOOLCHAIN_BASENAME}-strip
export ARCH_FLAGS="-march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16"
export ARCH_LINK="-march=armv7-a -Wl,--fix-cortex-a8"
export CPPFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 "
export CXXFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 -frtti -fexceptions "
export CFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 "
export LDFLAGS=" ${ARCH_LINK} "
最后, 生成Makefile make 安装 openssl 会安装到$OPENSSL_OUTPUT_PATH下
./Configure android-armv7 --openssldir=${OPENSSL_OUTPUT_PATH}
make && make install
写完了以后保存退出
./build_android.sh
通常情况下 $OPEM_OUT_PATH/lib 下的libcrypto.a libssl.a就是我们需要的,可以用readelf工具查看静态库支持的cpu架构
最终的shell script
#!/bin/sh

OPENSSL_TMP_FOLDER=$PWD
OPENSSL_TARGET_API=android-14
OPENSSL_GCC_VERSION=4.9
OPENSSL_OUTPUT_PATH=$PWD/openssl_build
rm -rf $OPENSSL_OUTPUT_PATH
mkdir -p $OPENSSL_OUTPUT_PATH
mkdir -p $OPENSSL_TMP_FOLDER/android-toolchain-arm
NDK_MAKE_TOOLCHAIN=$NDK/build/tools/make-standalone-toolchain.sh
${NDK_MAKE_TOOLCHAIN} \
    --platform=${OPENSSL_TARGET_API} \
    --arch=arm \
    --toolchain=arm-linux-androideabi-${OPENSSL_GCC_VERSION} \
    --install-dir="${OPENSSL_TMP_FOLDER}/android-toolchain-arm" \
    --force
export TOOLCHAIN_PATH="${OPENSSL_TMP_FOLDER}/android-toolchain-arm/bin"
export TOOL=arm-linux-androideabi
export NDK_TOOLCHAIN_BASENAME=${TOOLCHAIN_PATH}/${TOOL}
export CC=${NDK_TOOLCHAIN_BASENAME}-gcc
export CXX=${NDK_TOOLCHAIN_BASENAME}-g++
export LINK=${CXX}
export LD=${NDK_TOOLCHAIN_BASENAME}-ld
export AR=${NDK_TOOLCHAIN_BASENAME}-ar
export RANLIB=${NDK_TOOLCHAIN_BASENAME}-ranlib
export STRIP=${NDK_TOOLCHAIN_BASENAME}-strip
export ARCH_FLAGS="-march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16"
export ARCH_LINK="-march=armv7-a -Wl,--fix-cortex-a8"
export CPPFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 "
export CXXFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 -frtti -fexceptions "
export CFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 "
export LDFLAGS=" ${ARCH_LINK} "
cd ${OPENSSL_TMP_FOLDER}
rm $PWD/Makefile
echo $CC
./Configure android-armv7 --openssldir=${OPENSSL_OUTPUT_PATH}

make && make install
相应的 编译arm64-v8a的脚本
#!/bin/sh

OPENSSL_TMP_FOLDER=$PWD
OPENSSL_TARGET_API=android-21
OPENSSL_GCC_VERSION=4.9
TOOL=aarch64-linux-android-4.9
OPENSSL_OUTPUT_PATH=$PWD/openssl_build
rm -rf $OPENSSL_OUTPUT_PATH
mkdir -p $OPENSSL_OUTPUT_PATH
mkdir -p $OPENSSL_TMP_FOLDER/android-toolchain-aarch64
NDK_MAKE_TOOLCHAIN=$NDK/build/tools/make-standalone-toolchain.sh
${NDK_MAKE_TOOLCHAIN} \
    --platform=${OPENSSL_TARGET_API} \
    --arch=arm64 \
    --toolchain=aarch64-linux-android-${OPENSSL_GCC_VERSION} \
    --install-dir="${OPENSSL_TMP_FOLDER}/android-toolchain-aarch64" \
    --force
export TOOLCHAIN_PATH="${OPENSSL_TMP_FOLDER}/android-toolchain-aarch64/bin"
export TOOL=aarch64-linux-android
export NDK_TOOLCHAIN_BASENAME=${TOOLCHAIN_PATH}/${TOOL}
export CC=${NDK_TOOLCHAIN_BASENAME}-gcc
export CXX=${NDK_TOOLCHAIN_BASENAME}-g++
export LINK=${CXX}
export LD=${NDK_TOOLCHAIN_BASENAME}-ld
export AR=${NDK_TOOLCHAIN_BASENAME}-ar
export RANLIB=${NDK_TOOLCHAIN_BASENAME}-ranlib
export STRIP=${NDK_TOOLCHAIN_BASENAME}-strip
export ARCH_FLAGS=
export ARCH_LINK=
export CPPFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 "
export CXXFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 -frtti -fexceptions "
export CFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 "
export LDFLAGS=" ${ARCH_LINK} "
cd ${OPENSSL_TMP_FOLDER}
rm $PWD/Makefile
echo $CC
./Configure android --openssldir=${OPENSSL_OUTPUT_PATH}

make && make install

你可能感兴趣的:(Linux 下 Android NDK 编译 openssl)