The WebView API for Ads enables 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.
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.
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 registerWebView
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.
Prerequisites
- Google Mobile Ads SDK version 20.6.0 or higher
- Android API level 21 or higher
Add the following
<meta-data>
tag in yourAndroidManifest.xml
file to bypass the check for theAPPLICATION_ID
. If you miss this step, the Google Mobile Ads SDK might throw anIllegalStateException
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. Here 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)
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)
Register the WebView
The first step in using the API is to register each WebView
that could 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
:
- 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. - For best results, enable third-party cookies (before registering your
WebView
). 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 theWebView
inside theonCreate()
method of theMainActivity
.Java
package ... import ... import android.webkit.CookieManager; 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); // Enable third-party cookies. CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true); ... // Register the WebView. MobileAds.registerWebView(webView); ... } ... }
Kotlin
package ... import ... import android.webkit.CookieManager 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 // Enable third-party cookies. CookieManager.getInstance().setAcceptThirdPartyCookies(webView, 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.CookieManager; 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); // Enable third-party cookies. CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true); ... // Register the WebView. MobileAds.registerWebView(webView); webView.loadUrl("https://webview-api-for-ads-test.glitch.me/"); ... } ... }
Kotlin
package ... import ... import android.webkit.CookieManager 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 // Enable third-party cookies. CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true); ... // Register the WebView. MobileAds.registerWebView(webView) webView.loadUrl("https://webview-api-for-ads-test.glitch.me/") ... } ... }
If the integration was successful, the app shows "WebView integration with GMA SDK confirmed!" You can then replace the test URL with your URL.
You can also use a proxy tool such as Charles
to capture your app's HTTPS traffic and inspect the ad requests for a &scar=
parameter.