Preloading WebView libraries on Android 5.0+

1,735 views
Skip to first unread message

Kostya Vasilyev

unread,
Nov 20, 2017, 11:18:06 AM11/20/17
to android-webview-dev
Hi,

Since 5.0, Android WebView is provided by a separate "app".

The first time an app inflates or creates a WebView, there is a delay (WebView code loading native libraries, that sort of thing).

This delay can be as large as 200 ms (Nexus 5, 5.1) or even 300 ms (Samsung S6, 7.0) or larger - which is a very noticeable "freeze" of the app's UI, and in particular can ruin animations.

Is there a way to preload (pre-initialize) WebView on 5.0+ without blocking the UI thread?

I've tried doing "new WebView(appContext)" on a worker thread, and although that appeared to work...

... as soon as my code tried to inflate another WebView object back on the UI thread, the app crashed (stack trace below).

The error message in the exception is misleading too - this code was running on the UI thread, so apparently the original "new WebView(appContext)" on worker thread kept a reference to that worker thread, and assumed it was the UI thread, which it wasn't.

But in any case, this is a crash, so preloading by "new WebView" on a worker thread does not work.

Is there a better, working, technique for this?

---
 Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class org.kman.AquaMail.view.MessageWebView
     at android.view.LayoutInflater.createView(LayoutInflater.java:645)
     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:764)
     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
     at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
     ... 15 more
 Caused by: java.lang.reflect.InvocationTargetException
     at java.lang.reflect.Constructor.newInstance(Native Method)
     at android.view.LayoutInflater.createView(LayoutInflater.java:619)
     ... 18 more
 Caused by: java.lang.IllegalStateException: Calling View methods on another thread than the UI thread.
     at com.android.webview.chromium.WebViewChromium.createThreadException(WebViewChromium.java:66)
     at com.android.webview.chromium.WebViewChromium.checkThread(WebViewChromium.java:76)
     at com.android.webview.chromium.WebViewChromium.init(WebViewChromium.java:31)
     at android.webkit.WebView.<init>(WebView.java:606)
     at android.webkit.WebView.<init>(WebView.java:542)
     at android.webkit.WebView.<init>(WebView.java:525)
     at android.webkit.WebView.<init>(WebView.java:512)
     at org.kman.AquaMail.view.MessageWebView.<init>(MessageWebView.java:34)
---

-- K

Bo Liu

unread,
Nov 20, 2017, 12:38:24 PM11/20/17
to Kostya Vasilyev, android-webview-dev
Nothing great. Webview itself has strict thread checks as you've found out. And there is no explicit API to "warm up" webview.

Maybe a workaround is to create the CookieManager which is documented to be thread safe, and currently it loads the native library without other initialization. So it preloads some things, but everything.

--
You received this message because you are subscribed to the Google Groups "android-webview-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-webview-dev+unsub...@chromium.org.
To post to this group, send email to android-webview-dev@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/android-webview-dev/5ab1782f-9245-45f8-9a84-39f61a1bb022%40chromium.org.

Toby Sargeant

unread,
Nov 20, 2017, 12:48:36 PM11/20/17
to Bo Liu, Kostya Vasilyev, android-webview-dev
WebView.findAddress would also work (causes native code to load, and does JNI init - which is slightly less than CookieManager).

I think that CookieManager is unsafe in some circumstances? Torne would remember the details.

To unsubscribe from this group and stop receiving emails from it, send an email to android-webview-dev+unsubscribe...@chromium.org.

--
You received this message because you are subscribed to the Google Groups "android-webview-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-webview-dev+unsub...@chromium.org.
To post to this group, send email to android-webview-dev@chromium.org.

Bo Liu

unread,
Nov 20, 2017, 1:10:07 PM11/20/17
to Toby Sargeant, Kostya Vasilyev, android-webview-dev
findAddress (or any webview static method) calls ensureChromiumStartedLocked(true), which does the full initialization on the main thread, and blocks the calling thread if need be. That's not much different from new-ing a webview on the main thread.

Torne (Richard Coles)

unread,
Nov 20, 2017, 1:20:22 PM11/20/17
to bo...@chromium.org, Toby Sargeant, Kostya Vasilyev, android-webview-dev
Calling static methods on a background thread does do the code loading on the calling thread, which includes much of the disk IO; while Bo's correct that it does ultimately post tasks to the main thread, it should still reduce the time the main thread is blocked quite a bit. We normally suggest WebSettings.getDefaultUserAgentString() as it doesn't have any other side effects besides causing webview to initialise.

To unsubscribe from this group and stop receiving emails from it, send an email to android-webview...@chromium.org.
To post to this group, send email to android-w...@chromium.org.

--
You received this message because you are subscribed to the Google Groups "android-webview-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-webview...@chromium.org.
To post to this group, send email to android-w...@chromium.org.

--
You received this message because you are subscribed to the Google Groups "android-webview-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-webview...@chromium.org.
To post to this group, send email to android-w...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/android-webview-dev/CAPCJOUpGKGWZBaGzRCXbmo9g1FuyoZKWRVmc1cBYitT_yiA%3DJQ%40mail.gmail.com.

Kostya Vasilyev

unread,
Nov 20, 2017, 2:35:30 PM11/20/17
to android-webview-dev, bo...@chromium.org, tobi...@google.com, kman...@gmail.com
Richard,

WebSettings.getDefaultUserAgent(appContext)

on a worker thread worked beautifully, moved about 3/4 of the total delay to the worker thread, and everything normal back on the UI thread.

Thanks much!

Is it considered safe with any WebView version assuming Android 5.0 - or would you recommend checking WebView package version to be safe, to only do this for say version 60 (or whatever) and newer?

-- K
To unsubscribe from this group and stop receiving emails from it, send an email to android-webview-dev+unsub...@chromium.org.

--
You received this message because you are subscribed to the Google Groups "android-webview-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-webview-dev+unsub...@chromium.org.

To post to this group, send email to android-w...@chromium.org.

--
You received this message because you are subscribed to the Google Groups "android-webview-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-webview-dev+unsub...@chromium.org.

Torne (Richard Coles)

unread,
Nov 20, 2017, 2:38:21 PM11/20/17
to Kostya Vasilyev, android-webview-dev, bo...@chromium.org, tobi...@google.com
It's safe to do this on any version, even before 5.0. On earlier versions it'll just be a no-op, since webview is already loaded.

To unsubscribe from this group and stop receiving emails from it, send an email to android-webview...@chromium.org.

--
You received this message because you are subscribed to the Google Groups "android-webview-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-webview...@chromium.org.

--
You received this message because you are subscribed to the Google Groups "android-webview-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-webview...@chromium.org.
--
You received this message because you are subscribed to the Google Groups "android-webview-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-webview...@chromium.org.

To post to this group, send email to android-w...@chromium.org.
Reply all
Reply to author
Forward
0 new messages