Integrate the WebView API for Ads

Stay organized with collections Save and categorize content based on your preferences.

The WebView API for Ads allows in-app ad monetization using WebView. If you display web content that implements ads with AdSense code or Google Publisher Tag in your app through WebView, you should use this API to enable ads monetization. To learn more, see the AdSense and Ad Manager policies.

  1. Monetize by making ad requests with the Google Mobile Ads SDK

    You can monetize your app by making ad requests to Ad Manager with the Google Mobile Ads SDK by implementing ad formats for mobile app.

    Learn more.

  2. Monetize by using the WebView API for Ads

    If your app uses WebView to display web content that serves ads from Ad Manager or AdSense, use the WebView API for Ads to register WebView objects with the Google Mobile Ads SDK. The JavaScript in the AdSense code or Google Publisher Tag builds and sends ad requests so you don't need to make any ad requests with the SDK. Keep in mind that only the mobile web and desktop web inventory formats are available using this API.

    If you don't own the web content in a WebView, you are still encouraged to use this API to help protect advertisers from spam and improve monetization for the web publishers that provided the content.

Note that you can do either option, or even both, in the same app.

This guide is intended to help you integrate the WebView API for Ads into your Android app.

Before you begin

Before you start using the WebView API for Ads, make sure you do the following:

  • Use Google Mobile Ads SDK with version 20.6.0 or higher in your app.
  • Use Android API level 21 or higher.
  • Add the following <meta-data> tag in your AndroidManifest.xml file to bypass a check for the APPLICATION_ID. If you miss this step, the Google Mobile Ads SDK might throw an IllegalStateException on app start.

    <!-- Bypass APPLICATION_ID check for WebView API for Ads -->
      <meta-data
          android:name="com.google.android.gms.ads.INTEGRATION_MANAGER"
          android:value="webview"/>
    

Add WebView to the layout

To display web content in an activity, you can place WebView in the corresponding XML layout file. Below is an example that shows an activity’s WebView:

# activity_main.xml
...
<WebView
    android:id="@+id/webview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
/>
...

Alternatively, you can create the WebView programmatically:

Java

WebView webView = new WebView(this);

Kotlin

var webView: WebView? = WebView(this)

Register the WebView

The first step to use the API is to register each WebView that may show ads. You need to use the registerWebView() method provided by the Google Mobile Ads SDK to establish connection with the JavaScript handlers in the AdSense code or Google Publisher Tag within each WebView:

  1. Enable JavaScript in the WebView. Failure to do so can result in an error on the test web page in the next step and cause ads not to load.
  2. We recommend calling registerWebView() as early as possible after all the UI-heavy tasks are completed and before the first URL is loaded. The following example shows how to register the WebView inside the onCreate() method of the MainActivity.

Java

  package ...

  import ...
  import android.webkit.WebView;
  import com.google.android.gms.ads.MobileAds;

  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);
      // Enable JavaScript in the WebView.
      webView.getSettings().setJavaScriptEnabled(true);
      ...
      // Register the WebView.
      MobileAds.registerWebView(webView);
      ...
    }
    ...
  }

Kotlin

  package ...

  import ...
  import android.webkit.WebView
  import com.google.android.gms.ads.MobileAds

  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)
      // Enable JavaScript in the WebView.
      webView.settings.javaScriptEnabled = true
      ...
      // Register the WebView.
      MobileAds.registerWebView(webView)
      ...
    }
    ...
  }

Load the URL

You can now use the loadUrl() method to load a URL and display your web content through WebView. We recommend that you load this test URL: https://webview-api-for-ads-test.glitch.me/ to test the integration prior to using your own URL.

Java

  package ...

  import ...
  import android.webkit.WebView;
  import com.google.android.gms.ads.MobileAds;

  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);
      // Enable JavaScript in the WebView.
      webView.getSettings().setJavaScriptEnabled(true);
      ...
      // Register the WebView.
      MobileAds.registerWebView(webView);
      webView.loadUrl("https://webview-api-for-ads-test.glitch.me/");
      ...
    }
    ...
  }

Kotlin

  package ...

  import ...
  import android.webkit.WebView
  import com.google.android.gms.ads.MobileAds

  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)
      // Enable JavaScript in the WebView.
      webView.settings.javaScriptEnabled = true
      ...
      // Register the WebView.
      MobileAds.registerWebView(webView)
      webView.loadUrl("https://webview-api-for-ads-test.glitch.me/")
      ...
    }
    ...
  }

If the integration was successful, the app should show "WebView integration with GMA SDK confirmed!". You can now replace the test URL with your URL.