具体报错:
2021-12-21 08:41:54.489 19437-19437/com.maniu.mn_vip_webview E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.maniu.mn_vip_webview:remoteweb, PID: 19437
java.lang.RuntimeException: Unable to create application com.maniu.mn_vip_webview.BaseApplication: java.lang.RuntimeException: Using WebView from more than one process at once with the same data directory is not supported. https://crbug.com/558377 : Lock owner com.maniu.mn_vip_webview (pid 19339)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:7367)
at android.app.ActivityThread.access$2400(ActivityThread.java:308)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2295)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:219)
at android.app.ActivityThread.main(ActivityThread.java:8349)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1055)
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 : Lock owner com.maniu.mn_vip_webview (pid 19339)
原因是 Android P针对 WebView在不同进程下无法访问非自己进程中的webview目录
解决办法:
原来的代码
public class BaseApplication extends Application {
public static WebView webView;
@Override
public void onCreate() {
super.onCreate();
webView = new WebView(this);
webView.addJavascriptInterface(this,"android");//添加js监听 这样html就能调用客户端
... ...
}
只需要在 webView = new WebView(this); 前面加一个判断
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
String processName = getProcessName();
WebView.setDataDirectorySuffix(processName);
}
更改后的代码
public class BaseApplication extends Application {
public static WebView webView;
@Override
public void onCreate() {
super.onCreate();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
String processName = getProcessName();
WebView.setDataDirectorySuffix(processName);
}
webView = new WebView(this);
webView.addJavascriptInterface(this,"android");//添加js监听 这样html就能调用客户端
... ...