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.

T
@UiThread
@RequiresFeature(name = WebViewFeature.WEBVIEW_BUILDER_EXPERIMENTAL_V2, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
<T : WebView?> applyTo(webview: T)

Applies a builder config to an existing but unused WebView.

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
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
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
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.

applyTo

Added in 1.16.0-beta01
@UiThread
@RequiresFeature(name = WebViewFeature.WEBVIEW_BUILDER_EXPERIMENTAL_V2, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
fun <T : WebView?> applyTo(webview: T): T

Applies a builder config to an existing but unused WebView.

This allows the builder to be used in cases where build is not practical, including cases where WebView has been inflated from an XML layout or subclassed.

It is not permitted to call any other WebView APIs on the WebView before this. A WebView may only have a builder configuration applied at most once. This API may not be used with WebViews that were built with build.

This method should only be called if isFeatureSupported returns true for WEBVIEW_BUILDER_EXPERIMENTAL_V2.

Parameters
webview: T

The WebView to apply the config to.

Throws
androidx.webkit.WebViewBuilderException

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

java.lang.IllegalStateException

if the WebView has already been used or configured in some way.

java.lang.UnsupportedOperationException

if the WEBVIEW_BUILDER_EXPERIMENTAL_V2 feature is not supported.

build

Added in 1.15.0
@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.

This method should only be called if isFeatureSupported returns true for WEBVIEW_BUILDER_EXPERIMENTAL_V1.

Parameters
context: Context

The Activity Context for the WebView.

Throws
androidx.webkit.WebViewBuilderException

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

java.lang.UnsupportedOperationException

if the WEBVIEW_BUILDER_EXPERIMENTAL_V1 feature is not supported.

restrictJavaScriptInterfaces

Added in 1.15.0
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
@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.

This method should only be called if isFeatureSupported returns true for MULTI_PROFILE.

Parameters
profileName: String

The name of the profile to use.

Throws
java.lang.UnsupportedOperationException

if the MULTI_PROFILE feature is not supported.