# WebViewAssetLoader

Artifact: [androidx.webkit:webkit](https://developer.android.com/jetpack/androidx/releases/webkit) [View Source](https://cs.android.com/search?q=file:androidx/webkit/WebViewAssetLoader.java+class:androidx.webkit.WebViewAssetLoader) Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/webkit#1.1.0)

*** ** * ** ***

<br />

```
public final class WebViewAssetLoader
```

<br />

*** ** * ** ***

Helper class to load local files including application's static assets and resources using http(s):// URLs inside a `https://developer.android.com/reference/android/webkit/WebView.html` 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](https://developer.android.com/guide/topics/resources/providing-resources).

This class is expected to be used within `https://developer.android.com/reference/android/webkit/WebViewClient.html#shouldInterceptRequest(android.webkit.WebView, android.webkit.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))
         .build();

      
webView.setWebViewClient(new WebViewClientCompat() {
    @Override
    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 |
|---|
| `public final class https://developer.android.com/reference/androidx/webkit/WebViewAssetLoader.AssetsPathHandler implements https://developer.android.com/reference/androidx/webkit/WebViewAssetLoader.PathHandler` Handler class to open a file from assets directory in the application APK. |
| `public final class https://developer.android.com/reference/androidx/webkit/WebViewAssetLoader.Builder` A builder class for constructing `https://developer.android.com/reference/androidx/webkit/WebViewAssetLoader` objects. |
| `public final class https://developer.android.com/reference/androidx/webkit/WebViewAssetLoader.InternalStoragePathHandler implements https://developer.android.com/reference/androidx/webkit/WebViewAssetLoader.PathHandler` Handler class to open files from application internal storage. |
| `public interface https://developer.android.com/reference/androidx/webkit/WebViewAssetLoader.PathHandler` A handler that produces responses for a registered path. |
| `public final class https://developer.android.com/reference/androidx/webkit/WebViewAssetLoader.ResourcesPathHandler implements https://developer.android.com/reference/androidx/webkit/WebViewAssetLoader.PathHandler` Handler class to open a file from resources directory in the application APK. |

| ### Constants |
|---|---|
| `static final https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/androidx/webkit/WebViewAssetLoader#DEFAULT_DOMAIN() = "appassets.androidplatform.net"` An unused domain reserved for Android applications to intercept requests for app assets. |

| ### Public methods |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/webkit/WebResourceResponse.html` | `@https://developer.android.com/reference/androidx/annotation/WorkerThread https://developer.android.com/reference/androidx/webkit/WebViewAssetLoader#shouldInterceptRequest(android.net.Uri)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/net/Uri.html url)` Attempt to resolve the `url` to an application resource or asset, and return a `https://developer.android.com/reference/android/webkit/WebResourceResponse.html` for the content. |

## Constants

### DEFAULT_DOMAIN

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/webkit#1.1.0)

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

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/webkit#1.1.0)

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

Attempt to resolve the `url` to an application resource or asset, and return a `https://developer.android.com/reference/android/webkit/WebResourceResponse.html` for the content.

This method should be invoked from within `https://developer.android.com/reference/android/webkit/WebViewClient.html#shouldInterceptRequest(android.webkit.WebView, java.lang.String)`.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/net/Uri.html url` | the URL to process. |

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/webkit/WebResourceResponse.html` | `https://developer.android.com/reference/android/webkit/WebResourceResponse.html` if the request URL matches a registered URL, `null` otherwise. |