public final class WebViewAssetLoader
extends Object

   ↳ androidx.webkit.WebViewAssetLoader

Helper class to load local files including application's static assets and resources using http(s):// URLs inside a 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 WebViewClient.shouldInterceptRequest(WebView, WebResourceRequest), 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))

 webView.setWebViewClient(new WebViewClient() {
     public WebResourceResponse shouldInterceptRequest(WebView view,
                                      WebResourceRequest request) {
         return assetLoader.shouldInterceptRequest(request.getUrl());

     @SuppressWarnings("deprecation") // for API < 21
     public WebResourceResponse shouldInterceptRequest(WebView view,
                                      WebResourceRequest request) {
         return assetLoader.shouldInterceptRequest(Uri.parse(request));

 WebSettings webViewSettings = webView.getSettings();
 // Setting this off for security. Off by default for SDK versions >= 16.
 // Off by default, deprecated for SDK versions >= 30.
 // Keeping these off is less critical but still a good idea, especially if your app is not
 // using file:// or content:// URLs.

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


Nested classes

class WebViewAssetLoader.AssetsPathHandler

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

class WebViewAssetLoader.Builder

A builder class for constructing WebViewAssetLoader objects. 

class WebViewAssetLoader.InternalStoragePathHandler

Handler class to open files from application internal storage. 

interface WebViewAssetLoader.PathHandler

A handler that produces responses for a registered path. 

class WebViewAssetLoader.ResourcesPathHandler

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



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

Public methods

WebResourceResponse shouldInterceptRequest(Uri url)

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

Inherited methods