You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

已添加FLAG_IMMUTABLE仍触发Android PendingIntent S+版本报错原因咨询

已添加FLAG_IMMUTABLE仍收到PendingIntent兼容性警告的原因

你给出的PendingIntent创建代码:

val pendingIntent = PendingIntent.getActivities(this, requestCode, arrayOf(intent), PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)

Google控制台仍提示:

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.

已添加依赖:

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

可能的原因如下:

  • 存在未添加标记的其他PendingIntent实例:你贴的这段代码确实正确添加了FLAG_IMMUTABLE,但控制台的警告是针对项目中所有不符合要求的PendingIntent——可能是第三方SDK、自定义通知/广播的PendingIntent,甚至WorkManager某些未被覆盖的场景里,还有创建PendingIntent时没指定IMMUTABLE/MUTABLE标记的代码。

  • 依赖版本冲突:虽然你引入了work-runtime-ktx:2.7.0,但如果项目中其他依赖间接引入了旧版本的work-runtime(低于2.7.0),构建工具可能自动选择旧版本,而旧版本WorkManager内部创建PendingIntent时未适配S+系统的要求。可以查看项目依赖树确认实际生效的版本。

  • 位运算错误:检查其他PendingIntent创建代码,是否误将位运算符or写成了and,导致FLAG_IMMUTABLE被抵消,没有真正生效。

  • 混淆规则问题:ProGuard/R8混淆可能移除了FLAG_IMMUTABLE相关的常量引用,导致运行时实际没有设置该标记。需要确保混淆规则中保留了android.app.PendingIntent的flag字段。

内容的提问来源于stack exchange,提问作者Duseop

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 00:45:31