Home > front end >  load a .so file within a jar
load a .so file within a jar

Time:08-06

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:\Users\user\StudioProjects\project\projectmodule\libs\myJar.jar\libNativeFirst.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:\Users\user\StudioProjects\project\projectmodule\libs\myJar.jar/libNativeFirst.so

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

CodePudding user response:

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.

CodePudding user response:

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.

  • Related