Crash信息:
1、java.lang.RuntimeException: Using WebView from more than one process at once with the same data directory is not supported
2、java.lang.RuntimeException: Failed to create lock file /data/user/0/com.xxx/app_webview/webview_data.lock
3、java.io.FileNotFoundException: /data/user/0/com.xxx/app_webview/webview_data.lock: open failed: ENOENT (No such file or directory)
项目中开启了两个进程:
com.xxx.app.test 和 com.xxx.app.test.pushcore
错误原因:
Android P行为变更,不可多进程使用同一个目录webView,需要为不同进程webView设置不同目录
Fatal Exception: java.lang.RuntimeException: Using WebView from more than one process at once with the same data directory is not supported.
https://crbug.com/558377 : Current process com.xx.xxapp(pid 13862), lock owner com.xx.xx.xxAPP (pid 13559)
at org.chromium.android_webview.AwDataDirLock.b(AwDataDirLock.java:27)
at as0.i(as0.java:30)
at Zr0.run(Zr0.java:2)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:224)
at android.app.ActivityThread.main(ActivityThread.java:7520)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
解决方案
官方提供方案:WebView.setDataDirectorySuffix(suffix);
Android 9 prohibit sharing WebView data directory among multiple processes
add below code in your mainApplication file
protected void attachBaseContext(Context base) {
mApplicationContext = base;
webViewSetPath(this);
}
public void webViewSetPath(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
String processName = SpecialUtils.getCurProcessName(context);
if(!CommonConstant.NEW_PACKAGE_NAME.equals(processName)){
WebView.setDataDirectorySuffix(getString(processName,"这里隐藏名字,自己设置个目录"));
}
}
}
public String getString(String processName, String defValue) {
return TextUtils.isEmpty(processName) ? defValue : processName;
}
从源码分析调用链最终调用到了AwDataDirLock类中的lock方法。
public class WebViewChromiumAwInit {
protected void startChromiumLocked() {
...
AwBrowserProcess.start