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

最近帮一个内部的项目解决问题,他们在多进程使用webview的时候碰到了崩溃问题

问题:

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

原因:

Android 9 prohibit sharing WebView data directory among multiple processes
add below code in your mainApplication file

因为Android P行为变更,不可多进程使用同一个目录webView,需要为不同进程webView设置不同目录

解决方法:

为不同进程设置不同的目录即可解决问题

虽然在程序运行阶段有很多时间去为其他子进程的webview去设置目录,但是需要处理好一个进程里只能设置一次,否则会出现以下报错导致崩溃

Using WebView from more than one process at once with the same data directory is not supported

所以最终的解决方法是建议在application里的attachBaseContext或onCreate去做设置,来让进程子进程只执行一次【有小伙伴在四大组件里去设置,搞不好就会被多次执行】

    public static void dealH5DataDirectory(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            // 安卓9.0后不允许多进程使用同一个数据目录
            try {
                WebView.setDataDirectorySuffix("进程名");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 获取当前进程名
     * @return 进程名
     */
    public static String getCurrentProcessName(Context context) {
        int pid = android.os.Process.myPid();
        ActivityManager mActivityManager = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningAppProcessInfo appProcess : mActivityManager
                .getRunningAppProcesses()) {
            if (appProcess.pid == pid) {
                return appProcess.processName;
            }
        }
        return null;
    }

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值