android - 如何在 Expo 和 Expo Go 中使用 react-native-google-mobile-ads?

标签 android react-native expo admob

如何在 Expo 和 Expo Go 中使用 react-native-google-mobile-ads? (例如横幅广告)
非常感谢世博会背景下的最小示例。
截至 2022 年 5 月,所有在线 google 文档都引用了 expo-ads-admob,但 Expo 文档现在提到 expo-ads-admob 已弃用,并打算在 v46 中删除对该模块的支持。
Expo recommends using react-native-google-mobile-ads.当我将该模块集成到项目中时,当我通过 Expo Go(和 Metro)启动应用程序时会崩溃。我目前正在使用 Android 进行测试。

Invariant Violation: requireNativeComponent: "RNGoogleMobileAdsBannerView" was not found in the UIManager.                                                                                  
                                                                                              
This error is located at:                                                                     
    in RNGoogleMobileAdsBannerView (created by BaseAd)                                        
    in BaseAd (created by BannerAd)                                                           
    in BannerAd (created by App)                                                              
    in RCTView (created by View)                                                              
    in View (created by App)                                                                  
    in App (created by ExpoRoot)
    in ExpoRoot
    in RCTView (created by View)
    in View (created by AppContainer)
    in RCTView (created by View)
    in View (created by AppContainer)
    in AppContainer
    in main(RootComponent)
at node_modules/react-native/Libraries/Core/ExceptionsManager.js:95:4 in reportException
at node_modules/react-native/Libraries/Core/ExceptionsManager.js:141:19 in handleException
at node_modules/react-native/Libraries/Core/setUpErrorHandling.js:24:6 in handleError
at node_modules/expo-error-recovery/build/ErrorRecovery.fx.js:12:21 in ErrorUtils.setGlobalHandler$argument_0
我认为这个错误与链接有关,但我明确试图让它在 Expo 中工作,而不是纯粹的 react-native。
IMO,这个 react-native-google-mobile-ads 模块与 Expo Go 不兼容的原因也足以作为答案。
重申一下:如何在 Expo 和 Expo Go 中使用 react-native-google-mobile-ads?非常感谢世博会背景下的最小示例。

最佳答案

要将 react-native-google-mobile-ads 与 Expo 管理的工作流程一起使用,您需要使用 EAS 作为“开发客户端”来构建您的项目。开发客户端提供与 Expo Go 类似的功能和便利。主要区别在于您要么需要在本地设置有效的 EAS 构建环境,要么使用 Expo 提供的云构建。
假设您有本地 EAS build设置并希望为 android 构建:

# Install the ad module
expo install react-native-google-mobile-ads

# Install the dev-client module
expo install expo-dev-client

# Build the dev-client (as an APK)
eas build --platform android --profile development --local

# Note: Install the built APK into emulator or device.

# Start the expo service for the dev client
expo start --dev-client --lan
安装 APK 并设置服务后,您可以通过 Expo Go 扫描二维码加载应用程序。
注意:不要将 react-native-google-mobile-ads 配置放在 app.json 配置的 expo 部分。这个错误让我深入研究了 react-native-google-mobile-ads 的 build.gradle 代码。
// app.json
{
  "expo": {
  // ...
  },
  "react-native-google-mobile-ads": {
    "android_app_id": "ca-app-pub-xxxxxxxx~xxxxxxxx",
    "ios_app_id": "ca-app-pub-xxxxxxxx~xxxxxxxx"
  }
}

关于android - 如何在 Expo 和 Expo Go 中使用 react-native-google-mobile-ads?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72337988/

相关文章:

android - 如何修复记录为警告的 "android.enableSeparateAnnotationProcessing is experimental and unsupported"?

Android BLE Beacon 扫描

javascript - React Native 设置 TextInput 值大写

android - AdMob Interstitial 停止工作

reactjs - react native : Text wrapping inside View container with fixed-width button

android - 无法使用 Linking.openURL 打开电话

javascript - react native - 在 package.json 中使用别名进行绝对导入

Android 如何在调用 finish() 时保存和恢复 Activity 的状态?

android - Flutter应用程序未在我的oneplus 7中运行,即使其显示已连接,一切也都与开发者选项相关

react-native - React-Native Expo 的 Babel 配置