Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

External library and JNI with Android: dlopen failed: library "libcrypto.so.1.1" not found, even with SONAME set correctly

I've cross-compiled OpenSSL for Android (x86 and arm). I added the .so files (respectively: libcrypto.so and libssl.so) to the generated .apk file by using a jniLibs through build.gradle. I've checked, and the .so files are properly packed in the generated .apk.

Now, the .so files are properly named:

$ objdump -p libssl.so | grep SONAME
  SONAME               libssl.so.1.1
$ objdump -p libcrypto.so | grep SONAME
  SONAME               libcrypto.so.1.1

And yet, when the code is executed, I get the following error: java.lang.UnsatisfiedLinkError: dlopen failed: library "libcrypto.so.1.1" not found.

I mentioned in the title that I'm using JNI, because the issue actually comes when one of the JNI module is created.

The structure of my project is as follow:

  • I have a JNILibrary Android Studio project, whose goal is to generate an Android library (.aar file). It has 2 .java classes: AgentWrapper.java, and ClientWrapper.java, which both expose native methods. The CMakeLists.txt file compiles the C (JNI) code, and the generated .so files (libagent.so and libclient.so) are bundled in the final .aar.

  • This JNILibrary Android Studio project also contains a LibraryWrapper.java class ; it contains abstraction methods that handle the native methods implemented in JNI (accessible through AgentWrapper.java and ClientWrapper.java).

  • The C code of AgentWrapper.java uses the OpenSSL library. It's dependant of both libcrypto.so and libssl.so, which are both bundled in the generated .aar.

With that done, I create another Android Studio project, JNILibraryTestApk. I import the previously generated .aar library, and use it inside my MainActivity: LibraryWrapper.TestMethod();. This method calls a Client method, then a Agent method. When it reaches the Agent method, and tries to load the libcryto.so file... The application crashes and I am greeted by the usual error message.

Note: if I remove the part that calls a method from the Agent library (which is the one using libcrypto.so.1.1), the application runs fine, which proves that the issue is probably only coming from the libcrypto.so[.1.1] file missing/not found.

If anyone can help me with this, I'd be really grateful, I'm starting to lose my mind on this!

Cheers

like image 979
Nepho Avatar asked Oct 15 '25 12:10

Nepho


2 Answers

I believe that if the file is called "libcrypto.so", you should request it using:

System.loadLibrary("crypto");

like image 99
Pierre Avatar answered Oct 17 '25 03:10

Pierre


Based on information such as:

https://stackoverflow.com/a/55080634/4959635

Android's Gradle does not support versioned .sos so you should aim to use and build unversioned libraries.