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
Nested types |
|---|
@Retention(value = RetentionPolicy.CLASS) |
Constants |
|
|---|---|
const Int |
PRESET_LEGACY = 0Matches the configuration of a WebView created via the |
Public constructors |
|---|
WebViewBuilder(preset: Int)Create a new builder with settings initialized to the given preset Preset. |
Public functions |
|
|---|---|
WebViewBuilder |
addAllowlist(allowList: RestrictionAllowlist)Add an allowlist of behaviors for a list of origin patterns. |
T |
@UiThreadApplies a builder config to an existing but unused WebView. |
WebView |
@UiThreadConstructs a new WebView with all the properties defined. |
WebViewBuilder |
Restrict |
WebViewBuilder |
@RequiresFeature(name = WebViewFeature.MULTI_PROFILE, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")Set the profile for the WebView. |
Constants
PRESET_LEGACY
const val PRESET_LEGACY = 0: Int
Matches the configuration of a WebView created via the WebView constructor.
Public constructors
WebViewBuilder
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
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
@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.
| 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 |
build
@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. |
java.lang.UnsupportedOperationException |
if the |
restrictJavaScriptInterfaces
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
@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. |
| Throws | |
|---|---|
java.lang.UnsupportedOperationException |
if the |