添加react-native-google-mobile-ads后Expo iOS构建失败,Android正常
Expo项目集成react-native-google-mobile-ads后iOS构建失败
问题详情
Expo项目添加react-native-google-mobile-ads依赖后,Android端可正常构建,但iOS端构建失败。核心报错是RNGoogleMobileAdsModule.mm文件中存在BOOL到NSNumber*的类型转换不兼容问题,同时伴随多个脚本构建阶段警告,已按官方文档配置但无效。
项目配置(app.config.json)
export default { name: "Tarrhoon", slug: "social_assistance", version: "1.3.0", orientation: "portrait", icon: "./assets/appicon.png", splash: { image: "./assets/splash.png", resizeMode: "cover", }, plugins: [ [ "expo-notifications", { icon: "./assets/splashlogo.png", color: "#ffffff", }, ], [ "expo-media-library", { photosPermission: "Allow Social Assistance to access your photos.", savePhotosPermission: "Allow Social Assistance to save photos.", isAccessMediaLocationEnabled: true, }, ], [ "expo-image-picker", { photosPermission: "The app accesses your photos to let you share them With the Admin", }, ], [ "expo-av", { microphonePermission: "Allow App to access your microphone.", }, ], ], android: { versionCode: 3, icon: "./assets/simpleicon.png", googleServicesFile: process.env.GOOGLE_SERVICES_JSON, adaptiveIcon: { foregroundImage: "./assets/appicon.png", backgroundColor: "#7369ef", }, package: "com.muhammad1700.social_assistance", permissions: [ "android.permission.READ_EXTERNAL_STORAGE", "android.permission.WRITE_EXTERNAL_STORAGE", "android.permission.ACCESS_MEDIA_LOCATION", "android.permission.RECORD_AUDIO", "android.permission.MODIFY_AUDIO_SETTINGS", ], }, ios: { icon: "./assets/simpleicon.png", supportsTablet: true, infoPlist: { NSPhotoLibraryUsageDescription: "Allow Social Assistance to access your photos.", NSPhotoLibraryAddUsageDescription: "Allow Social Assistance to save photos.", }, bundleIdentifier: "com.muhammad1700.social-assistance", }, extra: { eas: { projectId: process.env.PROJECT_ID, }, }, "react-native-google-mobile-ads": { "android_app_id": process.env.ANDROID_APPID, "ios_app_id": process.env.IOS_APPID }, };
核心构建错误
类型转换编译错误
// 错误1:第120行 GADMobileAds.sharedInstance.requestConfiguration.tagForChildDirectedTreatment = tag; // 提示:incompatible integer to pointer conversion assigning to 'NSNumber * _Nullable' from 'BOOL' // 错误2:第125行 GADMobileAds.sharedInstance.requestConfiguration.tagForUnderAgeOfConsent = tag; // 提示:incompatible integer to pointer conversion assigning to 'NSNumber * _Nullable' from 'BOOL'
脚本构建警告(非致命,可暂时忽略)
[CP-User] Generate app.config for prebuilt Constants.manifest脚本将在每次构建时运行Start Packager脚本将在每次构建时运行Bundle React Native code and images脚本将在每次构建时运行[CP-User] [RNGoogleMobileAds] Configuration脚本将在每次构建时运行
构建最终结果
▸ ARCHIVE FAILED ▸ 编译RNGoogleMobileAdsModule.mm失败 ▸ Exit status: 65
解决方案
1. 修复类型转换错误(核心)
该问题是react-native-google-mobile-ads的iOS原生代码未正确将BOOL包装为NSNumber导致的,可通过patch-package打补丁解决:
- 打开
node_modules/react-native-google-mobile-ads/ios/RNGoogleMobileAds/RNGoogleMobileAdsModule.mm - 修改第120行和第125行代码:
// 第120行修改为 GADMobileAds.sharedInstance.requestConfiguration.tagForChildDirectedTreatment = @(tag); // 第125行修改为 GADMobileAds.sharedInstance.requestConfiguration.tagForUnderAgeOfConsent = @(tag); - 执行命令生成补丁:
npx patch-package react-native-google-mobile-ads - 将生成的
patches/react-native-google-mobile-ads+[版本号].patch文件提交到版本控制,确保CI构建时自动应用补丁
2. 清理构建缓存
删除以下文件/文件夹后重新构建:
node_modulesios/Podfile.lockios/Pods- 执行
eas build --clean(使用EAS构建时)或npx expo prebuild --clean(裸工作流)
3. 版本兼容性检查
确认react-native-google-mobile-ads版本与当前Expo/React Native版本兼容,若补丁无效,尝试降级/升级到稳定兼容版本(查看库的GitHub Releases说明)
4. 脚本警告处理(可选)
若需要消除警告,可在Xcode中打开项目,找到对应脚本构建阶段,取消勾选Based on dependency analysis,或添加输出依赖项,但此步骤不影响构建成功,可延后处理
内容的提问来源于stack exchange,提问作者Nouman Khan
相关产品推荐
相关产品推荐

