Register now for Android Dev Summit 2019!

WebViewAssetLoader

open class WebViewAssetLoader
kotlin.Any
   ↳ androidx.webkit.WebViewAssetLoader

Helper class to enable accessing the application's static assets and resources under an http(s):// URL to be loaded by android.webkit.WebView class. Hosting assets and resources this way is desirable as it is compatible with the Same-Origin policy.

For more context about application's assets and resources and how to normally access them please refer to Android Developer Docs: App resources overview.

This class is expected to be used within android.webkit.WebViewClient#shouldInterceptRequest, which is not invoked on the application's main thread. Although instances are themselves thread-safe (and may be safely constructed on the application's main thread), exercise caution when accessing private data or the view system.

Using http(s):// URLs to access local resources may conflict with a real website. This means that local resources should only be hosted on domains your organization owns (at paths reserved for this purpose) or the default domain Google has reserved for this: appassets.androidplatform.net.

A typical usage would be like:

WebViewAssetLoader.Builder assetLoaderBuilder = new WebViewAssetLoader.Builder(this);
      final WebViewAssetLoader assetLoader = assetLoaderBuilder.build();
      webView.setWebViewClient(new WebViewClient() {
          @Override
          public WebResourceResponse shouldInterceptRequest(WebView view,
                                           WebResourceRequest request) {
              return assetLoader.shouldInterceptRequest(request);
          }
      });
      // Assets are hosted under http(s)://appassets.androidplatform.net/assets/... by default.
      // If the application's assets are in the "main/assets" folder this will read the file
      // from "main/assets/www/index.html" and load it as if it were hosted on:
      // https://appassets.androidplatform.net/assets/www/index.html
      webview.loadUrl(assetLoader.getAssetsHttpsPrefix().buildUpon()
                                       .appendPath("www")
                                       .appendPath("index.html")
                                       .build().toString());
 
  

Summary

Nested classes

A builder class for constructing WebViewAssetLoader objects.

Constants

static String

An unused domain reserved by Google for Android applications to intercept requests for app assets.

Public methods

open Uri?

Get the HTTP URL prefix under which assets are hosted.

open Uri

Get the HTTPS URL prefix under which assets are hosted.

open Uri?

Get the HTTP URL prefix under which resources are hosted.

open Uri

Get the HTTPS URL prefix under which resources are hosted.

open WebResourceResponse?

Attempt to resolve the WebResourceRequest to an application resource or asset, and return a WebResourceResponse for the content.

open WebResourceResponse?

Attempt to resolve the url to an application resource or asset, and return a WebResourceResponse for the content.

Constants

KNOWN_UNUSED_AUTHORITY

static val KNOWN_UNUSED_AUTHORITY: String

An unused domain reserved by Google for Android applications to intercept requests for app assets.

It'll be used by default unless the user specified a different domain.

Value: "appassets.androidplatform.net"

Public methods

getAssetsHttpPrefix

@Nullable open fun getAssetsHttpPrefix(): Uri?

Get the HTTP URL prefix under which assets are hosted.

If HTTP is allowed, the prefix will be on the format: "http://<domain>/<prefix-path>/", for example: "http://appassets.androidplatform.net/assets/".

Return
Uri?: the HTTP URL prefix under which assets are hosted, or null if HTTP is not enabled.

getAssetsHttpsPrefix

@NonNull open fun getAssetsHttpsPrefix(): Uri

Get the HTTPS URL prefix under which assets are hosted.

The prefix will be on the format: "https://<domain>/<prefix-path>/", if the default values are used then it will be: "https://appassets.androidplatform.net/assets/".

Return
Uri: the HTTPS URL prefix under which assets are hosted.

getResourcesHttpPrefix

@Nullable open fun getResourcesHttpPrefix(): Uri?

Get the HTTP URL prefix under which resources are hosted.

If HTTP is allowed, the prefix will be on the format: "http://<domain>/<prefix-path>/", for example "http://appassets.androidplatform.net/res/".

Return
Uri?: the HTTP URL prefix under which resources are hosted, or null if HTTP is not enabled.

getResourcesHttpsPrefix

@NonNull open fun getResourcesHttpsPrefix(): Uri

Get the HTTPS URL prefix under which resources are hosted.

The prefix will be on the format: "https://<domain>/<prefix-path>/", if the default values are used then it will be: "https://appassets.androidplatform.net/res/".

Return
Uri: the HTTPs URL prefix under which resources are hosted.

shouldInterceptRequest

@Nullable open fun shouldInterceptRequest(@NonNull request: WebResourceRequest): WebResourceResponse?

Attempt to resolve the WebResourceRequest to an application resource or asset, and return a WebResourceResponse for the content.

The prefix path used shouldn't be a prefix of a real web path. Thus, in case of having a URL that matches a registered prefix path but the requested asset cannot be found or opened a WebResourceResponse object with a null InputStream will be returned instead of null. This saves the time of falling back to network and trying to resolve a path that doesn't exist. A WebResourceResponse with null InputStream will be received as an HTTP response with status code 404 and no body.

This method should be invoked from within android.webkit.WebViewClient#shouldInterceptRequest(android.webkit.WebView, WebResourceRequest).

Parameters
request WebResourceRequest: the WebResourceRequest to process.
Return
WebResourceResponse?: WebResourceResponse if the request URL matches a registered url, null otherwise.

shouldInterceptRequest

@Nullable open fun shouldInterceptRequest(@NonNull url: String): WebResourceResponse?

Attempt to resolve the url to an application resource or asset, and return a WebResourceResponse for the content.

The prefix path used shouldn't be a prefix of a real web path. Thus, in case of having a URL that matches a registered prefix path but the requested asset cannot be found or opened a WebResourceResponse object with a null InputStream will be returned instead of null. This saves the time of falling back to network and trying to resolve a path that doesn't exist. A WebResourceResponse with null InputStream will be received as an HTTP response with status code 404 and no body.

This method should be invoked from within android.webkit.WebViewClient#shouldInterceptRequest(android.webkit.WebView, String).

Parameters
url String: the URL string to process.
Return
WebResourceResponse?: WebResourceResponse if the request URL matches a registered URL, null otherwise.