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 T
@UiThread
@RequiresFeature(name = WebViewFeature.WEBVIEW_BUILDER_EXPERIMENTAL_V2, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
<T extends WebView> applyTo(@NonNull T webview)

Applies a builder config to an existing but unused WebView.

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

applyTo

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

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
@NonNull T webview

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

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

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

Parameters
@NonNull String profileName

The name of the profile to use.

Throws
java.lang.UnsupportedOperationException

if the MULTI_PROFILE feature is not supported.