Fatal Exception: java.lang.RuntimeException:Using WebView from more than one process at once with the same data directory is not supported
Asked Answered
M

4

35

1.When I in Fragment onCreateView method inflater.inflate(webview_layout, container, false) on Android 9 may Crash with blow log:


    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
    at jO.b(PG:102)
    at jQ.run(PG:3)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:226)
    at android.app.ActivityThread.main(ActivityThread.java:7210)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:499)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:961)

2.I try add blow code in Application onCreate method

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

but some Android mobile phone alse Crash with same reason,and I don't use webview with multi process, then I try add this code in Fragment onCreateView before inflater.inflate(webview_layout, container, false)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
    String processName = getProcessName();
    try {
        WebView.setDataDirectorySuffix(processName);
    } catch (Throwable e) {
        // ignore
    }
}

But I also get some the same crash report in PCAM10\PCEM00\PCAT10... and I can't reappear this crash local.

Is also some other reason with this Crash?

Mycorrhiza answered 29/5, 2020 at 5:56 Comment(2)
Getting same error in some of device.Mucker
Any update about this case?Subduct
A
13

using this code in Application class before initializing Admob solve my problem:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
        val process = getProcessName()
        if (packageName != process) WebView.setDataDirectorySuffix(process)
    }

MobileAds.initialize(this)
Annamaeannamaria answered 11/2, 2021 at 14:26 Comment(0)
I
2

There is a new effective API to get process name for API 28 onwards. [https://developer.android.com/reference/android/app/Application.html#getProcessName()][1]

As mentioned in google documentation, from API 28, it's clear that WebView running in multiprocess can not share the same data directory.

This means that different processes in the same application cannot directly share WebView-related data, since the data directories must be distinct.

Another thing, please check if you are initializing any process in the Application class or not. Reference link : Android Pie (9.0) WebView in multi-process

Inexperience answered 23/12, 2020 at 11:10 Comment(0)
C
1

If you have two or more different processes for your app and services then you could just disable WebView usage in the process which doesnt intend to use WebView WebView.disableWebView()

public void onCreate() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
        WebView.disableWebView();
    }
    ...
}

In that case exception will be thrown if a WebView is created or any other methods in the android.webkit package are used by process. But the "java.lang.RuntimeException:Using WebView from more than one process...." exception will gone

Crucial answered 11/8, 2021 at 3:52 Comment(0)
M
0

[Android] Below code help to solve this issue. I add process id in suffix directory name

  private fun setupWebView() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
      getAppProcessName()?.let {
        WebView.setDataDirectorySuffix(it)
      }
    }
  }

  private fun getAppProcessName(): String? {
    val pid = Process.myPid()
    val manager = getSystemService(Context.ACTIVITY_SERVICE) as? ActivityManager
    return manager?.runningAppProcesses?.filterNotNull()?.firstOrNull { it.pid == pid }?.processName + pid
  }
Malefic answered 8/4 at 2:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.