1. 概述
OpenSSL可以编译成ARM下面的二进制代码(动态库或者静态库),方便APP使用,APP在使用的时候,需要使用JNI来进行调用。
官方WIKI有写如何为Android编译OpenSSL,地址是:https://wiki.openssl.org/index.php/Android
因此也是参考这篇文章实现的。
编译不太复杂,基本步骤如下:
- 下载NDK
- 下载OpenSSL源码和设置环境变量所需要的setenv-android.sh
- 配置编译参数
- 编译
2. 准备工作
2.1 环境
CentOS
2.2 安装Linux版本的NDK
编译Openssl需要使用NDK,先下载NDK:
wget https://dl.google.com/android/repository/android-ndk-r14b-linux-x86_64.zip
unzip android-ndk-r14b-linux-x86_64.zip
注意:NDK版本太新编译OpenSSL可能会出错
2.3 准备openssl源码和setenv-android.sh
wget https://www.openssl.org/source/openssl-1.1.1.tar.gz
wget https://wiki.openssl.org/images/7/70/Setenv-android.sh
注意:需要将OpenSSL和setenv-android.sh下载到同一个目录,其中setenv-android.sh是Setenv-android.sh而且脚本需要用gedit保存成Unix/Linux格式(不然脚本会报错),需要改名为setenv-android.sh
3. 配置环境
修改以下参数:具体参数可以参看你下载的NDK信息
_ANDROID_EABI="arm-linux-androideabi-4.8"
修改为GCC 4.9
_ANDROID_EABI="arm-linux-androideabi-4.9"
4. 开始编译
- 设置环境变量
我们需要通过环境变量来指定NDK所在的位置,按照之前的安装位置,我们只需要执行:
export ANDROID_NDK_ROOT=你的NDK路径
export ANDROID_NDK=你的NDK路径(openssl-1.1.1需要)
source ./setenv-android.sh
If you receive a meesage "Error: FIPS_SIG does not specify incore module, please edit this script, then its safe to ignore it(这个错误可以忽略,我们不用编译FIPS版本OpenSSL). setenv-android.sh is used to build both the FIPS Capable OpenSSL library and the non-FIPS version of the library. FIPS_SIG is not needed in this configuration.
- 创建输出目录
mkdir output
- 配置和编译
[scott@scott openssl-1.1.1]$ ./config no-shared no-comp no-hw no-engine --openssldir=/home/scott/AndroidStudioProjects/openssl-1.1.1/output/$ANDROID_API --prefix=/home/scott/AndroidStudioProjects/openssl-1.1.1/output/$ANDROID_API
Operating system: armv7-whatever-android
Configuring OpenSSL version 1.1.1 (0x1010100fL) for android-armeabi
Using os-specific seed configuration
Creating configdata.pm
Creating Makefile
**********************************************************************
*** ***
*** If you want to report a building issue, please include the ***
*** output from this command: ***
*** ***
*** perl configdata.pm --dump ***
*** ***
**********************************************************************
这里需要注意的是,no-shared表示不编译动态库,这样编译出来的openssl命令,不依赖动态库,同时也没有so产生。更多的编译参数,详见源码目录下的:INSTALL(该文件没有后缀)
- 安装
make install
执行完上面的命令,openssl的头文件、库文件、文档以及命令就被复制在:./output 目录里了。
目录主要结构是:
bin 存放openssl命令
include 头文件
lib 库文件
share 文档一类的
自己编译各个版本库,比较麻烦,网上有已经编译好的库:https://github.com/leenjewel/openssl_for_ios_and_android