androidx.webkit


The Jetpack Webkit library is a static library you can add to your Android application in order to use android.webkit APIs that are not available for older platform versions.

How to use this library in your app

Add this to your build.gradle file:

dependencies {
    implementation "androidx.webkit:webkit:1.14.0"
}

Important: replace 1.14.0 with the latest version from https://da.cyanowen.top/jetpack/androidx/releases/webkit.

Jetpack Webkit and Android System WebView

The Jetpack Webkit library enables developers to access new features that are available in the installed version of Android System WebView, even if those features are not exposed through the android.webkit framework API. It does this by dynamically checking the set of available features through the WebViewFeature class.

You should take care to always check feature availability before calling an API, as you otherwise risk a runtime crash if the WebView provider installed on a users device doesn't support the feature in question. This is most likely to happen if the user in question has not yet updated to a version of Android System WebView that supports the feature, but in rare cases WebView may also stop supporting a previously supported feature as part of an API deprecation.

Sample apps

Please check out the WebView samples on GitHub for a showcase of a handful of Jetpack Webkit APIs.

For more APIs, check out the sample app in the AndroidX repo.

Public bug tracker

If you find bugs in the Jetpack Webkit library or want to request new features, please file a ticket.

Migrating to Jetpack Webkit

For static methods:

Old code:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
   WebView.startSafeBrowsing(appContext, callback);
}

New code:

if (WebViewFeature.isFeatureSupported(WebViewFeature.START_SAFE_BROWSING)) {
   WebViewCompat.startSafeBrowsing(appContext, callback);
}

Or, if you are using a non-static method:

Old code:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
   myWebView.postVisualStateCallback(requestId, callback);
}

New code:

if (WebViewFeature.isFeatureSupported(WebViewFeature.VISUAL_STATE_CALLBACK)) {
   WebViewCompat.postVisualStateCallback(myWebView, requestId, callback);
}

Experimental APIs

The Jetpack Webkit library contains a number of experimental APIs. These APIs will be marked with annotations that use the @RequiresOptIn annotation. We encourage you to try out these APIs in your application and file feedback on the public bug tracker if you encounter any issues with the API in the current shape.

When using an experimental API, you should keep the following points in mind:

  • New features or changes may be introduced to experimental APIs. If you are using experimental APIs, we recommend that you always use the latest version of the library in your dependencies, and that you adopt changes in the API as soon as possible. For example, if you are building against the stable version of the library, you should make sure to upgrade to the next stable version as soon as it becomes available.
  • Experimental APIs may be removed from future versions of the library. If this happens, you will not be able to continue using the API by relying on an older version of the library, since the support for the feature will also have been removed from new versions of WebView.
  • The number of WebView versions that support a given experimental API depends on the release status of a given experimental API. For stable library releases, you can expect that WebView versions will continue to offer support until the next stable release of the library. For example, if an experimental API is available in androidx.webkit:webkit:1.14.0, then the stable version of WebView will continue to support the API until androidx.webkit:webkit:1.15.0 has been released, but after the next release the support may then be dropped in WebView versions, depending on the evolution of the API.

Interfaces

Navigation

The Navigation instance passed by the navigation callbacks.

NavigationListener

Page identification and lifecycle APIs.

OutcomeReceiverCompat

Callback interface intended for use when an asynchronous operation may result in a failure.

Page

This class serves solely as a key for Page-associated data.

PrerenderOperationCallback

Callback interface for the prerender operation.

Profile

A Profile represents one browsing session for WebView.

ProfileStore

Manages any creation, deletion for Profile.

ScriptHandler

This interface represents the return result from addDocumentStartJavaScript.

StartUpLocation

The code location where WebView startup was triggered.

WebNavigationClient

Page identification and lifecycle APIs.

WebViewAssetLoader.PathHandler

A handler that produces responses for a registered path.

WebViewCompat.VisualStateCallback

Callback interface supplied to postVisualStateCallback for receiving notifications about the visual state.

WebViewCompat.WebMessageListener

This listener receives messages sent on the JavaScript object which was injected by addWebMessageListener.

WebViewCompat.WebViewStartUpCallback

Callback interface for startUpWebView.

WebViewStartUpResult

Result object associated with onSuccess.

Classes

BackForwardCacheSettings

A class for developers to configure the back-forward cache on a android.webkit.WebView.

BackForwardCacheSettings.Builder

Builder for BackForwardCacheSettings.

CookieManagerCompat

Compatibility version of android.webkit.CookieManager

CustomHeader

A HTTP header name-value pair that will be sent on all requests to origins that match the given origin rules.

DropDataContentProvider

WebView provides partial support for Android Drag and Drop allowing images, text and links to be dragged out of a WebView.

JavaScriptReplyProxy

This class represents the JavaScript object injected by WebViewCompat#addWebMessageListener.

NoVarySearchHeader

The No-Vary-Search header specifies a set of rules that define how a URL's query parameters will affect cache matching.

ProcessGlobalConfig

Process Global Configuration for WebView.

ProxyConfig

Config for setProxyOverride.

ProxyConfig.Builder

ProxyConfig builder.

ProxyConfig.ProxyRule

Class that holds a scheme filter and a proxy URL.

ProxyController

Manages setting and clearing a process-specific override for the Android system-wide proxy settings that govern network requests made by android.webkit.WebView.

RestrictionAllowlist

RestrictionAllowlist can be used to scope WebView behaviors to particular origin patterns.

RestrictionAllowlist.Builder

RestrictionAllowlist builder.

SafeBrowsingResponseCompat

Compatibility version of SafeBrowsingResponse.

ServiceWorkerClientCompat

Base class for clients to capture Service Worker related callbacks, see ServiceWorkerControllerCompat for usage example.

ServiceWorkerControllerCompat

Manages Service Workers used by WebView.

ServiceWorkerWebSettingsCompat

Manages settings state for all Service Workers.

SpeculativeLoadingConfig

Represents a configuration for speculative loading in a Profile instance.

SpeculativeLoadingConfig.Builder
SpeculativeLoadingParameters

Parameters for customizing the prefetch.

SpeculativeLoadingParameters.Builder

A builder class to use to construct the SpeculativeLoadingParameters.

TracingConfig

Holds tracing configuration information and predefined settings for TracingController.

TracingConfig.Builder

Builder used to create TracingConfig objects.

TracingController

Manages tracing of WebViews.

URLUtilCompat

Compatibility versions of methods in android.webkit.URLUtil.

UserAgentMetadata

Holds user-agent metadata information and uses to generate user-agent client hints.

UserAgentMetadata.BrandVersion

Class that holds brand name, major version and full version.

UserAgentMetadata.BrandVersion.Builder

Builder used to create BrandVersion objects.

UserAgentMetadata.Builder

Builder used to create UserAgentMetadata objects.

WebMessageCompat

The Java representation of the HTML5 PostMessage event.

WebMessagePortCompat

The Java representation of the HTML5 message ports.

WebMessagePortCompat.WebMessageCallbackCompat

The listener for handling MessagePort events.

WebResourceErrorCompat

Compatibility version of WebResourceError.

WebResourceRequestCompat

Compatibility version of WebResourceRequest.

WebResourceResponseCompat

Compatibility version of WebResourceResponse.

WebSettingsCompat

Compatibility version of android.webkit.WebSettings

WebStorageCompat

Compatibility class for android.webkit.WebStorage that introduces new methods to completely clear data.

WebViewAssetLoader

Helper class to load local files including application's static assets and resources using http(s):// URLs inside a android.webkit.WebView class.

WebViewAssetLoader.AssetsPathHandler

Handler class to open a file from assets directory in the application APK.

WebViewAssetLoader.Builder

A builder class for constructing WebViewAssetLoader objects.

WebViewAssetLoader.InternalStoragePathHandler

Handler class to open files from application internal storage.

WebViewAssetLoader.ResourcesPathHandler

Handler class to open a file from resources directory in the application APK.

WebViewBuilder

WebViewBuilder can be used in place of android.webkit.WebView's constructor.

WebViewClientCompat

Compatibility version of android.webkit.WebViewClient.

WebViewCompat

Compatibility version of WebView

WebViewFeature

Utility class for checking which WebView Support Library features are supported on the device.

WebViewMediaIntegrityApiStatusConfig

Configuration to set API enablement status for site origins through override rules.

WebViewMediaIntegrityApiStatusConfig.Builder

Builds a WebViewMediaIntegrityApiStatusConfig having a default API status and a map of origin pattern rules to their respective API status.

WebViewRenderProcess

WebViewRenderProcess provides an opaque handle to a WebView renderer.

WebViewRenderProcessClient

Used to receive callbacks on WebView renderer events.

WebViewStartUpConfig

Configuration object for startUpWebView.

WebViewStartUpConfig.Builder

Exceptions

PrefetchException

Super class for all asynchronous exceptions produced by Profile prefetch operations.

PrefetchNetworkException

Class for network &server related Exceptions produced by Profile prefetch operations.

PrerenderException

Super class for all asynchronous exceptions produced by WebViewCompat prerender operations.

WebViewBuilderException

Thrown by build if there was an issue with validation or constructing the WebView.

Annotations

Profile.ExperimentalAddQuicHints

Denotes that the Profile#addQuicHints API surface is experimental.

Profile.ExperimentalOriginMatchedHeader

Denotes that the OriginMatchedHeader API surface is experimental.

Profile.ExperimentalPreconnect

Denotes that the Profile#preconnect API surface is experimental.

Profile.ExperimentalUrlPrefetch

Denotes that the UrlPrefetch API surface is experimental.

Profile.ExperimentalWarmUpRendererProcess

Denotes that the WarmUpRendererProcess API surface is experimental.

WebNavigationClient.ExperimentalNavigationCallback

Denotes Navigation, Page, WebNavigationClient, and NavigationListener API surfaces are experimental.

WebSettingsCompat.ExperimentalBackForwardCacheSettings

Denotes that the SpeculativeLoading API surface is experimental.

WebSettingsCompat.ExperimentalSpeculativeLoading

Denotes that the SpeculativeLoading API surface is experimental.

WebViewBuilder.Experimental
WebViewCompat.ExperimentalAsyncStartUp

Denotes that the async startup-related API (e.g startupWebView, ProcessGlobalConfig.setUiThreadStartupMode) is experimental.

WebViewCompat.ExperimentalSaveState

Denotes that the WebViewCompat#saveState API surface is experimental.