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”后缀的,否则就会引发上面的错误。

看看文档中的描述:

Open Declarationvoid 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 System.mapLibraryName. 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交叉编译的问题。。。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值