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;

    }

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容