# 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
public final 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/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/androidx/annotation/RequiresOptIn(level = https://developer.android.com/reference/androidx/annotation/RequiresOptIn.Level#ERROR) public annotation https://developer.android.com/reference/androidx/webkit/WebViewBuilder.Experimental` |

| ### Constants |
|---|---|
| `static final int` | `https://developer.android.com/reference/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/androidx/webkit/WebViewBuilder#WebViewBuilder(int)(int preset)` Create a new builder with settings initialized to the given preset Preset. |

| ### Public methods |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/webkit/WebViewBuilder` | `https://developer.android.com/reference/androidx/webkit/WebViewBuilder#addAllowlist(androidx.webkit.RestrictionAllowlist)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/webkit/RestrictionAllowlist allowList)` Add an allowlist of behaviors for a list of origin patterns. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html T` | `@https://developer.android.com/reference/androidx/annotation/UiThread @https://developer.android.com/reference/androidx/annotation/RequiresFeature(name = https://developer.android.com/reference/androidx/webkit/WebViewFeature.WEBVIEW_BUILDER_EXPERIMENTAL_V2, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported") <T extends https://developer.android.com/reference/android/webkit/WebView.html> https://developer.android.com/reference/androidx/webkit/WebViewBuilder#applyTo(T)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html T webview)` Applies a builder config to an existing but unused WebView. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/webkit/WebView.html` | `@https://developer.android.com/reference/androidx/annotation/UiThread @https://developer.android.com/reference/androidx/annotation/RequiresFeature(name = https://developer.android.com/reference/androidx/webkit/WebViewFeature.WEBVIEW_BUILDER_EXPERIMENTAL_V1, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported") https://developer.android.com/reference/androidx/webkit/WebViewBuilder#build(android.content.Context)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html context)` Constructs a new WebView with all the properties defined. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/webkit/WebViewBuilder` | `https://developer.android.com/reference/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://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/webkit/WebViewBuilder` | `@https://developer.android.com/reference/androidx/annotation/RequiresFeature(name = https://developer.android.com/reference/androidx/webkit/WebViewFeature.MULTI_PROFILE, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported") https://developer.android.com/reference/androidx/webkit/WebViewBuilder#setProfile(java.lang.String)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/String.html profileName)` 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)

```
public static final int 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

### WebViewBuilder

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

```
public WebViewBuilder(int preset)
```

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

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

## Public methods

### addAllowlist

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

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/webkit/RestrictionAllowlist allowList` | 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")
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 `https://developer.android.com/reference/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/androidx/webkit/WebViewBuilder#build(android.content.Context)`.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html T webview` | The WebView to apply the config to. |

| Throws |
|---|---|
| `https://developer.android.com/reference/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/androidx/webkit/WebViewFeature#WEBVIEW_BUILDER_EXPERIMENTAL_V2()` feature is not supported. This should be checked before use with `https://developer.android.com/reference/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")
public @NonNull WebView build(@NonNull Context context)
```

Constructs a new WebView with all the properties defined.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html context` | The Activity Context for the WebView. |

| Throws |
|---|---|
| `https://developer.android.com/reference/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/androidx/webkit/WebViewFeature#WEBVIEW_BUILDER_EXPERIMENTAL_V1()` feature is not supported. This should be checked before use with `https://developer.android.com/reference/androidx/webkit/WebViewFeature#isFeatureSupported(java.lang.String)`. |

### restrictJavaScriptInterfaces

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

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

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

Set the profile for the WebView.

If the profile does not exist, it will be created when `https://developer.android.com/reference/androidx/webkit/WebViewBuilder#build(android.content.Context)` is called, as per `https://developer.android.com/reference/androidx/webkit/ProfileStore#getOrCreateProfile(java.lang.String)`.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/String.html profileName` | The name of the profile to use. |

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