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

Flutter安卓应用上传报错:PendingIntent需指定FLAG_IMMUTABLE/MUTABLE

安卓12+设备上Flutter应用恢复Activity时PendingIntent异常问题

问题概况

使用Flutter开发的安卓应用上传后出现崩溃,通过Play Console捕获到异常,触发在Activity恢复阶段,核心错误为安卓12(API 31)及以上版本要求创建PendingIntent时必须指定FLAG_IMMUTABLE或FLAG_MUTABLE标记。

异常详情

Exception java.lang.RuntimeException: Unable to resume activity : java.lang.IllegalArgumentException: 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.ActivityThread.performResumeActivity (ActivityThread.java:4803)
  at android.app.ActivityThread.handleResumeActivity (ActivityThread.java:4836)
  at android.app.servertransaction.ResumeActivityItem.execute (ResumeActivityItem.java:54)
  at android.app.servertransaction.ActivityTransactionItem.execute (ActivityTransactionItem.java:45)
  at android.app.servertransaction.TransactionExecutor.executeLifecycleState (TransactionExecutor.java:176)
  at android.app.servertransaction.TransactionExecutor.execute (TransactionExecutor.java:97)
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:2308)
  at android.os.Handler.dispatchMessage (Handler.java:106)
  at android.os.Looper.loopOnce (Looper.java:201)
  at android.os.Looper.loop (Looper.java:288)
  at android.app.ActivityThread.main (ActivityThread.java:7898)
  at java.lang.reflect.Method.invoke
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:548)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:936)

已尝试的无效操作

在模块级build.gradle中添加以下依赖,但问题未解决:

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.work:work-runtime-ktx:2.7.0'
}

应用依赖配置(pubspec.yaml)

version: 1.0.0+2

environment:
  sdk: '>=3.2.3 <4.0.0'

dependencies:
  flutter:
    sdk: flutter
  dio:
  lottie:
  mobile_scanner: ^3.5.6
  image_picker: ^1.0.7
  google_ml_kit: ^0.16.3
  any_link_preview:
    path: ./any_link_preview-modified/
  nfc_manager:
    git:
      url: https://github.com/okadan/flutter-nfc-manager
      branch: master
  flutter_native_splash: ^2.3.9
  url_launcher: ^6.2.4
  
dev_dependencies:
  flutter_test:
    sdk: flutter
  flutter_lints: ^2.0.0

解决方案

  • 排查并更新第三方插件:问题根源是第三方插件未适配安卓12+的PendingIntent要求,重点检查以下插件:
    • 将mobile_scanner、image_picker升级到最新稳定版本,主流插件已修复该适配问题
    • nfc_manager使用的是git master分支,确认该分支是否已处理PendingIntent适配;若未修复,切换至官方发布的稳定版本,或自行修改插件安卓原生代码,给PendingIntent添加FLAG_IMMUTABLE(仅在需要修改PendingIntent内容时使用FLAG_MUTABLE)
  • 强制升级安卓核心依赖:在模块级build.gradle中指定androidx.core:core版本至1.7.0及以上,该版本提供了适配安卓12+的PendingIntent创建方法:
    dependencies {
        // 保留原有依赖
        implementation 'androidx.core:core:1.12.0'
    }
    
  • 检查自定义原生代码:若项目包含自定义安卓原生代码(如MethodChannel实现),确保所有创建PendingIntent的代码都添加了正确的标记,优先使用FLAG_IMMUTABLE

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 02:30:32