WebViewBuilder


@WebViewBuilder.Experimental
class WebViewBuilder


WebViewBuilder can be used in place of android.webkit.WebView's constructor.

This API allows you to declare how the WebView will be used via APIs like RestrictionAllowlist.

WebView instances constructed by this builder can be used as direct drop-in replacements for WebViews created by WebView with no additional code changes.

Summary

Constants

const Int

Matches the configuration of a WebView created via the WebView constructor.

Public constructors

Create a new builder with settings initialized to the given preset Preset.

Public functions

WebViewBuilder

Add an allowlist of behaviors for a list of origin patterns.

WebView
@UiThread
@RequiresFeature(name = WebViewFeature.WEBVIEW_BUILDER_EXPERIMENTAL_V1, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
build(context: Context)

Constructs a new WebView with all the properties defined.

WebViewBuilder

Restrict addJavascriptInterface and removeJavascriptInterface from being callable.

WebViewBuilder
@RequiresFeature(name = WebViewFeature.MULTI_PROFILE, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
setProfile(profileName: String)

Set the profile for the WebView.

Constants

PRESET_LEGACY

Added in 1.15.0-beta01
const val PRESET_LEGACY = 0: Int

Matches the configuration of a WebView created via the WebView constructor.

Public constructors

WebViewBuilder

Added in 1.15.0-beta01
WebViewBuilder(preset: Int)

Create a new builder with settings initialized to the given preset Preset.

Currently, only the PRESET_LEGACY preset is supported.

Public functions

addAllowlist

Added in 1.15.0-beta01
fun addAllowlist(allowList: RestrictionAllowlist): WebViewBuilder

Add an allowlist of behaviors for a list of origin patterns. All allowlists will be merged together. A WebViewBuilderException will be thrown from build if a behavior is allowlisted that has not been restricted via the WebViewBuilder.

Parameters
allowList: RestrictionAllowlist

An allowlist that will allow behaviors for the origin patterns provided.

build

Added in 1.15.0-beta01
@UiThread
@RequiresFeature(name = WebViewFeature.WEBVIEW_BUILDER_EXPERIMENTAL_V1, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
fun build(context: Context): WebView

Constructs a new WebView with all the properties defined.

Parameters
context: Context

The Activity Context for the WebView.

Throws
androidx.webkit.WebViewBuilderException

if there was an issue with validation or constructing the WebView.

restrictJavaScriptInterfaces

Added in 1.15.0-beta01
fun restrictJavaScriptInterfaces(): WebViewBuilder

Restrict addJavascriptInterface and removeJavascriptInterface from being callable.

Opting into this restriction makes these methods throw a RuntimeException if called on the built WebView.

This needs to be called in order to allow specific origin patterns to inject JavaScript interfaces via addJavaScriptInterface.

setProfile

Added in 1.15.0-beta01
@RequiresFeature(name = WebViewFeature.MULTI_PROFILE, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
fun setProfile(profileName: String): WebViewBuilder

Set the profile for the WebView.

If the profile does not exist, it will be created when build is called, as per getOrCreateProfile.

Parameters
profileName: String

The name of the profile to use.