美文网首页移动流媒体Android技术知识Android知识
Andorid Studio NDK开发-编译OpenSSL类库

Andorid Studio NDK开发-编译OpenSSL类库

作者: 姜家志 | 来源:发表于2016-06-18 08:13 被阅读3870次

OpenSSL是一个强大的开源安全套接字层密码库,它包含了主要的密码学算法,常用的密钥和证书封装管理以及SSL协议,并提供丰富的应用程序供测试或其他目的使用。
Android上开发对于安全的需求越来越高,虽然OpenSSL出现过几次漏洞,但它仍然是在安全方面的使用最多的加密库之一。
OpenSSL是一个基于c语言开发的,古老的,开源的加密库,想要在Android上使用OpenSSL必须要借助NDK,先使用NDK编译成Android上面的动态连接库(或者静态链接库),再借助JNI层的封装,提供给Java层调用。
这篇文章主要写的是如何编译AndroidOpenSSL类库。参考OpenSSL的官方文档:https://wiki.openssl.org/index.php/Android

前期准备

环境准备:

  • 编译环境为MacOS
  • OpenSSL的源代码
  • Setenv-android.sh 构建脚本
  • 安装Make
  • 安装makedepend

OpenSSL可以在github上找到源代码,源代码地址:https://github.com/openssl/openssl

git clone git@github.com:openssl/openssl.git

也可以在官网上下载最新的release版本:https://www.openssl.org/source/
由于OpenSSL项目的主干(master)上提交的是开发分支,最好把OpenSSL切换到最新的release版本上面

git checkout OpenSSL_1_1_0e

Setenv-android.sh是用来编译Android上的OpenSSL的脚本,下载地址:https://wiki.openssl.org/images/7/70/Setenv-android.sh

wget https://wiki.openssl.org/images/7/70/Setenv-android.sh

Setenv-android.sh脚本可以运行的权限

chmod a+x Setenv-android.sh

安装makedepend

brew install makedepend

运行脚本

Setenv-android.sh脚本的作用是用来给编译OpenSSL配置Android编译的环境变量的。脚本下载完成之后是不能直接运行,原因在于脚本里面的变量并没有配置,需要配置变量:

ANDROID_NDK_ROOT:
ANDROID_ARCH: arch-arm
ANDROID_EABI: arm-linux-androideabi-4.9
ANDROID_API: android-23
ANDROID_SYSROOT: /platforms/android-23/arch-arm
ANDROID_TOOLCHAIN:
FIPS_SIG:
CROSS_COMPILE: arm-linux-androideabi-
ANDROID_DEV: /platforms/android-23/arch-arm/usr

已经设置了Android NDKAndroid SDK变量的,只需要把正确的值配置到脚本就可以了,设置脚本中的变量:

ANDROID_NDK_ROOT=$NDK_HOME
_ANDROID_API="android-23"
_ANDROID_EABI="arm-linux-androideabi-4.9"

运行脚本:

./Setenv-android.sh

没有Error出现就表示配置正确了。

编译OpenSSL

上面的配置已经给OpenSSl的编译环境设置了环境变量例如:

export MACHINE=armv7
export RELEASE=2.6.37
export SYSTEM=android
export ARCH=arm

根据上面的设置的环境变量,再运行./config就可以实现编译Android上OpenSSL的配置,make就可以开始编译了。
因为在Android设备上面运行,建议不要编译完整的OpenSSL库,官方给的建议编译Android的选项:

shared,no-ssl2,no-ssl3,no-comp,no-hw,no-engine

编辑OpenSSl的类库可以安装到本地,这样可以像使用NDK中其他库一样的使用OpenSSL,编译命令可以设置--openssldir用来指定OpenSSL的安装目录。

cd openssl-1.0.1t
perl -pi -e 's/install: all install_docs install_sw/install: install_docs install_sw/g' Makefile.org
./config shared no-ssl2 no-ssl3 no-comp no-hw no-engine --openssldir=/usr/local/ssl/$ANDROID_API

运行到这里的时候得到一个这样的信息:

perating system: i686-apple-darwinDarwin Kernel Version 15.5.0: Tue Apr 19 18:36:36 PDT 2016; root:xnu-3248.50.21~8/RELEASE_X86_64
WARNING! If you wish to build 64-bit library, then you have to
         invoke './Configure darwin64-x86_64-cc' *manually*.
         You have about 5 seconds to press Ctrl-C to abort.

编译的OpenSSL使用的是本地的darwin64-x86_64-cc,并不是想要的arm-linux-androideabi-gcc编译的,编译的这个类库是不能在Android上面使用的。原因是什么呢?查看了下环境变量:

echo $ANDROID_API

发现这个变量没有配置,上面的环境变量的配置Setenv-android.sh在设置的环境变量并没有起作用。

优化编译脚本

Setenv-android.sh中,尝试下打印$ANROID_API的值,打印$ANDROID_API的内容:

echo "ANDROID_API: echo $ANDROID_API"

得到结果为:

...
ANDROID_API: android-23
...

可以看到$ANDROID_API变量在Setenv-android.sh的生命周期内是有效的,而脚本运行结束之后设置的变量没有设置成功这个应该是因为我使用了fish有关,因此我就考虑把编译的命令都放在Setenv-adnroid.sh里,在Setenv-android.sh的生命周期内运行完所有的编译命令。
重新建一个脚本名字为build-android-openssl.sh
复制配置好的Setenv-android.sh的内容到build-android-openssl.sh,再添加命令:

cd openssl
make clean
perl -pi -e 's/install: all install_docs install_sw/install: install_docs install_sw/g' Makefile.org
./config shared no-ssl2 no-ssl3 no-comp no-hw no-engine --openssldir=/usr/local/ssl/$ANDROID_API
make depend
make all
sudo -E make install CC=$ANDROID_TOOLCHAIN/arm-linux-androideabi-gcc RANLIB=$ANDROID_TOOLCHAIN/arm-linux-androideabi-ranlib

注:该脚本和openssl源码的目录是同一级目录。
完成编译之后可以看到/usr/local/ssl/android-23有生成了对应的库和文件,OpenSSL源代码目录下生成了:libcrypto.solibcrypto.a
这样就完成了Android的OpenSSL库编译完成,下一步就可以尝试在NDK中引用OpenSSL了。

优化后的脚本地址:https://github.com/jjz/script/blob/master/build_android_openssl.sh

相关文章

网友评论

  • 406c6b68f4ae:./config shared no-ssl2 no-ssl3 no-comp no-hw no-engine编译出来的so库2M多,我只用到几种算法,能不能进一步精简?
  • 奔跑的羊驼:楼主,我是Windows下,怎么去做呢?求帮助
  • cf7f5a463e46:您好,我按照文章中的步骤进行,出现了error,求指点:
    In file included from /usr/include/assert.h:35:0,
    from crypto/aes/aes_ecb.c:10:
    /usr/include/features.h:374:25: fatal error: sys/cdefs.h: No such file or directory
    # include <sys/cdefs.h>
    ^
    compilation terminated.
    make[1]: *** [crypto/aes/aes_ecb.o] Error 1
    make[1]: Leaving directory `/home/saisai/Downloads/openssl-1.1.0d'
    make: *** [all] Error 2
    片片碎:我也遇到这个问题,include文件核对过 是ok 的
    姜家志:是不是include的路径不对
  • cf7f5a463e46:您好,我是一个新手,linux刚开始接触,我现在需要编译openssl新版本的android平台上的动态库文件,也就是 armeabi /armeabi-v7a /arm-v8a 这三个架构的动态库文件,也就是对应的libcrypto.so和libssl.so文件,请问我该如何编写脚本呢,是否能将您之前编译的整个项目分享一下作为参考呢?谢谢
    00d9e417a9a9:自己写很费劲啊,用别人写好的不行码
    姜家志: @徐赛赛 文章里面有项目地址吧
  • jcccn:按照你的方法生成的是armv7a架构的so。如果想要生成arm架构的so,应该怎么修改呢?我试过修改MACHINE=armv5,但是运行错误。
    czgsh:如何安装Make和
    安装makedepend呢
    00d9e417a9a9:需要修改的地方挺多的
    姜家志:修改_ANDROID_ARCH

本文标题:Andorid Studio NDK开发-编译OpenSSL类库

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