How to use openSSL Library in the ANDROID application

前端 未结 2 554
一向
一向 2021-02-05 23:15

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 u

相关标签:
2条回答
  • 2021-02-05 23:55

    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

    0 讨论(0)
  • 2021-02-05 23:59

    There are several tips about using OpenSSL with Android:

    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/armabi and .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 rpl is 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.

    0 讨论(0)
提交回复
热议问题