React Native+Expo构建应用遇S+版本FLAG_IMMUTABLE/MUTABLE错误求助
解决Targeting S+ (31+)需指定FLAG_IMMUTABLE/MUTABLE的错误
针对React Native + Expo + gradlew构建的应用出现的该错误,以下是适配Expo场景的针对性解决方案:
强制升级androidx.core依赖
多数情况下错误源于依赖的第三方库使用了旧版本的androidx.core(低于1.7.0),该版本未适配SDK31+的PendingIntent flag要求。- 若为已eject/prebuild的项目,在
app/build.gradle的dependencies块中添加强制依赖:implementation("androidx.core:core:1.10.1") { force = true } - 若为未eject的Expo项目,使用
expo-build-properties插件统一配置:- 安装插件:
npx expo install expo-build-properties - 在
app.json或app.config.js中添加配置:"plugins": [ [ "expo-build-properties", { "android": { "dependencies": [ "androidx.core:core:1.10.1" ] } } ] ]
- 安装插件:
- 若为已eject/prebuild的项目,在
排查并升级第三方风险库
推送、定时任务类库是高发源头,需确认版本适配SDK31+:- Firebase Messaging:升级至23.0.0及以上版本
- react-native-push-notification:升级至8.1.1及以上,确保配置中指定
channelId并在AndroidManifest中完成渠道注册
可通过./gradlew app:dependencies(Windows用gradlew.bat app:dependencies)查看依赖树,定位使用旧PendingIntent的库。
修正自定义PendingIntent代码
若项目中有手动创建PendingIntent的原生代码,需明确指定flag:// 无需动态更新Intent内容时使用(推荐) PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE); // 需要动态更新Intent内容时使用 PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_MUTABLE);
内容的提问来源于stack exchange,提问作者Armando Neto
相关产品推荐
相关产品推荐

