11

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.......

Related :

How to build OpenSSL on Android/Linux ?

Community
  • 1
  • 1

2 Answers2

6

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

  • 1
    we actually maintain our own version of this now: https://github.com/guardianproject/android-ffmpeg – Hans-Christoph Steiner Feb 22 '13 at 19:27
  • 1
    We're using the openssl build provided in the guardianproject.. I'm now trying to upgrade the openssl version due to a recent android playstore security alert. I followed the steps in the Readme file but am stuck at: 5) Cleanup before building with: m -j16 clean-libcrypto clean-libssl clean-openssl clean-ssltest. to my knowledge m and mm are available when you setup for building android from source, so I get m: command not found and in the next step same for mm. Any help is greatly appreciated! – Nonos Apr 04 '16 at 16:26
  • @Nonos Did you get any solution to update version of OpenSSL? I am also stuck into step 5. Herte is my [question](http://stackoverflow.com/questions/36493508/how-to-update-openssl-version-in-csipsimple) if you could help me. – Asfak Saiyed Apr 19 '16 at 06:56
4

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 stumble opon could not load library ... needed by ... error then your library may have 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 the building and before the running of your application and it will remove the version number from the project files.

    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. Make sure that there is a network permission in the manifest.

bartolo-otrit
  • 2,396
  • 3
  • 32
  • 50
  • How should I load the libcrypto.so in Java? I used `System.loadLibrary` and `System.load`, but appears the apk use the libcrypto.so under `/system/lib`, because the new function can't be found in Android 4.2 libcrypto.so. – bluesky Apr 11 '19 at 13:17