Page Summary
-
Ad preloading is an SDK-managed process for loading and caching ads, eliminating the need for manual ad loading.
-
This feature is currently available for interstitial, rewarded, and app open ad formats.
-
To use ad preloading, you need Google Mobile Ads SDK version 24.4.0 or higher and must complete the get started guide.
-
You start preloading by calling
start()with a preload configuration, and you can stop it withdestroy()ordestroyAll(). -
Preloaded ads are available to show after
onAdPreloaded()is called, and you can get the next available ad withpollAd().
Ad preloading is a Google-managed ad loading feature in Google Mobile Ads SDK that manages ad loading and caching on your behalf. Ad preloading requires a change in how you manage ad loading. To optimize performance using ad preloading, disable custom caching and delegate that responsibility to Google Mobile Ads SDK.
Ad preloading offers the following benefits over manual ad loading:
- Reference management: holds loaded ads so you don't have to maintain references until you're ready to show them.
- Automatic reloading: automatically loads a new ad when you pull one out of the cache.
- Managed retries: automatically retries failed requests using exponential backoff.
- Expiration handling: automatically refreshes ads before they expire (typically after one hour).
- Cache optimization: If you use a cache size larger than one, Google Mobile Ads SDK optimizes the cache order to deliver the best ad.
This guide covers configuring preload ads, checking preload ad availability, and showing the preloaded ad.
Prerequisites
Before you proceed with the tutorial, you need to complete the following items:
- Install Google Mobile Ads SDK version 24.4.0 or higher. Developer resources for earlier versions are available at 23.6.1 - 24.3.0, but we recommend using 24.4.0 or higher and following this guide going forward.
- Set up Google Mobile Ads SDK.
Start preloading ads
To begin preloading ads, call start(). Call this method
only once at the start of the app. After you call
start(), Google Mobile Ads SDK automatically
preloads ads and retries failed requests for preloaded configurations.
The following example starts preloading ads:
Kotlin
Java
Replace AD_UNIT_ID with your ad unit ID.
Get and show the preloaded ad
When using ad preloading, Google Mobile Ads SDK holds cached ads.
When you want to show an ad, call pollAd().
Google Mobile Ads SDK retrieves the available ad and automatically preloads
the next ad in the background.
Avoid calling this method until you are ready to show an ad. Keeping ads in the cache lets Google Mobile Ads SDK automatically refresh expired ads and perform cache optimization.
The following example retrieves and shows a preloaded ad:
Kotlin
Java
Check preloading ad availability
To check for ad availability, choose one of the following:
Get preloaded ad availability
The following example checks for ad availability:
Kotlin
Java
Listen to preloaded ad availability
Register for preload events to get notified when ads are preloaded successfully, fail to preload, or the ad cache is exhausted.
Preload events are intended for analytics purposes. Within preload event callbacks:
- Don't call
start(). - Avoid calling
pollAd()unless the ad will be shown immediately.
The following example registers for ad events:
Kotlin
Java
Stop preloading ads
If you don't need to show ads for a preload ID again in
the session, you can stop preloading ads. To stop
preloading ads for a specific preload ID, call destroy() with
a preload ID.
Kotlin
Java
Set the buffer size
Buffer size controls the number of preloaded ads held in memory. By default, Google optimizes buffer size to balance memory consumption and ad serving latency. If your app displays ads before the next ad is loaded, you can set a custom buffer size to increase the number of ads kept in memory. We recommend a buffer size of at most four.