To discuss and provide feedback on our products, join the official AdMob Discord channel in the Google Advertising and Measurement Community server.

Integrate the WebView API for Ads

  • The Web View APIs for Ads improves ad monetization by making app signals available to ad tags within your Android WebView.

  • The Google Mobile Ads SDK communicates with your WebView in response to ad events triggered by AdSense, Google Publisher Tag, or IMA for HTML5.

  • To use the Web View APIs for Ads, you need Google Mobile Ads SDK 20.6.0 or higher, Android API level 21 or higher, and to register your WebView using MobileAds.registerWebView().

  • You need to gather consent within the WebView context as it's not automatically propagated from the mobile app's consent flow.

The web view APIs for ads makes app signals available to the tags in your WebView, helping to improve monetization for the publishers that provided the content and protect advertisers from spam.

How it works

Communication with the Google Mobile Ads SDK only happens in response to ad events triggered by any of the following:

The SDK adds message handlers to the registered WebView to listen for these ad events. For a better sense of how this works, view the source code of the test page.

Prerequisites

  • Google Mobile Ads SDK version 20.6.0 or higher.
  • Android API level 21 or higher.

  • Add the following <meta-data> tag in your AndroidManifest.xml file to bypass the check for the APPLICATION_ID. If you miss this step and don't provide the <meta-data> tag, the Google Mobile Ads SDK throws an IllegalStateException on app start.

    <!--BypassAPPLICATION_IDcheckforwebviewAPIsforads-->
    <meta-data
    android:name="com.google.android.gms.ads.INTEGRATION_MANAGER"
    android:value="webview"/>
    

Register the web view

Call registerWebView() on the main thread to establish a connection with the JavaScript handlers in the AdSense code or Google Publisher Tag within each WebView instance. This should be done as early as possible, such as in the onCreate() method of your MainActivity.

Kotlin

importandroid.webkit.CookieManager
importandroid.webkit.WebView
importcom.google.android.gms.ads.MobileAds
classMainActivity:AppCompatActivity(){
lateinitvarwebView:WebView
overridefunonCreate(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
webView=findViewById(R.id.webview)
// Let the web view accept third-party cookies.
CookieManager.getInstance().setAcceptThirdPartyCookies(webView,true)
// Let the web view use JavaScript.
webView.settings.javaScriptEnabled=true
// Let the web view access local storage.
webView.settings.domStorageEnabled=true
// Let HTML videos play automatically.
webView.settings.mediaPlaybackRequiresUserGesture=false
// Register the web view.
MobileAds.registerWebView(webView)
}
}

Java

importandroid.webkit.CookieManager;
importandroid.webkit.WebView;
importcom.google.android.gms.ads.MobileAds;
publicclass MainActivityextendsAppCompatActivity{
privateWebViewwebView;
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView=findViewById(R.id.webview);
// Let the web view accept third-party cookies.
CookieManager.getInstance().setAcceptThirdPartyCookies(webView,true);
// Let the web view use JavaScript.
webView.getSettings().setJavaScriptEnabled(true);
// Let the web view access local storage.
webView.getSettings().setDomStorageEnabled(true);
// Let HTML videos play automatically.
webView.getSettings().setMediaPlaybackRequiresUserGesture(false);
// Register the web view.
MobileAds.registerWebView(webView);
}
}

Test your integration

Before using your own URL, we recommend that you load the following URL to test the integration:

https://google.github.io/webview-ads/test/#api-for-ads-tests

The test URL shows green status bars for a successful integration if the following conditions apply:

  • WebView connected to the Google Mobile Ads SDK

Next steps

  • Gather consent in WebView. The Web view APIs for Ads doesn't propagate consent collected in the mobile app context using IAB TCF v2.0 or IAB CCPA compliance frameworks to the tags in your web views. If you're interested in implementing a single consent flow as the owner of both the WebView and its corresponding web content being monetized, work with your consent management platform to gather consent in the WebView context.

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025年10月15日 UTC.