使用OpenSSL Android的NDK问题问题、OpenSSL、Android、NDK

2023-09-13 00:35:19 作者:萌面欧巴桑

我有以下的情况, 我移植了一块使用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 sintax它转换的jstring为char *和所有其他必要的变革,并使得调用,实际上进口的OpenSSL头(present,占另一个CPP文件对),并调用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建造,它建立笨手笨脚的,所以编译器编译正确它们。 接下来,我需要的端口OpenSSL的为Android,而我用这个 OpenSSL的Andr​​oid版 它的工作原理是用一个简单的NDK建造一个char(在项目的根着,当然),并建立了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建造编译和linkes一切(我想AP preciate如果有人有时间一个简单的例子项目它)

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)

所以我复制编译的libssl和libcrypto在JNI /包括/ prebuilt .so文件,并希望将其列入该项目的链接器能够最终创造,我将用在最后的lib。

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建造我得到一个dissapointing

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文件... 任何帮助将是非常美联社preciated!

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

一步零:下载并修复了Android NDK 我不知道怎样,但NDK有一个很有趣的缺陷,这(在我看来)不会让你编很多的东西,这样一来就能编译OpenSSL的,你需要做一个小的修复,提取ndk8c。无论您保持你的工具,然后编辑该文件: Android的NDK-R8C /建设/ G​​MSL / __ 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和编译的Andr​​oid版本: 无论是编译移植版本,发现 此处 或下载的OpenSSL的官方1.0.0c版本,然后用在github上我挂了Android的兼容版本提供的说明书编译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.so 和 libcrypto.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

第二步:把卷曲的最新源代码的 这里

Step two : get Curl's latest source for here

中打开文档文件/安装并按照需要作出独立的工具链,并在您需要的文件夹中的步骤,再棘手的部分,我需要有Android的源$ C ​​$ C的配置继续,甚至但我有一个独立的编译OpenSSL的,您可以包含有头文件也是如此,在anycase是这样的话,你选择什么你做什么,我没有选择逃避他们,你可以去的谷歌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.download,

1.download,

去源$ C ​​$ C根目录并运行:

go to root of the source code and run :

〜:编译/ envsetup.sh;午餐1;令;

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

所以最后我们需要编译卷曲SSL支持,所以,

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

第三步

提取物卷曲到所需的文件夹 (我有禁止一切除了HTTP / S特定的愿望,以保持库小至〜300K,如果你在你的lib希望更多的协议,删除 - 禁用协议所需的协议可能意) 运行以下命令:

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.

所以最后你有: 静态的:

So finally you have : static :

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 :)