# WebViewBuilder

Artifact: [androidx.webkit:webkit](https://developer.android.com/jetpack/androidx/releases/webkit) [View Source](https://cs.android.com/search?q=file:androidx/webkit/WebViewBuilder.java+class:androidx.webkit.WebViewBuilder) Added in [1.15.0](https://developer.android.com/jetpack/androidx/releases/webkit#1.15.0)

*** ** * ** ***


```
@WebViewBuilder.Experimental
class WebViewBuilder
```

<br />

*** ** * ** ***

WebViewBuilder can be used in place of `https://developer.android.com/reference/android/webkit/WebView.html`'s constructor.

This API allows you to declare how the WebView will be used via APIs like `https://developer.android.com/reference/kotlin/androidx/webkit/RestrictionAllowlist`.

WebView instances constructed by this builder can be used as direct drop-in replacements for WebViews created by `https://developer.android.com/reference/android/webkit/WebView.html#WebView(android.content.Context)` with no additional code changes.

## Summary

| ### Nested types |
|---|
| `@https://developer.android.com/reference/java/lang/annotation/Retention.html(value = https://developer.android.com/reference/java/lang/annotation/RetentionPolicy.html#CLASS) @https://developer.android.com/reference/java/lang/annotation/Target.html(value = [https://developer.android.com/reference/java/lang/annotation/ElementType.html#METHOD, https://developer.android.com/reference/java/lang/annotation/ElementType.html#TYPE, https://developer.android.com/reference/java/lang/annotation/ElementType.html#FIELD]) @https://developer.android.com/reference/kotlin/androidx/annotation/RequiresOptIn(level = https://developer.android.com/reference/kotlin/androidx/annotation/RequiresOptIn.Level#ERROR) annotation https://developer.android.com/reference/kotlin/androidx/webkit/WebViewBuilder.Experimental` |

| ### Constants |
|---|---|
| `const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/webkit/WebViewBuilder#PRESET_LEGACY() = 0` Matches the configuration of a WebView created via the `https://developer.android.com/reference/android/webkit/WebView.html#WebView(android.content.Context)` constructor. |

| ### Public constructors |
|---|
| `https://developer.android.com/reference/kotlin/androidx/webkit/WebViewBuilder#WebViewBuilder(int)(preset: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` Create a new builder with settings initialized to the given preset Preset. |

| ### Public functions |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/webkit/WebViewBuilder` | `https://developer.android.com/reference/kotlin/androidx/webkit/WebViewBuilder#addAllowlist(androidx.webkit.RestrictionAllowlist)(allowList: https://developer.android.com/reference/kotlin/androidx/webkit/RestrictionAllowlist)` Add an allowlist of behaviors for a list of origin patterns. |
| `T` | `@https://developer.android.com/reference/kotlin/androidx/annotation/UiThread @https://developer.android.com/reference/kotlin/androidx/annotation/RequiresFeature(name = https://developer.android.com/reference/kotlin/androidx/webkit/WebViewFeature.WEBVIEW_BUILDER_EXPERIMENTAL_V2, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported") <T : https://developer.android.com/reference/android/webkit/WebView.html?> https://developer.android.com/reference/kotlin/androidx/webkit/WebViewBuilder#applyTo(T)(webview: T)` Applies a builder config to an existing but unused WebView. |
| `https://developer.android.com/reference/android/webkit/WebView.html` | `@https://developer.android.com/reference/kotlin/androidx/annotation/UiThread @https://developer.android.com/reference/kotlin/androidx/annotation/RequiresFeature(name = https://developer.android.com/reference/kotlin/androidx/webkit/WebViewFeature.WEBVIEW_BUILDER_EXPERIMENTAL_V1, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported") https://developer.android.com/reference/kotlin/androidx/webkit/WebViewBuilder#build(android.content.Context)(context: https://developer.android.com/reference/android/content/Context.html)` Constructs a new WebView with all the properties defined. |
| `https://developer.android.com/reference/kotlin/androidx/webkit/WebViewBuilder` | `https://developer.android.com/reference/kotlin/androidx/webkit/WebViewBuilder#restrictJavaScriptInterfaces()()` Restrict `https://developer.android.com/reference/android/webkit/WebView.html#addJavascriptInterface(java.lang.Object, java.lang.String)` and `https://developer.android.com/reference/android/webkit/WebView.html#removeJavascriptInterface(java.lang.String)` from being callable. |
| `https://developer.android.com/reference/kotlin/androidx/webkit/WebViewBuilder` | `@https://developer.android.com/reference/kotlin/androidx/annotation/RequiresFeature(name = https://developer.android.com/reference/kotlin/androidx/webkit/WebViewFeature.MULTI_PROFILE, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported") https://developer.android.com/reference/kotlin/androidx/webkit/WebViewBuilder#setProfile(java.lang.String)(profileName: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)` Set the profile for the WebView. |

## Constants

### PRESET_LEGACY

Added in [1.15.0](https://developer.android.com/jetpack/androidx/releases/webkit#1.15.0)

```
const val PRESET_LEGACY = 0: Int
```

Matches the configuration of a WebView created via the `https://developer.android.com/reference/android/webkit/WebView.html#WebView(android.content.Context)` constructor.

## Public constructors

### WebViewBuilder

Added in [1.15.0](https://developer.android.com/jetpack/androidx/releases/webkit#1.15.0)

```
WebViewBuilder(preset: Int)
```

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

Currently, only the `https://developer.android.com/reference/kotlin/androidx/webkit/WebViewBuilder#PRESET_LEGACY()` preset is supported.

## Public functions

### addAllowlist

Added in [1.15.0](https://developer.android.com/jetpack/androidx/releases/webkit#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 `https://developer.android.com/reference/kotlin/androidx/webkit/WebViewBuilder#build(android.content.Context)` if a behavior is allowlisted that has not been restricted via the WebViewBuilder.

| Parameters |
|---|---|
| `allowList: https://developer.android.com/reference/kotlin/androidx/webkit/RestrictionAllowlist` | An allowlist that will allow behaviors for the origin patterns provided. |

### applyTo

Added in [1.16.0](https://developer.android.com/jetpack/androidx/releases/webkit#1.16.0)

```
@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 `https://developer.android.com/reference/kotlin/androidx/webkit/WebViewBuilder#build(android.content.Context)` 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 `https://developer.android.com/reference/kotlin/androidx/webkit/WebViewBuilder#build(android.content.Context)`.

| Parameters |
|---|---|
| `webview: T` | The WebView to apply the config to. |

| Throws |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/webkit/WebViewBuilderException` | if there was an issue with validation or constructing the WebView. |
| `https://developer.android.com/reference/java/lang/IllegalStateException.html` | if the WebView has already been used or configured in some way. |
| `https://developer.android.com/reference/java/lang/UnsupportedOperationException.html` | if the `https://developer.android.com/reference/kotlin/androidx/webkit/WebViewFeature#WEBVIEW_BUILDER_EXPERIMENTAL_V2()` feature is not supported. This should be checked before use with `https://developer.android.com/reference/kotlin/androidx/webkit/WebViewFeature#isFeatureSupported(java.lang.String)`. |

### build

Added in [1.15.0](https://developer.android.com/jetpack/androidx/releases/webkit#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.

| Parameters |
|---|---|
| `context: https://developer.android.com/reference/android/content/Context.html` | The Activity Context for the WebView. |

| Throws |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/webkit/WebViewBuilderException` | if there was an issue with validation or constructing the WebView. |
| `https://developer.android.com/reference/java/lang/UnsupportedOperationException.html` | if the `https://developer.android.com/reference/kotlin/androidx/webkit/WebViewFeature#WEBVIEW_BUILDER_EXPERIMENTAL_V1()` feature is not supported. This should be checked before use with `https://developer.android.com/reference/kotlin/androidx/webkit/WebViewFeature#isFeatureSupported(java.lang.String)`. |

### restrictJavaScriptInterfaces

Added in [1.15.0](https://developer.android.com/jetpack/androidx/releases/webkit#1.15.0)

```
fun restrictJavaScriptInterfaces(): WebViewBuilder
```

Restrict `https://developer.android.com/reference/android/webkit/WebView.html#addJavascriptInterface(java.lang.Object, java.lang.String)` and `https://developer.android.com/reference/android/webkit/WebView.html#removeJavascriptInterface(java.lang.String)` 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 `https://developer.android.com/reference/kotlin/androidx/webkit/RestrictionAllowlist.Builder#addJavaScriptInterface(java.lang.Object,java.lang.String)`.

### setProfile

Added in [1.15.0](https://developer.android.com/jetpack/androidx/releases/webkit#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 `https://developer.android.com/reference/kotlin/androidx/webkit/WebViewBuilder#build(android.content.Context)` is called, as per `https://developer.android.com/reference/kotlin/androidx/webkit/ProfileStore#getOrCreateProfile(java.lang.String)`.

| Parameters |
|---|---|
| `profileName: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html` | The name of the profile to use. |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/UnsupportedOperationException.html` | if the `https://developer.android.com/reference/kotlin/androidx/webkit/WebViewFeature#MULTI_PROFILE()` feature is not supported. This should be checked before use with `https://developer.android.com/reference/kotlin/androidx/webkit/WebViewFeature#isFeatureSupported(java.lang.String)`. |