Android 使用WebView报错:Using WebView from more than one process at once with the same data directory is

Posted 胡刚2021

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 使用WebView报错:Using WebView from more than one process at once with the same data directory is相关的知识,希望对你有一定的参考价值。

具体报错:

2021-12-21 08:41:54.489 19437-19437/com.maniu.mn_vip_webview E/androidRuntime: FATAL EXCEPTION: main
    Process: com.maniu.mn_vip_webview:remoteweb, PID: 19437
    java.lang.RuntimeException: Unable to create application com.maniu.mn_vip_webview.BaseApplication: java.lang.RuntimeException: Using WebView from more than one process at once with the same data directory is not supported. https://crbug.com/558377 : Lock owner com.maniu.mn_vip_webview (pid 19339)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:7367)
        at android.app.ActivityThread.access$2400(ActivityThread.java:308)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2295)
        at android.os.Handler.dispatchMessage(Handler.java:110)
        at android.os.Looper.loop(Looper.java:219)
        at android.app.ActivityThread.main(ActivityThread.java:8349)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1055)
     Caused by: java.lang.RuntimeException: Using WebView from more than one process at once with the same data directory is not supported. https://crbug.com/558377 : Lock owner com.maniu.mn_vip_webview (pid 19339)
        

原因是 Android P针对 WebView在不同进程下无法访问非自己进程中的webview目录
解决办法:
原来的代码

public class BaseApplication extends Application 
    public static WebView webView;

    @Override
    public void onCreate() 
        super.onCreate();
        
        webView = new WebView(this);
        webView.addjavascriptInterface(this,"android");//添加js监听 这样html就能调用客户端
       ... ...
    

只需要在 webView = new WebView(this); 前面加一个判断

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) 
   	String processName = getProcessName();
    WebView.setDataDirectorySuffix(processName);

更改后的代码

public class BaseApplication extends Application 
    public static WebView webView;

    @Override
    public void onCreate() 
        super.onCreate();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) 
            String processName = getProcessName();
            WebView.setDataDirectorySuffix(processName);
        
        webView = new WebView(this);
        webView.addJavascriptInterface(this,"android");//添加js监听 这样html就能调用客户端
        ... ...

以上是关于Android 使用WebView报错:Using WebView from more than one process at once with the same data directory is的主要内容,如果未能解决你的问题,请参考以下文章

android webview在系统签名文件下报错解决

错误记录Android Studio 编译报错 ( Android Gradle plugin requires Java 11 to run. You are currently using )

错误记录Android Studio 配置 GitHub 报错 ( Can‘t login using given credentials: Request response: 401 Una )(代

错误记录Android Studio 配置 GitHub 报错 ( Can‘t login using given credentials: Request response: 401 Una )(代

flutter -webview 报错 err_cleartext_not_permitted

cordova编译crosswalk-webview插件报错的处理办法