Search code examples
androidjava-native-interfaceunsatisfiedlinkerror

Android UnsatisfiedLinkError when using native library


I have been stuck on this for weeks already.

I have been using libtorrent in an Android torrent client. Recently, I wanted to add new features, such as magnet links.

All native functions are declared in PROJECT_FOLDER/jni/libtorrent.h and implemented in PROJECT_FOLDER/jni/libtorrent.cpp.

So far, nothing went wrong, but recently, I added this new function in libtorrent.h:

JNIEXPORT jstring JNICALL Java_com_my_package_LibTorrent_MagnetToTorrent
(JNIEnv *env, jobject obj, jstring MagnetLink, jstring TorrentFolder);

I added its implementation in libtorrent.cpp

JNIEXPORT jstring JNICALL Java_com_my_package_LibTorrent_MagnetToTorrent
(JNIEnv *env, jobject obj, jstring MagnetLink, jstring TorrentFolder) {
        //function code here
}

I ran ndk-build on the code, and it compiled.

In com.my.package.LibTorrent class, I added the following declaration, same way that I had previously declared other native methods, which worked fine:

public native String MagnetToTorrent(String MagnetLink, String TorrentFolder);

But whenever I call it, I get UnsatisfiedLinkError: MagnetToTorrent. This is really weird, since I added native functions before, and they worked fine.

Any help is very much appreciated. Thank you.

EDIT: All the functions declared in libtorrent.h are surrounded with extern "C" {} like this:

#ifdef __cplusplus
extern "C" {
#endif
/*Function declarations*/
#ifdef __cplusplus
}
#endif

Solution

  • I finally figured out what was wrong. I had to add this line to jni/Application.mk:

    APP_ABI := armeabi armeabi-v7a
    

    This causes the native code to be also built for ARMv7 processors, which is what I have on my new phone.

    All the other answers should also be useful for other developers with similar problems. I marked +1 for each of them. For other issues that might cause the app to throw UnsatisfiedLinkError, this link might be helpful: http://developer.android.com/guide/practices/jni.html#faq_ULE