Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have an Android project that contains a class that uses JNI to pull a value from a C function. The C function was built into a library using NDK. The value returned from the C function is in turn used to initialize a variable inside a class when its first loaded. This works fine. However, I also want it to work when the library is missing by providing a default value. So Im using something like this:

static native String getstring();

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

Despite the catch, Im still getting a UnsatisfiedLinkError when I try to run this code with the library missing. Why am I not catching the exception? What am I doing wrong?

like image 894
Eno Avatar asked Jul 27 '10 22:07

Eno


People also ask

What is the Android NDK How can one use it why should one use it?

The Native Development Kit (NDK) is a set of tools that allows you to use C and C++ code with Android, and provides platform libraries you can use to manage native activities and access physical device components, such as sensors and touch input.

What compiler does Android NDK use?

Code written in C/C++ can be compiled to ARM, or x86 native code (or their 64-bit variants) using the Android Native Development Kit (NDK). The NDK uses the Clang compiler to compile C/C++.

What is JNI libs in Android?

jni/libs folder is where your shared library files are built from the C/C++ sources. Your native code gets compiled and depending on the value you had set in your application.mk file for the parameter APP_ABI: = <all | x86 | armv7a | armeabi-v7 | mips>

What is NDK and SDK in Android?

Android provides Native Development Kit (NDK) to support native development in C/C++, besides the Android Software Development Kit (Android SDK) which supports Java. [TODO] more.


1 Answers

UnsatisfiedLinkError is not a subclass of Exception. The hierarchy of UnsatisfiedLinkError is:

Throwable->Error->UnsatisfiedLinkError

You better catch UnsatisfiedLinkError if you want to handle it.

like image 164
ognian Avatar answered Sep 28 '22 19:09

ognian