Javascript/reactnative/admob/react-native-cli/react-native-firebase
提供: 初心者エンジニアの簡易メモ
react-native-firebaseインストール
yarn add @react-native-firebase/app yarn add @react-native-firebase/admob
インストール確認
package.jsonにreact-native-firebaseとadmobが入ってるか確認。
"dependencies": {
"@react-native-firebase/admob": "^7.5.1",
"@react-native-firebase/app": "^8.4.5",
"react": "16.13.1",
"react-native": "0.63.2"
},
AppId設定
firebase.json
{
"react-native": {
"admob_android_app_id": "ca-app-pub-3940256099942544~3347511713",
"admob_ios_app_id": "ca-app-pub-3940256099942544~1458002511"
}
}
上記はadmobが公開してるサンプルAppId
Androidでの追加設定
- google-services.jsonをandroid/appの下に置く
andorid/build.gradle
buildscript {
ext {
buildToolsVersion = "29.0.2"
minSdkVersion = 16
compileSdkVersion = 29
targetSdkVersion = 29
}
repositories {
google()
jcenter()
}
dependencies {
classpath("com.android.tools.build:gradle:3.5.3")
classpath 'com.google.gms:google-services:4.2.0' # add
}
}
android/app/build.gradle
apply plugin: 'com.google.gms.google-services'
iOSの追加設定
- GoogleService-Info.plistをxcodeプロジェクトの直下に置く
ios/AdMobSample/AppDelegate.m
#import <Firebase.h>
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if ([FIRApp defaultApp] == nil) {
[FIRApp configure];
}
}
サンプル
App.js
import {
AdEventType,
BannerAd,
BannerAdSize,
InterstitialAd,
RewardedAd,
RewardedAdEventType,
} from '@react-native-firebase/admob';
起動
android
$ react-native run-android
androidでfind your app IDが出る時
adb logcatで、以下エラーが出る時
$ adb logcat > * Invalid application ID. Follow instructions here: * > * https://googlemobileadssdk.page.link/admob-android-update-manifest * > * to find your app ID.
以下辺りに、admobのAppIdが有るはずだが、何かしらのエラーで、無い可能性が有るので見てみる。
android//app/build/intermediates/bundle_manifest/debug/processDebugManifest/bundle-manifest/AndroidManifest.xml android//app/build/intermediates/merged_manifests/debug/AndroidManifest.xml android//app/build/intermediates/instant_app_manifest/debug/AndroidManifest.xml android//app/build/intermediates/manifest_merge_blame_file/debug/manifest-merger-blame-debug-report.txt
Dexエラーの対応
以下エラーが出る時
Execution failed for task ':app:mergeDexDebug'. > A failure occurred while executing com.android.build.gradle.internal.tasks.Workers$ActionFacade > java.lang.NullPointerException
android/app/build.gradleに以下があれば一旦、削除して確認する
implementation 'androidx.multidex:multidex:2.0.1'
Gradle 7.0.に更新してほしいと出た時
Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
gradle/wrapper/gradle-wrapper.properties
distributionUrl=https\://services.gradle.org/distributions/gradle-6.4.1-all.zip
Use '--warning-mode all'とでたら
以下を実行し、色々buildする
gradle --warning-mode all
Java ヒープ・スペースと出る場合
以下エラーが出る場合
Execution failed for task ':app:transformClassesWithDexBuilderForDebug'. > Java ヒープ・スペース
以下実行すると直った
gradle --warning-mode all
どうしてもビルドできないときは
reactnativeのdeamonを一度切って、 一旦buildとかを削ったりして再度buildする
rm -fr android/app/build rm -fr .gradle/ rm -fr android/.gradle/ rm -fr android/app/build/ gradle --warning-mode all react-native run-android gradle --warning-mode all react-native run-android
それでもできないときは
nodeプラグインを一旦消して、上のbuildの再インストールをして、nodeを再インストール
rm -fr node_modules/ // ここで上のbuildの再インストールをして・・。 npm install
iOS
$ cd ios/ && pod install $ react-native run-ios
参考
https://rnfirebase.io/admob/usage
https://dev-yakuza.github.io/react-native/react-native-firebase-admob/
