Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To integrate AdMob in a LibGDX game, keep Google’s Mobile Ads SDK and all Android ad views in the Android module. Use initializeForView() to place the LibGDX game view and an AdMob banner in the same Android layout, and expose ad actions to the cross-platform core module through a small interface. This guide builds that Android-only foundation with Google test ads, then covers interstitials, rewarded ads, consent, and release checks.

What this integration covers

This implementation is for Android. The SDK dependency, manifest entry, ad views, and ad callbacks belong in the Android launcher/module. Keep Android and Google Mobile Ads imports out of core, so desktop and other targets can still compile. iOS needs a separate native ad implementation.

A typical project boundary looks like this:

core/       AdsController interface and game logic
android/    Google Mobile Ads SDK, AndroidLauncher, ad views
lwjgl3/     No-op AdsController implementation

Most projects use an AndroidApplication launcher. LibGDX also supports hosting the game in an AndroidFragmentApplication when the game is part of a larger native Android screen. See LibGDX starter classes and configuration.

Create the AdMob app and ad units

In AdMob, register the Android app and create a separate ad unit for each placement and format you intend to use. Keep these identifiers distinct:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • App ID: Identifies your app to the Mobile Ads SDK. It belongs in Android manifest metadata and typically contains a tilde (~).
  • Ad unit ID: Identifies a particular banner, interstitial, or rewarded placement. Pass it to that format’s load method; it typically contains a slash (/).

Do not put an app ID where an ad unit ID is expected. For development, use Google’s test ad unit IDs rather than your live placements.

Add the Google Mobile Ads SDK to the Android module

Follow Google’s Android quick start for the current Gradle dependency and repository setup. Add the dependency to the Android application module—not core. Depending on the age and layout of your LibGDX project, the relevant file may be android/build.gradle, android/app/build.gradle, or another Android-module Gradle file.

The dependency has this general form; use the version currently specified by Google rather than pinning a number copied from an older tutorial:

dependencies {
    implementation("com.google.android.gms:play-services-ads:<current-version>")
}

Sync Gradle and build the Android target before adding game code. The official quick start also specifies the required repositories and app setup; follow it if your template does not already include them.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Declare the app ID in the Android manifest

Add the app ID as application metadata in AndroidManifest.xml. A string resource keeps the identifier out of the manifest itself:

<!-- android/src/main/res/values/strings.xml -->
<resources>
    <string name="admob_app_id">ca-app-pub-XXXXXXXXXXXXXXXX~YYYYYYYYYY</string>
</resources>
<!-- Inside the <application> element in AndroidManifest.xml -->
<meta-data
    android:name="com.google.android.gms.ads.APPLICATION_ID"
    android:value="@string/admob_app_id" />

Replace the example with the app ID from AdMob. This is not a banner or other ad unit ID. Google documents the manifest requirement in its Android quick start.

Put a test banner over the LibGDX game view

For an overlay, use initializeForView() rather than initialize(). The latter installs the LibGDX view for the activity; the former returns a view you can put inside a parent Android layout alongside the ad. The core idea is to add the game first and the banner second, so the banner sits above the game. LibGDX’s AdMob integration page illustrates this architecture, but its sample APIs are legacy; do not copy its old SDK setup as current code.

Here is the layout pattern in Java. It assumes admob_banner_id is a string resource containing Google’s banner test ID during development. Calculate the available width in density-independent pixels for your actual layout; 320 below is only a simple example, not a production width calculation.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
public class AndroidLauncher extends AndroidApplication {
    private AdView bannerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        AndroidApplicationConfiguration config =
                new AndroidApplicationConfiguration();
        config.useImmersiveMode = true;

        FrameLayout root = new FrameLayout(this);
        View gameView = initializeForView(new MyGdxGame(/* ads controller */), config);

        root.addView(gameView, new FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));

        bannerView = new AdView(this);
        bannerView.setAdSize(
                AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, 320));
        bannerView.setAdUnitId(getString(R.string.admob_banner_id));

        FrameLayout.LayoutParams bannerParams = new FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                Gravity.BOTTOM);
        root.addView(bannerView, bannerParams);

        setContentView(root);
        bannerView.loadAd(new AdRequest.Builder().build());
    }
}

Import the Android view classes and the Mobile Ads classes used in the snippet. Check Google’s banner documentation for the current adaptive-banner API and imports. A FrameLayout is a straightforward choice for stacking views; a RelativeLayout or another parent view group also works if your layout needs different positioning.

Google’s fixed-size banner test unit is ca-app-pub-3940256099942544/6300978111. Use that while developing, then confirm the banner displays with a test label. Google’s banner guidance is at Set up banner ads.

Let shared game code request ad actions

Define a platform-neutral contract in core. It should describe game actions, not expose AdView or other Android SDK types. For example:

public interface AdsController {
    void showBanner();
    void hideBanner();
    void showInterstitial();
    void showRewarded(Runnable onReward);
}

Pass the controller into the game when constructing it. The Android launcher can implement the interface; the desktop launcher can supply a no-op implementation:

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #2
public class MyGdxGame extends Game {
    private final AdsController ads;

    public MyGdxGame(AdsController ads) {
        this.ads = ads;
    }
}
// Desktop launcher
new MyGdxGame(new NoOpAdsController());

In the Android implementation, dispatch view changes to Android’s UI thread:

@Override
public void showBanner() {
    runOnUiThread(() -> bannerView.setVisibility(View.VISIBLE));
}

@Override
public void hideBanner() {
    runOnUiThread(() -> bannerView.setVisibility(View.GONE));
}

Hiding a banner is not the same as stopping its loading or refresh activity. Decide how the ad view should be managed when it is not needed, and avoid making requests just to keep an invisible banner alive. For many games, a banner makes more sense on menus or results screens than during active play.

Choose the format for the game moment

Different formats make different demands on the player and on your game logic:

  • Banner: Persistent and relatively simple, but it takes or overlays screen space. Avoid placing it near controls where it could attract accidental taps.
  • Interstitial: Full-screen. Use at a natural pause such as a completed level or round, not during active play or immediately at launch. It must be loaded asynchronously and may not be ready when requested. See Google’s interstitial guidance.
  • Rewarded: A player opts in to watch for an in-game benefit such as a retry or extra life. Grant the benefit only from the SDK’s earned-reward callback; load success, a button tap, or ad dismissal alone does not mean a reward was earned.
  • Rewarded interstitial: A distinct incentivized format designed to appear during transitions. Google describes clear reward messaging and a skip option for this format; it is not simply a drop-in replacement for standard opt-in rewarded ads. See Google’s rewarded interstitial documentation.
  • App open and native: Available alternatives, but app-open ads require a launch/resume design and native ads require more Android UI work. They are not necessary for a first integration.

A sensible starting design is banners on non-gameplay screens and optional rewarded ads for retries or bonuses. Consider interstitials only at explicit transitions and with a cadence that does not undermine play.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Load and show interstitials at explicit transitions

Load an interstitial ahead of the moment you may show it, keep the loaded object in a field, and show it only when the game reaches a deliberate transition such as levelCompleted. If it is unavailable, continue the game flow without blocking the player.

This representative structure follows the current callback pattern in Google’s interstitial guide. Add the relevant SDK imports and adapt lifecycle handling to your launcher:

private InterstitialAd interstitialAd;
private boolean loadingInterstitial;

private void loadInterstitial() {
    if (loadingInterstitial || interstitialAd != null) return;
    loadingInterstitial = true;

    InterstitialAd.load(
            this,
            getString(R.string.admob_interstitial_id),
            new AdRequest.Builder().build(),
            new InterstitialAdLoadCallback() {
                @Override
                public void onAdLoaded(@NonNull InterstitialAd ad) {
                    loadingInterstitial = false;
                    interstitialAd = ad;
                    ad.setFullScreenContentCallback(
                            new FullScreenContentCallback() {
                                @Override
                                public void onAdDismissedFullScreenContent() {
                                    interstitialAd = null;
                                    loadInterstitial();
                                }

                                @Override
                                public void onAdFailedToShowFullScreenContent(
                                        @NonNull AdError error) {
                                    interstitialAd = null;
                                    loadInterstitial();
                                }
                            });
                }

                @Override
                public void onAdFailedToLoad(@NonNull LoadAdError error) {
                    loadingInterstitial = false;
                    interstitialAd = null;
                }
            });
}

public void showInterstitial() {
    runOnUiThread(() -> {
        if (interstitialAd == null) {
            loadInterstitial();
            return;
        }
        interstitialAd.show(this);
        interstitialAd = null;
    });
}

Google’s interstitial test unit is ca-app-pub-3940256099942544/1033173712. Register the full-screen callback before showing. Treat loading, showing, dismissal, and show failure as separate states; the game should proceed normally if an ad cannot be shown.

Grant rewarded benefits only after the reward event

Keep the player’s reward decision in game logic, but let the Android implementation report whether the SDK awarded it. Load rewarded ads in advance, show one only after an explicit player choice, and invoke the game’s reward callback only from OnUserEarnedRewardListener. The current implementation patterns are in Google’s Mobile Ads Android examples.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #3
Sale
Beginning Android Games
  • Used Book in Good Condition

The lifecycle has distinct outcomes: an ad can fail to load, load successfully, fail to show, be shown and dismissed, or produce an earned reward. Dismissal is not proof of earning. If no ad is ready, tell the game that the option is unavailable or offer another route; do not leave the player waiting indefinitely. Clear the ad reference after display and load another for a future opportunity.

Make reward handling idempotent so duplicate callbacks or game events cannot grant the same benefit twice. If game state changes need to happen on LibGDX’s render thread, marshal them safely rather than mutating rendering state directly from an Android or SDK callback.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Handle consent and privacy before requesting ads

Adding the SDK is not a complete privacy or policy implementation. Depending on the user’s jurisdiction, age, data use, and applicable Google requirements, you may need to obtain or refresh consent and apply child-directed or under-age treatment settings before SDK initialization or ad requests. Google’s quick-start privacy guidance describes the ordering requirement for applicable settings.

For applicable regions—including the EEA, UK, and Switzerland—configure an appropriate consent flow, such as Google’s User Messaging Platform, and determine whether a request may be made before loading ads. Consider relevant U.S. state requirements as well. Keep your privacy policy and store disclosures accurate for the SDKs and data practices actually included in the app. The Google consent-management material provides background; check current Google and legal requirements for your release.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For a consent-sensitive app, do not blindly initialize Mobile Ads as the first operation in onCreate(). A safer high-level sequence is:

  1. Gather or refresh consent when required.
  2. Apply applicable child-directed, under-age, or other request settings.
  3. Initialize Mobile Ads once.
  4. Load ads only when the resulting settings and consent state permit requests.

Google recommends completing SDK initialization before loading ads. This also matters if you later add mediation: participating adapters need an opportunity to initialize. Mediation is optional and adds SDKs, adapters, privacy configuration, and additional testing work; it is not a guaranteed revenue increase. See Google’s mediation setup guidance.

Test safely, then prepare a release build

Google provides demo ad units for development. The IDs below are test placements, not production inventory:

Format Google test ad unit ID
Banner ca-app-pub-3940256099942544/6300978111
Interstitial ca-app-pub-3940256099942544/1033173712
Rewarded ca-app-pub-3940256099942544/5224354917
Rewarded interstitial ca-app-pub-3940256099942544/5354046379
Native ca-app-pub-3940256099942544/2247696110
Native video ca-app-pub-3940256099942544/1044960115

Google says Android emulators are automatically treated as test devices. Register physical test devices using the AdMob UI or the SDK’s test-device configuration. Do not click live ads while testing; Google warns that test activity on live inventory can be considered invalid and put an account at risk. For mediated ads, Google’s sample units show Google ads only; configure each mediation network’s own test mode, and do not rely on a Google “Test Ad” label for mediated inventory. Details: Google’s test ads guide.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • Replace every test ad unit ID with the correct production unit before release.
  • Use the real AdMob app ID in the manifest.
  • Remove temporary test-device configuration from production settings.
  • Test a signed release build and verify that banners do not obscure controls.
  • Confirm consent behavior and store disclosures for the actual release regions and audience.

Troubleshoot common LibGDX and AdMob problems

The banner loads but is invisible

  • Confirm the game view was added before the banner view, and that no later full-screen child covers it.
  • Check that you passed the parent layout to setContentView(), with valid banner layout parameters.
  • Verify the ID is a banner ad unit ID, not the app ID or a full-screen unit.
  • Check network access and inspect Logcat for the SDK’s load error code and message. No visible ad is not necessarily a view-layout bug.

initializeForView() is not found

Check that the Android launcher extends the expected LibGDX Android application class and that the project is using a compatible LibGDX Android backend. The overlay approach needs a returned game view; initialize() installs the game view directly rather than returning it for a custom parent layout.

Ads do not load on one device

Possible causes include no fill, network restrictions, incorrect IDs, consent preventing a request, mediation adapter setup, or device-specific lifecycle/rendering behavior. Read the load error rather than assuming the SDK is broken. While developing, use Google’s test units or a registered test device instead of live ads.

An interstitial appears at the wrong time

Trigger it from an explicit game event such as completion of a level, not a render callback or a generic screen transition. Check readiness first, and make the game’s resume path work whether the ad is dismissed, fails to show, or is unavailable.

The banner overlaps the HUD or the game is letterboxed

The Android view’s physical layout and LibGDX’s logical game coordinates are separate. Choose a layout intentionally: reserve space and resize the game view, overlay a banner only on a menu, or keep gameplay full-screen and show ads during transitions. Recalculate relevant layout and viewport dimensions when the available screen area changes. LibGDX explains viewport adaptation in its viewports guide.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The game freezes or crashes around an ad

Run Android view changes on the UI thread, and marshal game-state changes to the appropriate LibGDX thread. Do not assume an ad will always load, show, or dismiss normally; handle each callback path and keep gameplay recoverable.

Quick Recap

Bestseller No. 2
Android Game Programming For Dummies
Android Game Programming For Dummies
Used Book in Good Condition
$5.86
SaleBestseller No. 3
Beginning Android Games
Beginning Android Games
Used Book in Good Condition
$20.03

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.