AdMob広告設置の為の作業
0.strings.xml
本番用アプリIDはAdMobにログインして確認
本番用広告ユニットIDは作成する必要が有る
<resources> <!-- 本番用--> <!-- <string name="admobid">ca-app-pub-1234567890123456~1234567890</string>--> <!-- <string name="adunitid">ca-app-pub-1234567890123456~1234567890</string>--> <!-- テスト用--> <string name="admobid">ca-app-pub-3940256099942544~3347511713</string> <string name="adunitid">ca-app-pub-3940256099942544/2934735716</string> </resources>
=テスト用アプリID=
アプリID:ca-app-pub-3940256099942544~3347511713
=テスト用デモ広告ユニットID=
アプリ起動:ca-app-pub-3940256099942544/5662855259
バナー:ca-app-pub-3940256099942544/2934735716
インタースティシャル:ca-app-pub-3940256099942544/4411468910
インタースティシャル動画:ca-app-pub-3940256099942544/5135589807
リワード:ca-app-pub-3940256099942544/1712485313
リワード インタースティシャル:ca-app-pub-3940256099942544/6978759866
ネイティブ アドバンス:ca-app-pub-3940256099942544/3986624511
ネイティブ アドバンス動画:ca-app-pub-3940256099942544/2521693316
1.AndroidManifest.xml
meta-data の設置
<application ......... ......... <meta-data android:name="com.google.android.gms.ads.APPLICATION_ID" android:value="@string/admobid"/> <activity ............................ <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>
2.activity_main.xml
layout/activity_main.xml 内に com.google.android.gms.ads.AdView の設置
<com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto" android:id="@+id/adView" android:layout_width="wrap_content" android:layout_height="wrap_content" ads:adSize="BANNER" ads:adUnitId="@string/adunitid"> </com.google.android.gms.ads.AdView>
3.build.gradle (モジュール)
build.gradle 内の dependencies に
implementation ‘com.google.android.gms:play-services-ads:20.6.0’
を追加⇨「Sync Now」で同期します。
dependencies {
implementation 'com.google.android.gms:play-services-ads:20.6.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.6.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
4.MainActivity
MainActivity の onCreate 内で初期化
//AdMob/////////////////////////////////////////////////////////////////////////////// MobileAds.initialize(this, new OnInitializationCompleteListener() { @Override public void onInitializationComplete(InitializationStatus initializationStatus) { } }); AdView mAdView = findViewById(R.id.adView); AdRequest adRequest = new AdRequest.Builder().build(); mAdView.loadAd(adRequest); mAdView.setAdListener(new AdListener() { @Override public void onAdLoaded() { //Log.d("debug", " :onAdLoaded: " ); // Code to be executed when an ad finishes loading. } @Override public void onAdFailedToLoad(@NonNull LoadAdError adError) { //Log.d("debug", " :onAdFailedToLoad: " + adError); // Code to be executed when an ad request fails. } @Override public void onAdOpened() { //Log.d("debug", " :onAdOpened: "); // Code to be executed when an ad opens an overlay that // covers the screen. } @Override public void onAdClicked() { //Log.d("debug", " :onAdClicked: "); // Code to be executed when the user clicks on an ad. } @Override public void onAdClosed() { //Log.d("debug", " :onAdClosed: "); // Code to be executed when the user is about to return // to the app after tapping on an ad. } }); //AdMob///////////////////////////////////////////////////////////////////////////////
app-ads.txt の設置
デベロッパーサイトを開設してルートディレクトリにapp-ads.txtを設置
https://<<hostname>>/app-ads.txt
“https://help.example.com/app-ads.txt”
“https://example.com/app-ads.txt”
google.com, pub-1234567890123456, DIRECT, a01b34cde5678fg9
app-ads.txt(文字コードutf-8)ファイルを作成
スニペットを貼り付け、
デベロッパーサイトを開設してルートディレクトリに保存して下さい。
まとめ
AdMob設定方法をまとめて置きます参考にして下さい。