Android 9.0及以上版本中,关于多进程问题对于WebView的影响

"Android P 以及之后版本不支持同时从多个进程使用具有相同数据目录的WebView"

以上为官方给出的说明,用白话来说就是,一个app中,如果有多个进程A、B、C,并且在A、B、C进程都是用到WebView的话,在Android P 以及之后的版本中运行会报错:

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

这行代码翻译过来的意思就是:不支持同时使用多个进程中具有相同数据目录的WebView。

针对这个问题,谷歌也给出了解决方案,代码很简单:在初始化的时候,需要为其它进程webView设置目录

//Android P 以及之后版本不支持同时从多个进程使用具有相同数据目录的WebView
    //为其它进程webView设置目录

    @RequiresApi(api = 28)
    public void webviewSetPath(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            String processName = getProcessName(context);
            if (!MainProcessName.equals(processName)) {//判断不等于默认进程名称
                WebView.setDataDirectorySuffix(processName);
            }
        }
    }

    public String getProcessName(Context context) {
        if (context == null) return null;
        ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningAppProcessInfo processInfo : manager.getRunningAppProcesses()) {
            if (processInfo.pid == android.os.Process.myPid()) {
                return processInfo.processName;
            }
        }
        return null;
    }

webviewSetPath()的调用一定是在进程初始化的时候调用,比如Application中进行调用,并且这行代码需要在其他的SDK等等初始化之前就要调用,否则会报其他的错误。

这个坑,你需要认真对待!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章