在Andrid P以上的系统中,如果使用了多个进程,而且在这些进程中使用到了webview,那么你可能遇到下面的异常提示

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

java.lang.RuntimeException: Using WebView from more than one process at once with the same data directory is not supported. https://crbug.com/558377        at org.chromium.android_webview.AwBrowserProcess.b(PG:12)        at n6.m(PG:33)        at m6.run(PG:2)        at org.chromium.base.task.TaskRunnerImpl.g(PG:11)        at Nt.run(Unknown Source:2)        at android.os.Handler.handleCallback(Handler.java:873)        at android.os.Handler.dispatchMessage(Handler.java:99)        at android.os.Looper.loop(Looper.java:207)        at android.app.ActivityThread.main(ActivityThread.java:6878)        at java.lang.reflect.Method.invoke(Native Method)        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:876)

这个错误有可能导致app崩溃退出。

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

谷歌官方也是给了解决方法,就是给不同的进程中的webview设置不同的数据目录

在Applicationd类的onCreate或者onBaseContextAttached方法中加入

public void onBaseContextAttached(Context base) {    super.onBaseContextAttached(base);    initWebViewDataDirectory(this);}/*** 得到进程名称* @param context* @return*/public static String getProcessName(Context context) {    try {            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;                }            }        } catch (Exception e) {            e.printStackTrace();        }    return null;}/*** 为webView设置目录后缀* @param context*/@RequiresApi(api = Build.VERSION_CODES.P)public static void initWebViewDataDirectory(Context context) {    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {        String processName = getProcessName(context);        if (!context.getPackageName().equals(processName)) {//判断是否是默认进程名称            WebView.setDataDirectorySuffix(processName);        }    }}

写在最后,如果非必要,不要在多进程中使用webview,也尽量少修改webview的datadirectory,只在主进程初始化浏览器,否则高版本中多进程初始化webview会crash

更多相关文章

  1. Android(安卓)之 下拉框(Spinner)的简单使用
  2. Android技术之使用Handler引发的内存泄露
  3. Android:pm包管理命令使用说明。
  4. Android(安卓)Mediarecorder录制的时候屏蔽掉声音
  5. Eclipse中使用Ant打Android包报错及解决方案
  6. 通过网络使用ADB ( Connect to android with ADB over TCP )
  7. 箭头函数的基础使用
  8. NPM 和webpack 的基础使用
  9. Python list sort方法的具体使用

随机推荐

  1. React教程:组件,Hooks和性能 [每日前端夜话
  2. 7个开放式的前端面试题 [每日前端夜话0x2
  3. 提高页面的加载速度的几个小技巧[每日前
  4. 8 个给前端的顶级 VS Code 扩展插件 [每
  5. 在 JavaScript 中优雅的提取循环内的数据
  6. 必须要会的 50 个React 面试题(上)[每日前
  7. C语言的for循环以及一些练习
  8. 必须要会的 50 个React 面试题(下)[每日前
  9. 今天记的笔记
  10. 2021.1.19