Admob

AdMob #

https://stackoverflow.com/questions/68855709/how-to-show-a-banner-ad-in-jetpack-compose

@Composablefun AdvertView(modifier: Modifier = Modifier) {
    val isInEditMode = LocalInspectionMode.current
    if (isInEditMode) {
        Text(modifier = modifier.fillMaxWidth().background(Color.Red).padding(horizontal = 2.dp, vertical = 6.dp),
        textAlign = TextAlign.Center,
        color = Color.White,
        text = "Advert Here",
        )
    } else {
        AndroidView(modifier = modifier.fillMaxWidth(),
            factory = { context -> AdView(context).apply {                    adSize = AdSize.BANNER
            adUnitId = context.getString(R.string.ad_id_banner)
            loadAd(AdRequest.Builder().build())
            }
            })
    }
}

@Preview(showBackground = true)
@Composablefun AdvertPreview() {
    AdvertView()
}

Quick Start #

https://developers.google.com/admob/android/quick-start

buildscript {
   repositories {
       google()
       mavenCentral()
   }
}

allprojects {
   repositories {
       google()
       mavenCentral()
   }
}
dependencies {
  implementation 'com.google.android.gms:play-services-ads:20.5.0'
}
<manifest>
    <application>
        <!-- Sample AdMob app ID: ca-app-pub-3940256099942544~3347511713 -->
        <meta-data
            android:name="com.google.android.gms.ads.APPLICATION_ID"
            android:value="ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy"/>
    </application>
</manifest>
import com.google.android.gms.ads.MobileAds

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        MobileAds.initialize(this) {}
    }
}

バナータイプの広告 #

https://developers.google.com/admob/android/banner

Example #

https://github.com/googleads/googleads-mobile-android-examples/tree/master/kotlin/admob/BannerExample

テスト広告 #

https://developers.google.com/admob/android/test-ads

テストデバイスの追加方法 #

app-ads.txtのBloggerへの登録 #