jaregier
jaregier

Reputation: 121

Android Webview crashes on Samsung & Android 11

This is the stacktrace we are seeing on Firebase:

Fatal Exception: java.lang.RuntimeException
Using WebView from more than one process at once with the same data directory is not supported. https://crbug.com/558377 : Current process [Our package] (pid 28562), lock owner [Our package] (pid 13324)
org.chromium.android_webview.AwDataDirLock.b (AwDataDirLock.java:27)
as0.i (as0.java:30)
as0.b (as0.java:17)
as0.k (as0.java:2)
com.android.webview.chromium.WebViewChromiumFactoryProvider.g (WebViewChromiumFactoryProvider.java:2)
com.android.webview.chromium.WebViewChromium.init (WebViewChromium.java:14)
android.webkit.WebView.<init> (WebView.java:435)
android.webkit.WebView.<init> (WebView.java:355)
android.webkit.WebView.<init> (WebView.java:337)
android.webkit.WebView.<init> (WebView.java:324)
android.webkit.WebView.<init> (WebView.java:314)
[Our code initializing the webview]
android.os.Handler.handleCallback (Handler.java:938)
android.os.Handler.dispatchMessage (Handler.java:99)
android.os.Looper.loop (Looper.java:246)
android.app.ActivityThread.main (ActivityThread.java:8506)
java.lang.reflect.Method.invoke (Method.java)
com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:602)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1130)

We have not been able to reproduce this, but we get hundreds/thousands of these crashes on Firebase. What is weird is that 99% of the crashes are happening on Samsung devices running Android 11. Our app is also a single process app, so it should not be running multiple processes. I've posted in the chromium issue tracker, but it seems the bug is more with the Samsung OS than with the webview itself, so I thought I'd post here as well.

It seems that some users have a process of our app running for hours, holding onto this Webview lock. However when they try to open our app, it is starting a new process instead of the existing process, and causing a crash.

I'm trying to gain more information: Does anyone have any insight into why this would be happening specifically on Samsung Android 11? Is there something our app, or our users, can do to mitigate this issue? Has anyone else faced this problem and found a workaround?

Upvotes: 9

Views: 4911

Answers (3)

Harsh
Harsh

Reputation: 1

Do not remember exact stacktrace, but I had similar problem with WebView crashing app when returning to app on some devices.

override fun onResume() {
        webView = WebView(activity)
        //... init webview
        webViewLayout.addView(webView)
   }
   override fun onStop() {
        webViewLayout.removeView(webView)
        webView?.destroy()
        webView = null
    }

You can move creating to onCreate() and destroying to onDestroy(), if you don't need to refresh layout when returning to screen to save resources. In our case it is required to content be always updated when returning to screen.

Upvotes: 0

Sujith S Manjavana
Sujith S Manjavana

Reputation: 1596

Try adding this to the onCreate of your application class before initializing any WebView related stuff such as AdMob,

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            String processName = getProcessName();
            if (!getPackageName().equals(processName)) {
                WebView.setDataDirectorySuffix(processName);
            }
        }

//AdMob creates a new process.

Upvotes: 2

David Vareka
David Vareka

Reputation: 262

Do not remember exact stacktrace, but I had similar problem with WebView crashing app when returning to app on some devices. After no solution worked I tried to remove WebView definition from XML and moved WebView inicialization into Activity (Kotlin) and destroying manually in onStop() method. Did not find information, about what was the problem, but solved my problem.

   override fun onResume() {
        webView = WebView(activity)
        //... init webview
        webViewLayout.addView(webView)
   }
   override fun onStop() {
        webViewLayout.removeView(webView)
        webView?.destroy()
        webView = null
    }

EDIT: Thank you kingston for pointing my mistake. Misread creation of webview in code, which does not make sence to do in onCreate() with destroying onStop.

You can move creating to onCreate() and destroying to onDestroy(), if you don't need to refresh layout when returning to screen to save resources. In our case it is required to content be always updated when returning to screen.

Upvotes: 1

Related Questions