已添加FLAG_IMMUTABLE仍触发Android PendingIntent S+版本报错原因咨询
你给出的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

