Android12构建错误:Targeting S+需设置FLAG_IMMUTABLE/MUTABLE求助
Android 12构建错误:Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE
已尝试操作
- 在Activity标签中添加
android:exported="true":<activity android:exported="true"...>...</activity> - 在
android/app/build.gradle中添加WorkManager依赖:def work_version = "2.7.0" // (Java only) implementation "androidx.work:work-runtime:$work_version"
当前依赖配置(package.json)
"dependencies": { "react": "16.9.0", "react-native": "0.61.3", "react-native-firebase": "^5.5.6", "react-native-fs": "^2.14.1", "react-native-push-notification": "^8.1.1", }
错误截图

解决方案
1. 升级react-native-push-notification
你当前使用的v8.1.1版本未完全适配Android 12的PendingIntent标记要求,升级到v8.1.3及以上版本(优先选择最新稳定版),新版本已内置处理FLAG_IMMUTABLE/FLAG_MUTABLE的逻辑。
2. 引入AndroidX Core兼容库
如果暂时无法升级第三方库,可在android/app/build.gradle中添加高版本的AndroidX Core依赖,它会提供兼容的PendingIntent创建工具类:
implementation 'androidx.core:core:1.7.0+'
3. 处理旧版react-native-firebase的适配
你使用的v5.5.6版本过于老旧,完全没有适配Android 12。建议:
- 若项目允许,升级到v14及以上的适配版本;
- 若无法升级,需手动修改Firebase相关代码中创建PendingIntent的部分,添加
FLAG_IMMUTABLE标记(需深入修改原生模块代码)。
4. 完善AndroidManifest配置
确保所有与通知相关的BroadcastReceiver都添加android:exported属性:
<receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationActions" android:exported="true" /> <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationPublisher" android:exported="true" /> <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationBootEventReceiver" android:exported="true"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> <action android:name="android.intent.action.QUICKBOOT_POWERON" /> <action android:name="com.htc.intent.action.QUICKBOOT_POWERON" /> </intent-filter> </receiver>
5. 临时降级targetSdkVersion(不推荐长期使用)
若以上方案暂时无法实施,可临时将android/app/build.gradle中的targetSdkVersion降至30:
android { defaultConfig { targetSdkVersion 30 } }
内容的提问来源于stack exchange,提问作者Germano
相关产品推荐
相关产品推荐

