iOS端React Native项目安装react-native-Admob后Import 'GoogleMobileAds' not found报错如何解决
React Native iOS 运行报
Import 'GoogleMobileAds' not found 解决方案 1. 修正依赖包(优先检查)
目前react-native-admob已经停止维护,Google官方推出的替代包为react-native-google-mobile-ads,旧包本身就存在iOS依赖缺失的兼容问题,优先替换依赖:
- 卸载旧包:
# npm用户执行 npm uninstall react-native-admob # yarn用户执行 yarn remove react-native-admob
- 安装官方新包:
# npm用户执行 npm install react-native-google-mobile-ads # yarn用户执行 yarn add react-native-google-mobile-ads
2. 重新安装Pod依赖
进入项目ios目录执行以下命令清理旧Pod缓存并拉取最新依赖:
pod deintegrate rm Podfile.lock pod install --repo-update
3. 校验iOS项目配置
打开Xcode加载项目下的.xcworkspace文件,进行以下检查:
- 进入项目Target的 Build Settings 页,搜索
Minimum Deployments,确认iOS最低版本不低于10.0,低于该版本GoogleMobileAds SDK将无法适配 - 同样在Build Settings页搜索
Framework Search Paths,确认列表中存在${PODS_ROOT}/Google-Mobile-Ads-SDK/Frameworks,如果不存在手动添加,后面的权限选项设置为Recursive
4. 全量清理缓存后重编译
执行以下命令清理所有缓存后重新编译:
# 清理Xcode缓存 rm -rf ~/Library/Developer/Xcode/DerivedData # 清理RN构建缓存 npx react-native start --reset-cache # 重新运行iOS项目 npx react-native run-ios
特殊情况修复
如果以上步骤执行完成仍报错,在ios目录的Podfile末尾添加以下配置后重新执行pod install即可:
post_install do |installer| installer.pods_project.targets.each do |target| if target.name == 'Google-Mobile-Ads-SDK' target.build_configurations.each do |config| config.build_settings['BUILD_LIBRARY_FOR_DISTRIBUTION'] = 'YES' end end end end
内容的提问来源于stack exchange,提问作者Muhammad Haidar
相关产品推荐
相关产品推荐

