Android
WebView
localStorage
Mobile App Development
JavaScript

Android WebView localStorage: enable DOM storage

Codemia Knowledge Hub ·

localStorage works in Android WebView only after you enable DOM storage with settings.domStorageEnabled = true. Pages reach localStorage only from script, so enable JavaScript as well. Leaving DOM storage off is the most common reason localStorage appears to be missing.

Enabling JavaScript and DOM storage

localStorage is part of the browser's DOM storage APIs, so the WebView settings must allow it. A minimal Kotlin setup:

kotlin
1import android.os.Bundle
2import android.webkit.WebView
3import androidx.appcompat.app.AppCompatActivity
4
5class MainActivity : AppCompatActivity() {
6    override fun onCreate(savedInstanceState: Bundle?) {
7        super.onCreate(savedInstanceState)
8
9        val webView = WebView(this)
10        setContentView(webView)
11
12        webView.settings.javaScriptEnabled = true
13        webView.settings.domStorageEnabled = true
14
15        webView.loadUrl("https://example.com")
16    }
17}

With domStorageEnabled left as false, web code that expects localStorage may fail silently or behave as if storage does not exist. Enabling DOM storage without JavaScript is not enough, because no script runs to use it.

Checking that storage works

A small page script confirms it:

html
1<script>
2  localStorage.setItem("theme", "dark");
3  console.log(localStorage.getItem("theme"));
4</script>

In a correctly configured WebView, the stored value survives page reloads for the same origin.

Storage is scoped by origin

localStorage is not one bucket for the whole WebView. It is scoped to the page origin: protocol, host and port. Data written by https://app.example.com is separate from https://api.example.com and from file:// content.

If a hybrid app seems to lose its data, confirm it loads the same origin every time. Debugging one URL while the app actually loads another origin can hide the mismatch.

file:// pages need extra care

Older hybrid apps often loaded HTML from file:// URLs and expected storage to behave like a normal website. Local file origins are not the same as a hosted origin, which makes this awkward. file:// access is also off by default from Android 11 (API 30), where allowFileAccess defaults to false, and allowFileAccessFromFileURLs and allowUniversalAccessFromFileURLs are deprecated.

The recommended fix is WebViewAssetLoader from androidx.webkit. It serves bundled files from a real https origin, https://appassets.androidplatform.net, so storage is scoped to that one stable origin:

kotlin
1import androidx.webkit.WebViewAssetLoader
2import android.webkit.WebResourceRequest
3import android.webkit.WebResourceResponse
4import android.webkit.WebViewClient
5
6val assetLoader = WebViewAssetLoader.Builder()
7    .addPathHandler("/assets/", WebViewAssetLoader.AssetsPathHandler(this))
8    .build()
9
10webView.webViewClient = object : WebViewClient() {
11    override fun shouldInterceptRequest(
12        view: WebView,
13        request: WebResourceRequest
14    ): WebResourceResponse? = assetLoader.shouldInterceptRequest(request.url)
15}
16
17// src/main/assets/index.html
18webView.loadUrl("https://appassets.androidplatform.net/assets/index.html")

When to use native storage instead

WebView localStorage is not a replacement for native persistence such as SharedPreferences, Room or files. It suits web-layer state inside the embedded page. For large app data or business-critical offline state that belongs to the Android app itself, native storage is the more robust choice.


Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms