Caused by: java.lang.RuntimeException: Using WebView from more than one process at once with the sam

转自:https://blog.csdn.net/lvshuchangyin/article/details/89446629

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设置目录


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

Application onCrete中

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {

webviewSetPath(this);

        }

privatestaticfinalString TAG ="MyApplication";

@RequiresApi(api = 28)

publicvoidwebviewSetPath(Context context){

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {

            String processName = getProcessName(context);

if(!getApplicationContext().getPackageName().equals(processName)) {//判断不等于默认进程名称

                WebView.setDataDirectorySuffix(processName);

            }

        }

    }

publicStringgetProcessName(Context context){

if(context ==null)returnnull;

        ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);

for(ActivityManager.RunningAppProcessInfo processInfo : manager.getRunningAppProcesses()) {

if(processInfo.pid == android.os.Process.myPid()) {

returnprocessInfo.processName;

            }

        }

returnnull;

    }

你可能感兴趣的:(Caused by: java.lang.RuntimeException: Using WebView from more than one process at once with the sam)