WebViewBuilder


@WebViewBuilder.Experimental
public final 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

static final int

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

Public constructors

WebViewBuilder(int preset)

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

Public methods

@NonNull WebViewBuilder

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

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

Constructs a new WebView with all the properties defined.

@NonNull WebViewBuilder

Restrict addJavascriptInterface and removeJavascriptInterface from being callable.

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

Set the profile for the WebView.

Constants

PRESET_LEGACY

Added in 1.15.0-beta01
public static final int PRESET_LEGACY = 0

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

Public constructors

WebViewBuilder

Added in 1.15.0-beta01
public WebViewBuilder(int preset)

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

Currently, only the PRESET_LEGACY preset is supported.

Public methods

addAllowlist

Added in 1.15.0-beta01
public @NonNull WebViewBuilder addAllowlist(@NonNull RestrictionAllowlist allowList)

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
@NonNull RestrictionAllowlist allowList

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")
public @NonNull WebView build(@NonNull Context context)

Constructs a new WebView with all the properties defined.

Parameters
@NonNull 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
public @NonNull WebViewBuilder restrictJavaScriptInterfaces()

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")
public @NonNull WebViewBuilder setProfile(@NonNull String profileName)

Set the profile for the WebView.

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

Parameters
@NonNull String profileName

The name of the profile to use.