当前位置:网站首页>Problems of Android 9.0/p WebView multi process usage

Problems of Android 9.0/p WebView multi process usage

2020-11-07 21:28:26 Gray floating

Recently, I saw an error log statistics platform WebView The error of , I haven't encountered this error before . The error is as follows :

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

After searching for relevant information , Found to be Android P When it was released , Yes WebView The relevant usage has been changed : Multiple processes are not allowed to use the same directory WebView, Need for different processes of WebView Set up different directories .

You can see it , When our targetSdkVersion by 28 And above , And it needs to be used in multi process mode WebView When , You need to adjust accordingly , In order to correctly support .

1. Multi process WebView Use problem recurrence

First of all, we need to apply targetSdkVersion Of Api Level set to 28 And above . And then through two different processes Activity Separate use WebView Add Web page to realize .

Yes Activity The following configuration methods can be used to set up different processes :

 <activity
            android:name=".TestWebViewActivity"
            android:process=":ProcessName" />

Last , stay Android P Running on a real machine , You can reproduce the mistakes mentioned earlier .

2. Multi process WebView Use problem fix

Obviously ,webview The errors are caused by different processes , in other words Android P in the light of WebView In different processes, you can't access the process that is not your own webview Catalog .

Solve this problem , You need to be compatible with Android P The mechanism of :

public class MyApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        //  Restoration WebView Multi process loading of bug
        initWebView();
    }

    private void initWebView() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            String processName = getProcessName();
            WebView.setDataDirectorySuffix(processName);
        }
    }
}

The example code address for this problem is :

link :https://pan.baidu.com/s/1RrActtFvjQ4X9l18FMx5sQ
Extraction code :p09p 

 

版权声明
本文为[Gray floating]所创,转载请带上原文链接,感谢