ThanosFisherman ThanosFisherman - 9 months ago 55
Android Question

Unit test Java class that loads native library

I'm running unit tests in Android Studio. I have a Java class that loads a native library with the following code


But when I test this class inside my
directory I get

java.lang.UnsatisfiedLinkError: no mylibrary in java.library.path
at java.lang.ClassLoader.loadLibrary(
at java.lang.Runtime.loadLibrary0(
at java.lang.System.loadLibrary(

How can I make it find the path of native .so libraries which is located at
in order to unit test without errors?

Note: inside
directory I have 3 more subdirectories:
. Each one of those contains the proper .so file. I'm using the Non experimental version for building NDK libs.

I don't wanna use other 3rd party testing libraries as all my other "pure" java classes can be unit tested fine. But if that's not possible then I'm open to alternatives.

Here is my test code which throws the error

public void testNativeClass() throws Exception
MyNativeJavaClass test = new MyNativeJavaClass("lalalal")
List<String> results = test.getResultsFromNativeMethodAndPutThemInArrayList();
assertEquals("There should be only three result", 3, results.size());

Answer Source

The only solution I found that works without hacks is to use JUnit through instrumentation testing (androidTest directory). My class can now be tested fine but with help of the android device or emulator.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download