Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error while loading native library in Android Application

I'm having some troubles about using a shared library (.so) in an Android application. I have 3 projects:

  1. Native code project => build c++ files in .so library Let's call it libNative.so

  2. Android library project => calls the native code. The output of this project is a .jar file. Let's call it "stack.jar"

  3. Android application sample which uses the android library project.

In the sample application (3) libraries' properties tab, I configured the stack.jar file to be linked with the .so library. My .so native library is well included in the generated APK. => Eclipse copies it in the "lib/armeabi" folder.

If I launch the APK on a phone, I get the following error:

Caused by: java.lang.UnsatisfiedLinkError: Couldn't load libNative:
findLibrary returned null at
java.lang.Runtime.loadLibrary(Runtime.java:365) at
java.lang.System.loadLibrary(System.java:535)

I'm guessing the error is that Eclipse copies the .so library in a lib folder instead of libs , and that System.loadLibrary is looking for the path libs/armeabi..., right ? Or I may be totally wrong !

Thanks a lot for your help,

EDIT: when I install the APK, the .so lib is well copied in data/data/mypackage/lib folder. Still, I can't load it. Is it because the load is happening in the .jar file and not directly in the application sample ??

like image 807
hico Avatar asked Apr 07 '26 20:04

hico


1 Answers

According you have generated a dynamic library called libExample.so

  1. Put the libExample.so in the libs/armeabi folder of your project (create it if it doesn't exist)
  2. Write in a class the following code as fadden said:

    try
    {
        System.loadLibrary("Example");
    }
    catch (UnsatisfiedLinkError use)
    {
        Log.e("JNI", "WARNING: Could not load native library");
    }
    

    Should work !

like image 158
hico Avatar answered Apr 09 '26 09:04

hico