How to catch UnsatisifiedLinkError when using the NDK library in an Android app?

I have an Android project that contains a class that uses JNI to pull a value from function C. Function C has been built into the library using the NDK. The value returned by the C function, in turn, is used to initialize the variable inside the class when it is first loaded. It works great. However, I also want it to work when the library is missing, providing a default value. Therefore, I use something like this:

static native String getstring(); static { try { System.loadLibrary("library"); NAME = getstring(); } catch (Exception e) { NAME = "Default"; } } 

Despite the trick, I still get UnsatisfiedLinkError when I try to run this code with a missing library. Why can't I see an exception? What am I doing wrong?

+6
android linker android-ndk jni
source share
1 answer

UnsatisfiedLinkError not a subclass of Exception . The UnsatisfiedLinkError is:

 Throwable->Error->UnsatisfiedLinkError 

Better to catch UnsatisfiedLinkError if you want to handle it.

+10
source share

All Articles