29

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?

Wenlong Mo
  • 291
  • 1
  • 3
  • 6

3 Answers3

11

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)
Hadi Ahmadi
  • 1,924
  • 2
  • 17
  • 38
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

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

Anatolii Shuba
  • 4,614
  • 1
  • 16
  • 17