React Native 0.71.3升级AdMob SDK至20.0.0+后崩溃问题求助
React Native AdMob 升级崩溃问题解决及替代方案
一、崩溃原因
你使用的react-native-admob@2.0.0-beta.6是已废弃版本,其原生代码仅适配旧版Google Mobile Ads SDK(16.x),直接升级play-services-ads到20.0.0后,新旧SDK的API不兼容,导致应用崩溃。
二、临时修复方案(不更换依赖包)
如果暂时不想换包,可以尝试以下步骤:
- 强制统一依赖版本:在项目根目录的
build.gradle中添加版本强制配置,避免依赖冲突:allprojects { configurations.all { resolutionStrategy.force "com.google.android.gms:play-services-ads:20.0.0" resolutionStrategy.force "com.google.android.gms:play-services-basement:18.1.0" resolutionStrategy.force "com.google.android.gms:play-services-measurement-base:21.2.0" } } - 清理缓存并重建:执行
./gradlew clean清空gradle缓存,然后重新运行项目。 - 注意:这种方法只是临时 workaround,原包已停止维护,后续还可能出现其他兼容问题,不建议长期使用。
三、推荐替代包(长期解决方案)
由于react-native-admob已废弃,推荐使用以下维护活跃的替代库:
1. react-native-google-mobile-ads
这是官方推荐的AdMob集成库,支持最新的Google Mobile Ads SDK,完美适配React Native 0.70+版本。
- 替换步骤:
- 卸载旧包:
npm uninstall react-native-admob - 安装新包:
npm install @react-native-google-mobile-ads/google-mobile-ads - 代码替换示例:
import { BannerAd, BannerAdSize } from '@react-native-google-mobile-ads/google-mobile-ads'; import { View } from 'react-native'; const Footer = () => ( <View style={{alignItems: 'center', width: '100%'}}> <BannerAd unitId="ca-app-pub-6xxxxx4xx/7xxxxxx9" size={BannerAdSize.FULL_BANNER} style={{width: '100%'}} onAdFailedToLoad={(error) => console.error(error)} /> </View> );
- 卸载旧包:
2. react-native-admob-native-ads
专注于原生广告的集成,同样支持最新AdMob SDK,适合需要自定义原生广告样式的场景,适配RN 0.70+版本。
内容的提问来源于stack exchange,提问作者gamerdev
相关产品推荐
相关产品推荐

