Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rhulha/3a2bce7dc3d43c4bab8a14a31feecc38 to your computer and use it in GitHub Desktop.
Save rhulha/3a2bce7dc3d43c4bab8a14a31feecc38 to your computer and use it in GitHub Desktop.
Compiling an Android project that depends on OpenSSL using Prefabs
Prefabs are a new feature of the Android Gradle Plugin 4.0.
Here is an outdated but still informative blog post about the subject:
https://android-developers.googleblog.com/2020/02/native-dependencies-in-android-studio-40.html
In the app build.gradle you simply add:
dependencies {
implementation 'com.android.ndk.thirdparty:openssl:1.1.1g-alpha-1'
}
Here is a list of available packages:
https://maven.google.com/web/index.html?q=com.android.ndk.thirdparty#com.android.ndk.thirdparty:openssl
Currently available packages are: curl, jsoncpp and openssl.
In the android section in the app build.gradle you also add:
buildFeatures {
prefab true
}
And in the defaultConfig section in the android section in the app build.gradle you also add:
externalNativeBuild {
cmake {
arguments "-DANDROID_STL=c++_shared"
}
}
Otherwise you get this error message:
> User is using a static STL but library requires a shared STL
In the CMakeList.txt you add:
find_package(openssl REQUIRED CONFIG)
And to target_link_libraries you add:
openssl::crypto
openssl::ssl
@mrousavy
Copy link

mrousavy commented Nov 3, 2021

Thank you! 👏

@mrousavy
Copy link

mrousavy commented Nov 3, 2021

I'm trying to include

#include <openssl/sha.h>

and I added the OpenSSL library as you mentioned, unfortunately my app is crashing:

    java.lang.UnsatisfiedLinkError: dlopen failed: library "libcrypto.so" not found: needed by /data/app/~~2-M8lYDV9CwRUTUVJ-xI7Q==/com.MYAPPNKq4Bj7P2ot9_6vUGCPvhQ==/lib/x86/libMYLIB.so in namespace classloader-namespace

do you know why that is happening and what I'm doing wrong? alternatively, is there a way to link the lib statically, so libcrypto.a instead of libcrypto.so? Looks like someone on stackoverflow says that the library is compiled incorrectly, but that's not using a prefab...

@rhulha
Copy link
Author

rhulha commented Nov 3, 2021

Take a look at this example: that one might be more up to date: https://github.com/android/ndk-samples/tree/master/prefab/curl-ssl

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment