如何在 ANDROID 应用程序中使用 openSSL 库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3046573/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:27:42  来源:igfitidea点击:

How to use openSSL Library in the ANDROID application

androidandroid-ndkjava-native-interfaceopensslnative

提问by Pankaj D. Gaurshettiwar

i am trying to embed the openssl library in my Android application using Android NDK but i don't know how to use exactly that library and so please any one can tell me how to use that please send a source code for my reference.......

我正在尝试使用 Android NDK 在我的 Android 应用程序中嵌入 openssl 库,但我不知道如何使用该库,所以请任何人告诉我如何使用它,请发送源代码以供我参考... ....

Related :

有关的 :

How to build OpenSSL on Android/Linux ?

如何在 Android/Linux 上构建 OpenSSL?

采纳答案by Hans-Christoph Steiner

Have you tried this, its a standalone build of the openssl that's included in Android: https://github.com/fries/android-external-openssl/blob/master/README.android

你试过这个吗,它是 Android 中包含的 openssl 的独立版本:https: //github.com/fries/android-external-openssl/blob/master/README.android

回答by bartolo-otrit

There are several tips about using OpenSSL with Android:

在 Android 上使用 OpenSSL 有几个技巧:

  1. It is necessary to build OpenSSL libraries using NDK tools, otherwise they will be incompatible with the NDK. Compiling the latest OpenSSL for Android

    CC=~/android-ndk-r9/toolchains/arm-linux-androideabi-4.8/prebuilt/darwin-x86_64/bin/arm-linux-androideabi-gcc
    ./Configure android-armv7
    export ANDROID_DEV=~/android-ndk-r9//platforms/android-8/arch-arm/usr
    make build_libs
    

    It is supposed that these commands are executed in the source directory of OpenSSL.

  2. In order to use these libraries (ssl and crypto) with your own library from the NDK, you need to create additional *.mk files in jni folder. For example:

    include $(CLEAR_VARS)
    
    LOCAL_MODULE    := ssl-crypto
    LOCAL_SRC_FILES := openssl-crypto/libcrypto.so
    
    include $(PREBUILT_SHARED_LIBRARY)
    

    and include them into the main Android.mk:

    include $(LOCAL_PATH)/openssl-ssl/Android.mk
    

    and probably add

    include $(CLEAR_VARS) 
    

    after it to avoid errors. Libraries will be placed into libs/armabiand .apk.

  3. If you will encounter with could not load library ... needed by ...error then it probably means that your library has soname with a version number. AFAIK NDK is unable to work with such libraries at this moment. There is a workaround (Dalvik is looking for .so file with '.0' extension - why?):

    rpl -R -e library.so.1.1 "library.so\x00\x00\x00\x00" libs obj
    

    where rplis a linux string replacement tool. Run this script after building and before running your application and it will remove version number from the project files. Follow the link for more information.

    If you use a C++ compiler you may get "undefined references" error in your C functions. Use extern "C" {}to avoid this (see "C++ name mangling" for more info).

  4. At last make sure that there is a network permission in the manifest.

  1. 有必要使用 NDK 工具构建 OpenSSL 库,否则它们将与 NDK 不兼容。为 Android 编译最新的 OpenSSL

    CC=~/android-ndk-r9/toolchains/arm-linux-androideabi-4.8/prebuilt/darwin-x86_64/bin/arm-linux-androideabi-gcc
    ./Configure android-armv7
    export ANDROID_DEV=~/android-ndk-r9//platforms/android-8/arch-arm/usr
    make build_libs
    

    假设这些命令是在 OpenSSL 的源目录中执行的。

  2. 为了将这些库(ssl 和加密)与您自己的 NDK 库一起使用,您需要在 jni 文件夹中创建额外的 *.mk 文件。例如:

    include $(CLEAR_VARS)
    
    LOCAL_MODULE    := ssl-crypto
    LOCAL_SRC_FILES := openssl-crypto/libcrypto.so
    
    include $(PREBUILT_SHARED_LIBRARY)
    

    并将它们包含在主 Android.mk 中:

    include $(LOCAL_PATH)/openssl-ssl/Android.mk
    

    并可能添加

    include $(CLEAR_VARS) 
    

    在它之后以避免错误。库将被放置到libs/armabi和 中.apk

  3. 如果您遇到could not load library ... needed by ...错误,则可能意味着您的库具有带有版本号的 soname。AFAIK NDK 目前无法使用此类库。有一个解决方法(Dalvik 正在寻找带有“.0”扩展名的 .so 文件 - 为什么?):

    rpl -R -e library.so.1.1 "library.so\x00\x00\x00\x00" libs obj
    

    rpllinux字符串替换工具在哪里。在构建之后和运行应用程序之前运行此脚本,它将从项目文件中删除版本号。点击链接了解更多信息。

    如果您使用 C++ 编译器,您的 C 函数可能会出现“未定义引用”错误。使用extern "C" {}避免这种(参见“C ++名称重整”获取更多信息)。

  4. 最后确保清单中有网络权限。