Unity应用(target SDK 34)上线到GooglePlay,有用户反馈fold5设备上(Android14系统)疯狂闪退,经测试,在小米手机Android14系统的版本复现成功了,奇怪的是apk直接安装没问题,而打包成aab就是疯狂闪退。
老办法,logcat抓包,看看闪退日志。
日志有一行引起了我的注意,也就是在闪退前的报错:
No pending exception expected: java.lang.SecurityException: com.xxx.xxx: One of RECEIVER_EXPORTED or RECEIVER_NOT_EXPORTED should be specified when a receiver isn't being registered exclusively for system broadcasts
既然有错误,就好解决了,现在问ChatGPT没用的,因为ChatGPT经常会瞎编,所以上谷歌搜索下,立马就搜到了。?android - One of RECEIVER_EXPORTED or RECEIVER_NOT_EXPORTED should be specified when a receiver isn't being registered exclusively for system broadcasts - Stack Overflow
?As discussed at Google I/O 2023, registering receivers with intention using the RECEIVER_EXPORTED / RECEIVER_NOT_EXPORTED flag was introduced as part of Android 13 and is now a requirement for apps running on Android 14 or higher (U+).
If you do not implement this, the system will throw a security exception.
To allow the broadcast receiver to receive broadcasts from other apps, register the receiver using the following code:
context.registerReceiver(broadcastReceiver, intentFilter, RECEIVER_EXPORTED);
To register a broadcast receiver that does not receive broadcasts from other apps, including system apps, register the receiver using the following code:
context.registerReceiver(broadcastReceiver, intentFilter, RECEIVER_NOT_EXPORTED);
Note: That call will need minSdkVersion to be 26 (Android 8) al least
翻译过来就是,Goole I/O 2023讨论的,使用RECEIVER_EXPORTED / RECEIVER_NOT_EXPORTED标志注册接收者是Android 13的一部分,现在是运行在Android 14或更高版本(U+)上的应用程序的要求。也就是Android14必须增加该标志。
如何增加呢?在Activity里加上这句话即可,这样就不会抛出异常了。
context.registerReceiver(broadcastReceiver, intentFilter, RECEIVER_EXPORTED);
context.registerReceiver(broadcastReceiver, intentFilter, RECEIVER_NOT_EXPORTED);