java.lang.NoClassDefFoundError: <clinit> failed for class xxx

这个错误是在我使用第三方监控sdk展示监控视频时发生的,而且运行在Android6.0以下版本的机器上时则不会崩溃。运行时崩溃的日志如下:

java.lang.NoClassDefFoundError: <clinit> failed for class org.MediaPlayer.PlayM4.Player; see exception in other thread
    at com.gexun.kanban.main.monitor.widget.PlaySurfaceView.processRealData(PlaySurfaceView.java:121)
    at com.gexun.kanban.main.monitor.widget.PlaySurfaceView.access$000(PlaySurfaceView.java:23)
    at com.gexun.kanban.main.monitor.widget.PlaySurfaceView$1.fRealDataCallBack(PlaySurfaceView.java:105)

意思是出现了java.lang.NoClassDefFoundError这种类型的错误,而原因是执行org.MediaPlayer.PlayM4.Player这个类的<clinit>方法失败导致的。

<clinit>是类和接口的初始化方法。Java虚拟机动态加载、链接和初始化类和接口,而初始化时就会执行这个方法。此方法包含类变量初始化和静态初始化语句。请参考:Chapter 5. Loading, Linking, and Initializing.

于是我就去检查org.MediaPlayer.PlayM4.Player这个类,类变量初始化没什么问题,唯一比较可疑的是这个静态代码块:

    static {
        System.loadLibrary("CpuFeatures");
    }

在这个静态代码块中加载了一个本地so库,联想到错误只会出现在Android6.0及以上版本,猜测可能是这个so库比较旧,不兼容6.0及以上版本。由于没有第三方sdk不在维护,只能将项目的targetSdkVersion改为23以下,然后就运行在6.0及以上的设备上就不在报错了。

而且,有趣的是有时候运行就会直接打印出下面这个错误:

java.lang.UnsatisfiedLinkError: dlopen failed: /data/app/com.gexun.kanban-1/lib/arm/libCpuFeatures.so: has text relocations
    at java.lang.Runtime.loadLibrary(Runtime.java:372)
    at java.lang.System.loadLibrary(System.java:1076)
    at org.MediaPlayer.PlayM4.Player.<clinit>(Player.java:988)
    at com.gexun.kanban.main.monitor.widget.PlaySurfaceView.processRealData(PlaySurfaceView.java:121)
    at com.gexun.kanban.main.monitor.widget.PlaySurfaceView.access$000(PlaySurfaceView.java:23)
    at com.gexun.kanban.main.monitor.widget.PlaySurfaceView$1.fRealDataCallBack(PlaySurfaceView.java:105)

即执行<clinit>方法失败的具体原因。果然是在加载libCpuFeatures.so这个库时出错。

其实在官方文档中已有说明:

On previous versions of Android, if your app requested the system to load a shared library with text relocations, the system displayed a warning but still allowed the library to be loaded. Beginning in this release, the system rejects this library if your app’s target SDK version is 23 or higher. To help you detect if a library failed to load, your app should log the dlopen(3) failure, and include the problem description text that the dlerror(3) call returns. To learn more about handling text relocations, see this guide.

也就是说,Android6.0及以上的系统将会拒绝加载包含text relocations的共享库。

所以,要么你就把targetSdkVersion改为23以下,要么你就修改so库的源码重新编译(如果可能)。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值