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

Set up WebView

  • This guide explains how to configure Android WebView for optimal ad monetization by enabling features like third-party cookies, JavaScript, and local storage access.

  • It is crucial to apply all recommendations to every WebView instance in your app for consistent ad performance.

  • Developers should use loadUrl() with network-based URLs directly to ensure proper functionality of cookies and optimized web view performance.

  • A provided test URL helps verify the effectiveness of your settings for ad integration during development.

  • For production, replace the test URL with the intended URL for your WebView to load.

If your app utilizes WebView to display web content, it's recommended to configure it so that content can be optimally monetized with ads.

This guide shows you how to provide information about how to configure a WebView object.

Enable third-party cookies

To improve your user's ad experience and be consistent with Chrome's cookie policy, enable third-party cookies on your WebView instance.

Java

CookieManager.getInstance().setAcceptThirdPartyCookies(webView,true);

Kotlin

CookieManager.getInstance().setAcceptThirdPartyCookies(webView,true)

Web settings

Default WebView settings are not optimized for ads. Use the WebSettings APIs to configure your WebView for:

  • JavaScript
  • Access to local storage
  • Automatic video play

Java

importandroid.webkit.CookieManager;
importandroid.webkit.WebView;
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);
}
}

Kotlin

importandroid.webkit.CookieManager
importandroid.webkit.WebView
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
}
}

Load web view content

Cookies and page URLs are important for web view monetization and only function as expected when loadUrl() is used with a network-based URL. For optimized WebView performance, load web content directly from network-based URLs. Avoid using WebViewAssetLoader, loading assets from the device, or generating web content dynamically.

Java

importandroid.webkit.CookieManager;
importandroid.webkit.WebView;
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);
// Load the URL for optimized web view performance.
webView.loadUrl("https://google.github.io/webview-ads/test/");
}
}

Kotlin

importandroid.webkit.CookieManager
importandroid.webkit.WebView
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
// Load the URL for optimized web view performance.
webView.loadUrl("https://google.github.io/webview-ads/test/")
}
}

Test the web view

During app development, we recommend that you load this test URL:

https://google.github.io/webview-ads/test/

to verify these settings have the intended effect on ads. The test URL has success criteria for a complete integration if the following are observed:

Web view settings

  • Third-party cookies work
  • First-party cookies work
  • JavaScript enabled
  • DOM storage enabled

Video ad

  • The video ad plays inline and does not open in the full screen built-in player
  • The video ad plays automatically without clicking the play button
  • The video ad is replayable

After testing is complete, substitute the test URL with the URL the web view intends to load.

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.