skip to Main Content

I have a jar file containing .so and java files and I would like to load the native libraries (.so files) from my .class file.

-libs
    -myJar.jar
         - armeabi
             - libNativeFirst.so
             - libNativeSecond.so
         - sdk
             - classWithNativeMethods.class

Here’s how code for loading libraries look like in my class:

    static {
    try {
        Log.i(TAG, "Trying to load libNativeFirst.so");

        System.load("C:UsersuserStudioProjectsprojectprojectmodulelibsmyJar.jarlibNativeFirst.so");
        
        Log.i(TAG, "Loaded libNativeFirst.so");
    } catch (UnsatisfiedLinkError ule) {
        Log.e(TAG, "WARNING: Could not load libNativeFirst.so");
        ule.printStackTrace();
    } catch (Exception e) {
        Log.e(TAG, "EXCEPTION: " + e.getMessage());
        e.printStackTrace();
    }

    if (!OpenCVLoader.initDebug()) {
        // Handle initialization error
    }

}

But i get UnsatisfiedLinkError from system.load:
W/System.err: java.lang.UnsatisfiedLinkError: Expecting an absolute path of the library: C:UsersuserStudioProjectsprojectprojectmodulelibsmyJar.jar/libNativeFirst.so

How can i load a .so file within a jar?

2

Answers


  1. The native code loader do not know (yet) how to look inside jar files as it expects a plain file in the file system.

    You must unpack your libraries to be able to use them.

    Login or Signup to reply.
  2. Build an Android Library (AAR) rather than a JAR and this will work automatically. The JAR format does not have any standardized way of packaging and using native libraries. AAR does.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search