openSSL 使用 Android 的 NDK 问题 [英] openSSL using Android's NDK problems

查看:52
本文介绍了openSSL 使用 Android 的 NDK 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下情况,我正在移植一个使用 OpenSSL 进行 AES 加密的应用程序,我已经编译了所有内容,但是链接器失败了.情况如下:1. 我写了一个简单的 JNI 包装器:

I have the following situation, I am porting a piece of an app using OpenSSL for AES encryption, I have everything compile, but the linker fails. The situation is the following: 1. I wrote a JNI wrapper that simply does :

private native String cipherString(String plainData, int datasize, String password, int passSize);
private native String decipherString(String cipheredData, int datasize, String password, int passSize);

接下来我有一个 C++ 文件,我调用它具有正确的 JNI 语法,它将 jstring 转换为 char * 和所有其他需要的转换,并调用另一个 cpp 文件,该文件实际上导入了 openssl 头文件(存在和说明)和调用 openssl 方法进行加密和解密.

next I have a c++ file which I call that has the proper JNI sintax which translates jstring to char * and all other needed transformations, and makes a call to another cpp file which actually imports openssl headers (present and accounted for) and calls openssl methods for ciphering and deciphering.

因此,当我调用 ndk-build 时,它会构建所有拇指,因此编译器会正确编译它们.接下来我需要为 android 移植 openssl,我使用了这个 OpenSSL for Android它就像一个带有简单 ndk-build 的字符(当然,在项目的根目录中)并构建 libssl.so 和 libcrypto.so

So when I call ndk-build, it builds all thumbs, so the compiler compiles them correctly. next i needed to port openssl for android, and I used this OpenSSL for Android which works like a char with a simple ndk-build (in the root of the project, ofcourse) and builds the libssl.so and libcrypto.so

所以我需要将两者连接起来..我发现连接构建脚本是一个挑战,以便一个 ndk-build 编译并链接所有内容(如果有人有时间,我会很感激一个简单的示例项目)

So I need to connect the two.. I find it a challenge to connect the build scripts, so that one ndk-build compiles and linkes everything (I would appreciate a simple example project if someone has the time for it)

所以我在 jni/includes/prebuilt 中复制了已编译的 libssl 和 libcrypto .so 文件,并希望将它们包含在项目中,以便链接器能够最终创建我将在最后使用的库.

so I copied the compiled libssl and libcrypto .so files in jni/includes/prebuilt and want to include them in the project for the linker to be able to finally create the lib which I will use at the end.

我有以下 Android.mk 文件

I have the following Android.mk file

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
include $(LOCAL_PATH)/includes/build/common.mk
include $(LOCAL_PATH)/includes/build/common_includes.mk
APP_STL := gnustl_static

LOCAL_MODULE    := packer
LOCAL_SRC_FILES := modules/cipher/wrapper.cpp 
                    ... #rest of the cpp code

LOCAL_C_INCLUDES += $(LOCAL_PATH)/includes/openssl 
LOCAL_SHARED_LIBRARIES := $(LOCAL_PATH)/includes/precompiled/libssl.so 
            $(LOCAL_PATH)/includes/precompiled/libcrypto.so 
LOCAL_SHARED_MODULES := sslx cryptox
include $(BUILD_SHARED_LIBRARY)

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := sslx
LOCAL_SRC_FILES := $(LOCAL_PATH)/includes/precompiled/libssh.so
include $(PREBUILT_SHARED_LIBRARY)

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := cryptox
LOCAL_SRC_FILES := $(LOCAL_PATH)/includes/precompiled/libssh.so
include $(PREBUILT_SHARED_LIBRARY)

当调用 ndk-build 时,我很失望

And when calling ndk-build I get a dissapointing

sslx: LOCAL_SRC_FILES points to a missing file. Check that /home/user/Development/Tools/sdk/android/ndk/build/core//home/user/Development/Tools/sdk/android/ndk/build/core/includes/precompiled/libssh.so exists  or that its path is correct. Aborting    .  Stop.

正如您已经猜到的路径是完全错误的,让我感到困惑的是 ${LOCAL_PATH} 为第一批包含返回了正确的路径,而对于 .so 文件则返回了一个完全错误的路径...任何帮助将不胜感激!

as you can already guess the path is totally wrong, and what confuses me is that ${LOCAL_PATH} returns correct path for the first batch of includes and a completely wrong one for the .so files ... Any help would be really appreciated!

推荐答案

这里是解决方案,更新到NDK8c

Here is the solution, updated to NDK8c

第 0 步:下载并修复 Android NDK我不知道如何,但 ndk 有一个非常有趣的缺陷,(在我看来)它不允许你编译很多东西,所以为了能够编译 OpenSSL,你需要做一个小的修复,无论你把 ndk8c 提取到哪里你的工具,然后编辑文件:android-ndk-r8c/build/gmsl/__gmsl第512行:换行

step zero: download and fix the Android NDK I dunno how but the ndk has a very interesting flaw, which (in my oppinion) doesn't allow you to compile lot's of stuff, so to be able to compile OpenSSL you need to make a small fix, extract the ndk8c whereever you keep your tools, and then edit the file : android-ndk-r8c/build/gmsl/__gmsl line 512 : change line

int_encode = $(__gmsl_tr1)$(wordlist 1,$1,$(__gmsl_input_int))

带线

int_encode = $(__gmsl_tr1)$(wordlist 1,$(words $1),$(__gmsl_input_int))

你可以走了!

第一步:下载 OpenSSL 并为 Android 编译:要么编译在此处找到的移植版本或者下载官方的1.0.0c版本的OpenSSL,然后使用我为Android兼容版本链接的github中提供的手册为android编译

step one : Download OpenSSL and compile for Android : either compile a ported version found here or Download the official 1.0.0c version of OpenSSL and then compile it for android using the manual provided in the github I linked for the Android compatible version

所以下一步是获取libssl.solibcrypto.so并将它们放在 NDK 文件夹中以便于访问,因此从

So the next step is to get the libssl.so and libcrypto.so and put the them in the NDK folder for easy access, so copy them from

openssl-folder/libs/armeabi/

android-ndk-r8c/platforms/android-8/arch-arm/usr/lib

通过这种方式,您可以在编译时使用简单的链接器开关 -lssl -lcrypto

this way when compiling you can include the libs using a simple linker switch -lssl -lcrypto

第二步:在此处获取 Curl 的最新资源

Step two : get Curl's latest source for here

在 Docs/INSTALL 中打开文件并按照制作独立工具链所需的步骤并将其放入所需的文件夹中,然后是棘手的部分,我需要有 android 的配置源代码才能继续,即使我有一个独立编译的 openssl 你也可以从那里包含头文件,无论如何这是更复杂的版本所以你选择你做什么,我没有选择逃避它们所以你可以去 Google AOSP 站点 并完成构建和初始化环境的步骤.

Open the file in Docs/INSTALL and follow the steps needed to make the standalone toolchain and put in your desired folder, and then the tricky part, I needed to have android's source code for the config to continue, even though I have a standalone compiled openssl you can include the header files from there too, in anycase this is the more complicated version so you choose what you do, I did not choose to evade them so you can go to Google AOSP site and go trough the steps to build and initialize the environment.

所以它会是这样的:

1.下载,

  1. 转到源代码的根目录并运行:

  1. go to root of the source code and run :

~: build/envsetup.sh;午餐1份;制作;

~: build/envsetup.sh; lunch 1; make;

所以最后我们需要编译支持 SSL 的 curl,所以,

So finally we need to compile curl with SSL support, so,

第三步

将 curl 解压到所需的文件夹(我特别希望禁用除 http/s 之外的所有内容,以使库尽可能小,这意味着大约 300k,如果您想在库中使用更多协议,请删除所需协议的 --disable-protocol)运行以下:

extract curl to the desired folder (I have a specific desire of disabling everything except http/s to keep the library as small as possible meaning about ~300k, if you want more protocols in your lib, remove the --disable-protocol for the desired protocol) run the following :

make clean

export PATH=/opt/arm-linux-androideabi-4.4.3/bin:$PATH

export LDFLAGS="
-lssl 
-lcrypto 
-L/home/user/Development/Tools/sdk/android/ndk/platforms/android-8/arch-arm/usr/lib"

export CFLAGS="
-I/home/user/Development/AOSP/2.3.7/system/core/include 
-I/home/user/Development/Tools/sdk/android/ndk/platforms/android-8/arch-arm/usr/include"

./configure --host=arm-linux-androideabi 
--with-ssl=/home/user/Development/Projects/portingLibs/openssl-android-master 
--disable-ftp 
--disable-gopher 
--disable-file 
--disable-imap 
--disable-ldap 
--disable-ldaps 
--disable-pop3 
--disable-proxy 
--disable-rtsp 
--disable-smtp 
--disable-telnet 
--disable-tftp 
--without-gnutls 
--without-libidn 
--without-librtmp 
--disable-dict


make



Note that in the block above, if you don't want to use the AOSP source, you could switch 

-I/home/user/Development/AOSP/2.3.7/system/core/include 

with the include folder for your ssl distribution.

所以最后你有:静态:

curl-7.28.1/lib/.libs/libcurl.a

并分享:

curl-7.28.1/lib/.libs/libcurl.so.5.3

就是这样..拿走文件,然后编译:)

So that's it.. take the file, and compile away :)

这篇关于openSSL 使用 Android 的 NDK 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆