Register now for Android Dev Summit 2019!

WebViewAssetLoader

public class WebViewAssetLoader
extends Object

java.lang.Object
   ↳ androidx.webkit.WebViewAssetLoader


Helper class to enable accessing the application's static assets and resources under an http(s):// URL to be loaded by 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 WebViewClient.shouldInterceptRequest(WebView, WebResourceRequest), 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

class WebViewAssetLoader.Builder

A builder class for constructing WebViewAssetLoader objects. 

Constants

String KNOWN_UNUSED_AUTHORITY

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

Public methods

Uri getAssetsHttpPrefix()

Get the HTTP URL prefix under which assets are hosted.

Uri getAssetsHttpsPrefix()

Get the HTTPS URL prefix under which assets are hosted.

Uri getResourcesHttpPrefix()

Get the HTTP URL prefix under which resources are hosted.

Uri getResourcesHttpsPrefix()

Get the HTTPS URL prefix under which resources are hosted.

WebResourceResponse shouldInterceptRequest(WebResourceRequest request)

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

WebResourceResponse shouldInterceptRequest(String url)

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

Inherited methods

Constants

KNOWN_UNUSED_AUTHORITY

public static final String KNOWN_UNUSED_AUTHORITY

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.

Constant Value: "appassets.androidplatform.net"

Public methods

getAssetsHttpPrefix

public Uri getAssetsHttpPrefix ()

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/".

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

getAssetsHttpsPrefix

public Uri getAssetsHttpsPrefix ()

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/".

Returns
Uri the HTTPS URL prefix under which assets are hosted.

getResourcesHttpPrefix

public Uri getResourcesHttpPrefix ()

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/".

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

getResourcesHttpsPrefix

public Uri getResourcesHttpsPrefix ()

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/".

Returns
Uri the HTTPs URL prefix under which resources are hosted.

shouldInterceptRequest

public WebResourceResponse shouldInterceptRequest (WebResourceRequest request)

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 WebViewClient.shouldInterceptRequest(android.webkit.WebView, WebResourceRequest).

Parameters
request WebResourceRequest: the WebResourceRequest to process.

Returns
WebResourceResponse WebResourceResponse if the request URL matches a registered url, null otherwise.

shouldInterceptRequest

public WebResourceResponse shouldInterceptRequest (String url)

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 WebViewClient.shouldInterceptRequest(android.webkit.WebView, String).

Parameters
url String: the URL string to process.

Returns
WebResourceResponse WebResourceResponse if the request URL matches a registered URL, null otherwise.