In Android Studio choose New Project → Empty Views Activity, with Language Java and Minimum SDK API 24.
2. How AdMob IDs work
AdMob uses two IDs: an app ID (~ in the middle) for your whole app, and an ad unit ID (/ in the middle) for each ad spot. Google publishes test IDs that always show safe test ads. Use them while you build.
3. Keep your IDs out of the code
In app/build.gradle.kts, add the import, the localProperties loader, the two build types, buildConfig = true, and the dependency, exactly as shown in Required setup. The dependency line is:
Press Sync Now. Debug builds now get Google's test IDs, and release builds read yours from local.properties, which is never committed. Your code reads the ad unit ID as BuildConfig.BANNER_AD_UNIT_ID.
4. Add the app ID to the manifest
Inside <application> in AndroidManifest.xml, add the APPLICATION_ID meta-data with the value ${ADMOB_APP_ID}. Gradle fills it in. Without an app ID, the app crashes on launch. The SDK adds the internet permission for you.
5. Make room for the banner
In activity_main.xml, put your content in a ScrollView with layout_weight="1", and an empty FrameLayout (adContainer) under it. The banner is added there in code.
6. Start the SDK
Call MobileAds.initialize once, on a background thread, so the app opens quickly.
7. Create an adaptive banner
Create an AdView, set the ad unit ID, and give it AdSize.getLargeAnchoredAdaptiveBannerAdSize(this, widthDp). Adaptive banners fill the screen width and AdMob picks the best height for each phone. Add it to the container and call loadAd.
8. Respect the lifecycle
Call banner.pause() in onPause, resume() in onResume, and destroy() in onDestroy, so the ad stops working when it can't be seen.
9. Run it
A banner labelled Test Ad appears at the bottom.
Publish with your own ads
In the AdMob console, add your app and create an ad unit.
Build a release version. Keep testing with debug builds: tapping your own live ads can get your AdMob account suspended.
What's improved over the video
The current SDK (25.5.0), adaptive banners that fit every phone, the SDK started off the main thread, the lifecycle handled, and your real IDs kept out of the code.
Files affected
Check what to add and what to replace before copying code.
Add each part where its comment says, then press Sync Now.
app/build.gradle.kts
import java.util.Properties
// At the very top of app/build.gradle.kts, after the importsval localProperties =Properties().apply{
rootProject.file("local.properties").takeIf{ it.exists()}?.inputStream()?.use{load(it)}}// Inside android { ... }
buildTypes {// Debug builds always show Google's test ads, so you can never click your own real ads by mistake.
debug {
manifestPlaceholders["ADMOB_APP_ID"]="ca-app-pub-3940256099942544~3347511713"buildConfigField("String","BANNER_AD_UNIT_ID","\"ca-app-pub-3940256099942544/9214589741\"")}// Release builds use your own IDs from local.properties.
release {
manifestPlaceholders["ADMOB_APP_ID"]= localProperties.getProperty("ADMOB_APP_ID","")buildConfigField("String","BANNER_AD_UNIT_ID","\"${localProperties.getProperty("ADMOB_BANNER_ID","")}\"")}}
buildFeatures {
buildConfig =true}// Inside dependencies { ... }implementation("com.google.android.gms:play-services-ads:25.5.0")
Your real IDs (for release)
Add these lines
Where: local.properties (project folder)
Never committed, so your IDs stay out of your code and off GitHub.
local.properties (project folder)
# Only needed when you publish. Debug builds use test ads.
ADMOB_APP_ID=ca-app-pub-xxxxxxxxxxxxxxxx~xxxxxxxxxx
ADMOB_BANNER_ID=ca-app-pub-xxxxxxxxxxxxxxxx/xxxxxxxxxx
Application code
Select a file to see its code and where it belongs.
This code uses package com.example.admobbanner. Change that first line to your own package, the one at the top of your MainActivity.
Starts the SDK, creates an adaptive banner and loads it; pauses and frees it with the screen.
MainActivity.java
packagecom.example.admobbanner;importandroid.os.Bundle;importandroid.util.DisplayMetrics;importandroid.widget.FrameLayout;importandroidx.appcompat.app.AppCompatActivity;importcom.google.android.gms.ads.AdRequest;importcom.google.android.gms.ads.AdSize;importcom.google.android.gms.ads.AdView;importcom.google.android.gms.ads.MobileAds;/**
* A screen with an adaptive AdMob banner pinned to the bottom.
* Adaptive banners pick the best height for each phone's width, so they look right everywhere.
*/publicclassMainActivityextendsAppCompatActivity{privateAdView banner;@OverrideprotectedvoidonCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);Screens.setUp(this,R.layout.activity_main);// Start the Mobile Ads SDK once, off the main thread, so the app opens quickly.newThread(()->MobileAds.initialize(this, status ->{})).start();
banner =newAdView(this);// The ad unit ID comes from app/build.gradle.kts: a test ID while you develop, yours in release.
banner.setAdUnitId(BuildConfig.BANNER_AD_UNIT_ID);
banner.setAdSize(adaptiveSize());FrameLayout container =findViewById(R.id.adContainer);
container.addView(banner);
banner.loadAd(newAdRequest.Builder().build());}/** The banner fills the screen width; AdMob chooses the height. */privateAdSizeadaptiveSize(){DisplayMetrics metrics =getResources().getDisplayMetrics();int widthDp =(int)(metrics.widthPixels / metrics.density);returnAdSize.getLargeAnchoredAdaptiveBannerAdSize(this, widthDp);}// Pause the banner when the screen is hidden, and free it when the screen closes.@OverrideprotectedvoidonPause(){
banner.pause();super.onPause();}@OverrideprotectedvoidonResume(){super.onResume();
banner.resume();}@OverrideprotectedvoidonDestroy(){
banner.destroy();super.onDestroy();}}