简体   繁体   English

如何将 react-native-google-mobile-ads 与 Expo 和 Expo Go 一起使用?

[英]How do I use react-native-google-mobile-ads with Expo and Expo Go?

How do I use react-native-google-mobile-ads with Expo and Expo Go?如何将 react-native-google-mobile-ads 与 Expo 和 Expo Go 一起使用? (eg Banner Ads) (例如横幅广告)

A minimal example within the context of Expo would be very much appreciated.非常感谢 Expo 上下文中的一个最小示例。

All the online google documentation as of May 2022 refers to expo-ads-admob, but Expo documentation now mentions that expo-ads-admob is deprecated and intends to remove support for the module in v46.截至 2022 年 5 月的所有在线谷歌文档都提到了 expo-ads-admob,但 Expo 文档现在提到 expo-ads-admob 已被弃用,并打算在 v46 中删除对该模块的支持。

Expo recommends using react-native-google-mobile-ads. Expo 建议使用 react-native-google-mobile-ads。 When I integrate that module into the project I get a crash when I start the app via Expo Go (and Metro).. I'm currently testing with Android.当我将该模块集成到项目中时,当我通过 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

I presume this error has to do with linking, but I am explicitly trying to get this to work within Expo and not pure react-native.我认为这个错误与链接有关,但我明确试图让它在 Expo 内工作,而不是纯粹的 react-native。

IMO, Reasons why this react-native-google-mobile-ads module is not compatible with Expo Go would suffice as an answer as well. IMO,此 react-native-google-mobile-ads 模块与 Expo Go 不兼容的原因也足以作为答案。

To re-iterate: How do I use react-native-google-mobile-ads with Expo and Expo Go?重申一下:如何将 react-native-google-mobile-ads 与 Expo 和 Expo Go 一起使用? A minimal example within the context of Expo would be very much appreciated.非常感谢 Expo 上下文中的一个最小示例。

To use react-native-google-mobile-ads with an Expo managed workflow, you'll need to build your project with EAS as a "dev-client".要将 react-native-google-mobile-ads 与 Expo 托管工作流一起使用,您需要使用 EAS 作为“开发客户端”构建您的项目。 The dev-client provides similar functionality and conveniences to Expo Go. The primary difference is that you'll either need to have a working EAS build environment setup locally or use the Expo provided cloud build. dev-client 提供与 Expo Go 类似的功能和便利。主要区别在于您需要在本地设置有效的 EAS 构建环境或使用 Expo 提供的云构建。

Assuming you have a local EAS build setup and would like to build for android:假设您有一个本地 EAS 构建设置并且想要为 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

Once the APK is install and the service is setup, you may load the app via Expo Go by scanning the QR code.安装 APK 并设置服务后,您可以通过扫描二维码通过 Expo Go 加载应用程序。

Note: Do not put the react-native-google-mobile-ads configuration in the expo section of the app.json configuration.注意:不要将 react-native-google-mobile-ads 配置放在 app.json 配置的 expo 部分。 This mistake had me digging deep into react-native-google-mobile-ads' build.gradle code.这个错误让我深入研究了 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"
  }
}

V46 of expo has some issues. expo 的 V46 有一些问题。 I used expo V45 and it works perfectly.我用的是 expo V45,效果很好。

You have to complete this step for expo您必须为 expo 完成此步骤

npx pod-install
npx react-native run-ios

# For Android
npx react-native run-android

# For expo users not using EAS
npx expo prebuild

# For expo users using EAS
npx eas-cli build --profile development

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Expo 客户端上打开 React Native 应用程序? - How do I open a React Native Application on Expo Client? 如何在 Expo React-Native 中使用下拉列表? - How can I use DropDown List in Expo React-Native? Expo React Native 应用程序在 Android 上崩溃,但在 Expo 上运行 Go - Expo React Native app crashes on Android but works in Expo Go 如何在 react native expo 项目中从一个屏幕转到另一个屏幕? - How to go from one screen to another in react native expo project? 我可以在 React Native 的 Expo 项目中使用 Zebra 扫描仪包吗? - Can I use a Zebra scanner package with Expo project in React Native? 我可以在反应原生裸项目中使用 expo facebook 广告 npm 库吗 - Can I Used expo facebook ads npm library in react native bare project Firebase React Native Expo 谷歌登录错误 - Firebase React Native Expo login error with google 如何修复每当我在 Expo react-native 中使用 KeyboardAwareView 或 react-native-keyboard-aware-scroll-view 时显示的白色键盘空间? - How do fix the white keyboard space that shows whenever I use KeyboardAwareView or react-native-keyboard-aware-scroll-view in my Expo react-native? 如何在 React Native(expo android)中将 output 纸张大小更改为 A4 - How Do I change output Paper Size to A4 in React Native (expo android) 如何使用Expo框架在React Native中测量文本 - How can I measure text in React Native using the Expo framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM