AI Integration Agent Instructions for Rewarded Ads
Required Imports
Use the following imports to implement a rewarded ad:
import com.google.android.libraries.ads.mobile.sdk.common.AdRequest
import com.google.android.libraries.ads.mobile.sdk.common.AdLoadCallback
import com.google.android.libraries.ads.mobile.sdk.common.FullScreenContentError
import com.google.android.libraries.ads.mobile.sdk.common.LoadAdError
import com.google.android.libraries.ads.mobile.sdk.rewarded.OnUserEarnedRewardListener
import com.google.android.libraries.ads.mobile.sdk.rewarded.RewardedAd
import com.google.android.libraries.ads.mobile.sdk.rewarded.RewardedAdEventCallbackMethod Signatures
Use the following method signatures to implement rewarded ads:
The AdRequest object takes the ad unit ID:
AdRequest.Builder(adUnitId: String).build()The method signature to load an ad from RewardedAd:
fun load(
adRequest: AdRequest,
adLoadCallback: AdLoadCallback<RewardedAd>
): UnitThe AdLoadCallback interface is defined as:
interface AdLoadCallback<T> {
fun onAdLoaded(ad: T): Unit
fun onAdFailedToLoad(adError: LoadAdError): Unit
}Gotchas
UI Threading: MANDATORY: Callbacks in GMA-Next Gen SDK are invoked on a
background thread. ALL UI-RELATED OPERATIONS (e.g., Toasts, View updates,
Fragment transactions) MUST be wrapped in runOnUiThread {} or
Dispatchers.Main.launch {} within GMA SDK callbacks. SKIPPING THIS STEP WILL
CAUSE THE APPLICATION TO CRASH.
Rewarded ad workflow
Load the ad
Register for ad event callbacks
- Set the
adEventCallbackon theRewardedAdobject.- Drop the reference to the rewarded ad when the ad is dismissed or fails to show.
- Set the
Add a UI element to view the ad for a reward
- Rewarded ads must never be shown automatically. Users must explicitly opt in.
- Add a UI element, such as a button, that shows the ad upon user interaction.
Show the ad
- The
show()method requires anOnUserEarnedRewardListener.
- The
Verify the implementation: Verify the build to ensure there are no compile errors:
- Run
gradle build -x testto confirm a successful clean build. Resolve any GMA SDK related compile errors.
- Run
Links
Additional documentation: