设置 WebView

If your Android 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

默认的WebView设置不会针对广告进行优化。使用 WebSettings 用于配置 WebView 的 API:

  • JavaScript
  • 访问本地存储空间
  • 自动播放视频

Java

import android.webkit.CookieManager;
import android.webkit.WebView;

public class MainActivity extends AppCompatActivity {
  private WebView webView;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    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

import android.webkit.CookieManager
import android.webkit.WebView

class MainActivity : AppCompatActivity() {
  lateinit var webView: WebView

  override fun onCreate(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 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

import android.webkit.CookieManager;
import android.webkit.WebView;

public class MainActivity extends AppCompatActivity {
  private WebView webView;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    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://webview-api-for-ads-test.glitch.me");
  }
}

Kotlin

import android.webkit.CookieManager
import android.webkit.WebView

class MainActivity : AppCompatActivity() {
  lateinit var webView: WebView

  override fun onCreate(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://webview-api-for-ads-test.glitch.me")
  }
}

Test the web view

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

https://webview-api-for-ads-test.glitch.me#webview-settings-tests

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.