您的当前位置:首页正文

重要:升级 Android14 后出现报错:One of RECEIVER_EXPORTED or RECEIVER_NOT_EXPORTED should be specified ……

2024-11-13 来源:个人技术集锦

老项目升级到Android14出现错误:

java.lang.ExceptionInInitializerError
	at com.jeremyliao.liveeventbus.core.LiveEventBusCore.get(LiveEventBusCore.java:49)
	at com.jeremyliao.liveeventbus.LiveEventBus.get(LiveEventBus.java:35)
	at com.lib.common.ui.base.CommonActivity.initEvent(CommonActivity.kt:53)
	at com.lib.common.ui.base.CommonActivity.onCreate(CommonActivity.kt:29)
	at com.lib.common.ui.activity.DeviceMainActivity.onCreate(DeviceMainActivity.kt:141)
	at android.app.Activity.performCreate(Activity.java:8595)
	at android.app.Activity.performCreate(Activity.java:8573)
	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1456)
	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3764)
	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3922)
	at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
	at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:139)
	at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:96)
	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2443)
	at android.os.Handler.dispatchMessage(Handler.java:106)
	at android.os.Looper.loopOnce(Looper.java:205)
	at android.os.Looper.loop(Looper.java:294)
	at android.app.ActivityThread.main(ActivityThread.java:8177)
	at java.lang.reflect.Method.invoke(Native Method)
	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971)
Caused by: java.lang.SecurityException: com.lydsto.robotvacuum: One of RECEIVER_EXPORTED or RECEIVER_NOT_EXPORTED should be specified when a receiver isn't being registered exclusively for system broadcasts
	at android.os.Parcel.createExceptionOrNull(Parcel.java:3057)
	at android.os.Parcel.createException(Parcel.java:3041)
	at android.os.Parcel.readException(Parcel.java:3024)
	at android.os.Parcel.readException(Parcel.java:2966)
	at android.app.IActivityManager$Stub$Proxy.registerReceiverWithFeature(IActivityManager.java:5684)
	at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1852)
	at android.app.ContextImpl.registerReceiver(ContextImpl.java:1792)
	at android.app.ContextImpl.registerReceiver(ContextImpl.java:1780)
	at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:755)
	at com.jeremyliao.liveeventbus.core.LiveEventBusCore.registerReceiver(LiveEventBusCore.java:127)
	at com.jeremyliao.liveeventbus.core.LiveEventBusCore.<init>(LiveEventBusCore.java:84)
	at com.jeremyliao.liveeventbus.core.LiveEventBusCore.<init>(LiveEventBusCore.java:39)
	at com.jeremyliao.liveeventbus.core.LiveEventBusCore$SingletonHolder.<clinit>(LiveEventBusCore.java:45)
	at com.jeremyliao.liveeventbus.core.LiveEventBusCore.get(LiveEventBusCore.java:49) 
	at com.jeremyliao.liveeventbus.LiveEventBus.get(LiveEventBus.java:35) 

根据错误定位到问题是由于使用了一个三方库导致的:

implementation 'com.jeremyliao:live-event-bus:1.7.3'

进一步分析三方库错误原因:

Caused by: java.lang.SecurityException: com.lydsto.robotvacuum: One of RECEIVER_EXPORTED or RECEIVER_NOT_EXPORTED should be specified when a receiver isn't being registered exclusively for system broadcasts

错误描述翻译过就是:在注册广播接收器(application.registerReceiver())时需要 指定导出标志 为RECEIVER_EXPORTED 或者 RECEIVER_NOT_EXPORTED;

查看三方库源码进一步确认问题:

确实是没有根据版本判断进行flags赋值,修改后:

问题解决。

总结:Android14 强制要求  注册广播接收器(application.registerReceiver())时需要 指定导出标志 为RECEIVER_EXPORTED 或者 RECEIVER_NOT_EXPORTED。

Top