Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

java.lang.RuntimeException: Using WebView from more than one process at once with the same data directory is not supported

panwj edited this page Nov 15, 2021 · 2 revisions

官方给出的说明是:Android P 以及之后版本不支持同时从多个进程使用具有相同数据目录的WebView.

如果在多个进程中使用具有相同目录的WebView就会报以下错误:

Caused by: java.lang.RuntimeException: Using WebView from more than one process at once with the same data directory is not supported

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

public void setWebViewPath() {
 //java.lang.RuntimeException: Using WebView from more than one process at once with the same data directory is not supported
 //android 9.0开始不支持从多个进程同时使用同一数据目录的WebView, 针对这个问题,谷歌也给出了解决方案:在初始化的时候,需要为其它进程webView设置不同目录
 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
 String processName = getProcessName();
 if (!getPackageName().equals(processName)) {//判断不等于默认进程名称
 WebView.setDataDirectorySuffix(processName);
 }
 }
 }

遗憾的是,经过上面的代码优化后,仍然还有该问题,最终尝试按掘金上的一篇文章(有效解决WebView多进程崩溃(续))提到的解决方案做了处理,经过一段时间观察,统计平台里没有再出现该WebView错误。

注意事项

如果项目引入一些第三方sdk(比如google广告sdk), 那么这些第三方sdk可能会引入该WebView问题,尤其需要注意对不同进程下的WebView设置不同数据目录的操作需要放到所有sdk初始化前,这里建议放到Application attach时(attachBaseContext(Context base)方法里,因为attachBaseContext()方法调用时机早于ContentProvider onCreate(),ContentProvider onCreate()早于Application onCreate(), 而一些第三方的sdk初始化可能在ContentProvider里)。

参考:

Clone this wiki locally

AltStyle によって変換されたページ (->オリジナル) /