安卓14(sdk34)app启动闪退修复,FLAG_IMMUTABLE or FLAG_MUTABLE

296 阅读1分钟

安卓14(sdk34)app启动闪退修复,FLAG_IMMUTABLE or FLAG_MUTABLE

报错日志

com.abc.defg.test: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent. Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles.

翻译:

com.abc.defg.test:针对 S+(版本 31 及更高版本)要求在创建 PendingIntent 时指定 FLAG_IMMUTABLE 或 FLAG_MUTABLE 之一。 强烈考虑使用 FLAG_IMMUTABLE,仅当某些功能依赖于 PendingIntent 可变时才使用 FLAG_MUTABLE,例如如果需要与内联回复或气泡一起使用。

详细报错代码

FATAL EXCEPTION: pool-4-thread-1
Process: com.abc.defg.test, PID: 6218
java.lang.IllegalArgumentException: com.abc.defg.test: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.
Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles.
	at android.app.PendingIntent.checkPendingIntent(PendingIntent.java:428)
	at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:731)
	at android.app.PendingIntent.getBroadcast(PendingIntent.java:718)
	at androidx.work.impl.utils.ForceStopRunnable.getPendingIntent(ForceStopRunnable.java:174)
	at androidx.work.impl.utils.ForceStopRunnable.isForceStopped(ForceStopRunnable.java:108)
	at androidx.work.impl.utils.ForceStopRunnable.run(ForceStopRunnable.java:86)
	at androidx.work.impl.utils.SerialExecutor$Task.run(SerialExecutor.java:75)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:644)
	at java.lang.Thread.run(Thread.java:1012)

解决方法

安卓项目的build.gradle配置文件中加入依赖

implementation 'androidx.work:work-runtime-ktx:2.7.1'