1. 程式人生 > >java.lang.UnsatisfiedLinkError,findLibrary returned null的一種可能的解決方式

java.lang.UnsatisfiedLinkError,findLibrary returned null的一種可能的解決方式

今天弄NDK+JNI,編譯沒問題,但死活執行失敗,報的錯誤是:

 E/AndroidRuntime(10679): java.lang.UnsatisfiedLinkError: Couldn't load libfilterengine from loader dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.qihoo.abptest-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.qihoo.abptest-2, /vendor/lib, /system/lib]]]: findLibrary returned null

中間糾結的過程就不說了,直接說結果:

我出錯的時候是這麼寫的:

public class JniTest{
static{
System.loadLibrary("libfilterengine.so");
}
public long runJs(String js){
stringFromJNI();
return 1;
}
public native void  stringFromJNI();
}

後來改成這樣寫就可以了:

public class JniTest{
static{
System.loadLibrary("filterengine");
}
public long runJs(String js){
stringFromJNI();
return 1;
}
public native void  stringFromJNI();
}

看出區別了嗎?

就是loadLibrary()函式中,傳入的名字必須是沒有“lib”字首和“.so”字尾的,否則就會引發上面的錯誤。

看看文件中的描述:

void java.lang.Runtime.loadLibrary(String nickname)

Loads a shared library. Class loaders have some influence over this process, but for a typical Android app, it works as follows:

Given the name "MyLibrary", that string will be passed to . That means it would be a mistake for the caller to include the usual "lib"

prefix and ".so" suffix.

That file will then be searched for on the application's native library search path. This consists of the application's own native library directory followed by the system's native library directories.

然而呼叫System.load()的時候是可以寫全名的,我一開始都用的是load,結果踩坑了,一直以為是cpp交叉編譯的問題。。。