Stay organized with collections Save and categorize content based on your preferences.

WebViewAssetLoader

public final class 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 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))
         .build();

webView.setWebViewClient(new WebViewClientCompat() {
    @Override
    @RequiresApi(21)
    public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
        return assetLoader.shouldInterceptRequest(request.getUrl());
    }

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

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

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

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

public final class WebViewAssetLoader.Builder

A builder class for constructing WebViewAssetLoader objects.

Handler class to open files from application internal storage.

A handler that produces responses for a registered path.

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

Constants

static final String
DEFAULT_DOMAIN = "appassets.androidplatform.net"

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

Public methods

@Nullable WebResourceResponse

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

Constants

DEFAULT_DOMAIN

public static final String DEFAULT_DOMAIN = "appassets.androidplatform.net"

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.

Public methods

shouldInterceptRequest

@WorkerThread
public @Nullable WebResourceResponse shouldInterceptRequest(@NonNull Uri url)

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 shouldInterceptRequest.

Parameters
@NonNull Uri url

the URL to process.

Returns
@Nullable WebResourceResponse

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