Android Dev Summit, October 23-24: two days of technical content, directly from the Android team. Sign-up for livestream updates.

WebViewAssetLoader

class WebViewAssetLoader
kotlin.Any
   ↳ androidx.webkit.WebViewAssetLoader

Helper class to load local files including application's static assets and resources using http(s):// URLs inside a android.webkit.WebView class. Loading local files using web-like URLs instead of "file://" 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 invoked on a different thread than 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 files should only be hosted on domains your organization owns (at paths reserved for this purpose) or the default domain reserved for this: appassets.androidplatform.net.

A typical usage would be like:

final WebViewAssetLoader assetLoader = new WebViewAssetLoader.Builder()
               .addPathHandler("/assets/", new AssetsPathHandler(this))
               .addPathHandler("/res/", new ResourcesPathHandler(this))
               .build();
 
      webView.setWebViewClient(new WebViewClient() {
          @Override
          public WebResourceResponse shouldInterceptRequest(WebView view,
                                           WebResourceRequest request) {
              return assetLoader.shouldInterceptRequest(request.getUrl());
          }
      });
      // Assets are hosted under http(s)://appassets.androidplatform.net/assets/... .
      // 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("https://appassets.androidplatform.net/assets/www/index.html");
 
  

Summary

Nested classes

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

A builder class for constructing WebViewAssetLoader objects.

Handler class to open files from application internal storage.

abstract

A handler that produces responses for a registered path.

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

Constants

static String

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

Public methods

WebResourceResponse?
shouldInterceptRequest(@NonNull url: Uri)

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

Constants

DEFAULT_DOMAIN

static val DEFAULT_DOMAIN: String

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

It is used by default unless the user specified a different domain.

Value: "appassets.androidplatform.net"

Public methods

shouldInterceptRequest

@WorkerThread @Nullable fun shouldInterceptRequest(@NonNull url: Uri): WebResourceResponse?

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

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

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