项目中需要用到openssl库的AES加密,写下来方便以后查阅。
编译
1、安装Linux版本的NDK
编译openssl需要使用NDK,先下载NDK:
# 在home目录下建立ndk目录
$ mkdir android
$ cd android
$ wget https://dl.google.com/android/repository/android-ndk-r14b-linux-x86_64.zip
$ unzip android-ndk-r14b-linux-x86_64.zip
最后NDK被解压在:/home/zoujin6649/android/android-ndk-r14b
2、下载openssl源码
# 下载openssl源码
$ cd android
$ wget https://www.openssl.org/source/old/1.1.1/openssl-1.1.1a.tar.gz
$ tar -zxvf openssl-1.1.1a.tar.gz
3、开始编译
根据NOTES.ANDROID文件提示的执行以下命令设置环境变量并安装
$ cd /home/zoujin6649/android/openssl-1.1.1b
$ rm -rf ./output
$ mkdir ./output
$ export ANDROID_NDK=/home/zoujin6649/android/android-ndk-r14b
$ export PATH=$ANDROID_NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin:$PATH
$ ./Configure android-arm -D__ANDROID_API__=23 --prefix=/home/zoujin6649/android/openssl-1.1.1b/output
$ make && make install
4、编译其他cpu架构的库
# armeabi armeabi-v7a armeabi-v7a-hard arm64-v8a mips mips64 x86 x86_64
$ export ARCH=armeabi
$ cd /home/zoujin6649/android/openssl-1.1.1b
$ rm -rf ./output-$ARCH
$ mkdir ./output-$ARCH
$ export ANDROID_NDK=/home/zoujin6649/android/android-ndk-r14b
$ export PATH=$ANDROID_NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin:$PATH
$ ./Configure android-arm -D__ANDROID_API__=23 --prefix=/home/zoujin6649/android/openssl-1.1.1b/output-$ARCH
$ make && make install
5、编译脚本编译各cpu架构的库
编写脚本build-android.sh如下:
func_cmp(){
cd /home/zoujin6649/android/openssl-1.1.1b
make clean
rm -rf ./output-$ARCH
mkdir ./output-$ARCH
export ANDROID_NDK=/home/zoujin6649/android/android-ndk-r14b
export PATH=$ANDROID_NDK/toolchains/$TOOL_CHAIN/prebuilt/linux-x86_64/bin:$PATH
./Configure android-$ARCH_NAME -D__ANDROID_API__=23 --prefix=/home/zoujin6649/android/openssl-1.1.1b/output-$ARCH
make && make install
}
for ARCH in armeabi-v7a armeabi arm64-v8a x86 x86_64 mips mips64
do
echo $ARCH
if [ "$ARCH" = "armeabi-v7a" ]; then
ARCH_NAME=android-arm
TOOL_CHAIN=arm-linux-androideabi-4.9
fi
if [ "$ARCH" = "armeabi" ]; then
ARCH_NAME=android-arm
TOOL_CHAIN=arm-linux-androideabi-4.9
fi
if [ "$ARCH" = "arm64-v8a" ]; then
ARCH_NAME=android-arm64
TOOL_CHAIN=aarch64-linux-android-4.9
fi
if [ "$ARCH" = "mips" ]; then
ARCH_NAME=android-mips
TOOL_CHAIN=mipsel-linux-android-4.9
fi
if [ "$ARCH" = "mips64" ]; then
ARCH_NAME=android-mips64
TOOL_CHAIN=mips64el-linux-android-4.9
fi
if [ "$ARCH" = "x86" ]; then
ARCH_NAME=android-x86
TOOL_CHAIN=x86-4.9
fi
if [ "$ARCH" = "x86_64" ]; then
ARCH_NAME=android-x86_64
TOOL_CHAIN=x86_64-4.9
fi
echo $TOOL_CHAIN
func_cmp
done
执行编译
$ cd /home/zoujin6649/android/openssl-1.1.1b
$ sh build-android.sh