Java – The best way to load multiple native libraries in Android apps

The best way to load multiple native libraries in Android apps… here is a solution to the problem.

The best way to load multiple native libraries in Android apps

I have an app that uses 3 different JAR libraries… We call them a.jar, b.jar, and c.jar.
Each of these JARS has an accompanying native C++ shared object.

Both a.jar and b.jar now import and use C.jar.

So, my question is – what is the best way to load a native library using system.loadLibrary?
Can I load them just from my application code, or do I have to load them by the respective JARs?
If I load them from their respective JAR files, will they be loaded in a separate thread?

Best Solution

Loading native libraries is the responsibility of the class.

Let’s assume that each jar file also has classes named A, B, and C. All of these classes will most likely need to be statically loaded with their native partners.

class A { 
    static { 
        System.loadLibrary(“A”); 
    }

    C c;
} 

class C { 
     static { 
         System.loadLibrary(“C”); 
     }
} 

In this structure, when you access class A, class C is loaded and initialized by the class loader.

Related Problems and Solutions