Horv
Horv

Reputation: 184

Android NDK linking OpenSSL

I want to use openssl to my android ndk project.

But when I build it, I got these errors:

Error:(38) undefined reference to SSL_library_init' Error:(39) undefined reference toSSL_load_error_strings' Error:(40) undefined reference to OPENSSL_add_all_algorithms_noconf' Error:(42) undefined reference toCRYPTO_num_locks' Error:(45) undefined reference to CRYPTO_set_locking_callback' Error:(46) undefined reference toCRYPTO_set_id_callback' etc..

I've included two files to Android.mk (one to ssl, one to crypto):

//libcrypto.mk
include $(CLEAR_VARS)

LOCAL_MODULE    := ssl-crypto
LOCAL_SRC_FILES := ../libs/system/$(TARGET_ARCH_ABI)/libcrypto.so

include $(PREBUILT_SHARED_LIBRARY)
//libssl.mk
include $(CLEAR_VARS)

LOCAL_MODULE    := ssl-ssl
LOCAL_SRC_FILES := ../libs/system/$(TARGET_ARCH_ABI)/libssl.so

include $(PREBUILT_SHARED_LIBRARY)

And I've added this to app/build.gradle:

    stl = "gnustl_static"

    cppFlags  += "-I${file("../../../../support-lib/jni")}".toString()
    cppFlags  += "-I${file("../../../generated-src/cpp")}".toString()
    cppFlags  += "-I${file("../../../taps-api")}".toString()
    cppFlags  += "-I${file("../../../include")}".toString()

    cppFlags  += "-std=c++11"
    cppFlags  += "-DASIO_STANDALONE"

    cppFlags  += "-lssl"
    cppFlags  += "-lcrypto"

I'm using openssl from C++, and generate jni wrappers with dropbox/djinni. Also I'm using Android Studio 1.3 stable and gradle 2.5

EDIT: I changed my build.gradle in the app:

abiFilters += "armeabi"
abiFilters += "armeabi-v7a"
abiFilters += "x86"
abiFilters += "mips"
ldLibs += ['ssl', 'crypto']     

android.sources {
   main {
      jniLibs {
        source {
          srcDirs 'jni/libs'
        }
   }
}

It worked, but the app crashed (because it's searching the libs in a wrong directory in the apk...). Also I found that in the new experimental gradle recently not supports this 'third-party shared lib including thing'. So I'll have to wait for it. (Also I can try something with the makefiles, but default they skipped by AS)

Upvotes: 4

Views: 5232

Answers (1)

stephane k.
stephane k.

Reputation: 1788

I do not believe you can have shared libraries as such in your app, you can only load an so object from Java with System.loadLibrary()

You need to compile OpenSSL statically into the rest of your C++ code (as in link the .a crypto and SSL libraries)

Here is an example

https://github.com/gcesarmza/curl-android-ios/blob/master/test-android-project/app/src/main/jni/Android.mk

the curl Android iOS project does that, it is worth having a look https://github.com/gcesarmza/curl-android-ios

Upvotes: 1

Related Questions