轉(zhuǎn)自:https://blog.csdn.net/lvshuchangyin/article/details/89446629
Android P 以及之后版本不支持同時從多個進程使用具有相同數(shù)據(jù)目錄的WebView"
以上為官方給出的說明,用白話來說就是后雷,一個app中季惯,如果有多個進程A吠各、B、C勉抓,并且在A贾漏、B、C進程都是用到WebView的話藕筋,在Android P 以及之后的版本中運行會報錯:
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
這行代碼翻譯過來的意思就是:不支持同時使用多個進程中具有相同數(shù)據(jù)目錄的WebView纵散。
針對這個問題,谷歌也給出了解決方案念逞,代碼很簡單:在初始化的時候,需要為其它進程webView設(shè)置目錄
Application onCrete中
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
webviewSetPath(this);
? ? ? ? }
privatestaticfinalString TAG ="MyApplication";
@RequiresApi(api = 28)
publicvoidwebviewSetPath(Context context){
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
? ? ? ? ? ? String processName = getProcessName(context);
if(!getApplicationContext().getPackageName().equals(processName)) {//判斷不等于默認(rèn)進程名稱
? ? ? ? ? ? ? ? WebView.setDataDirectorySuffix(processName);
? ? ? ? ? ? }
? ? ? ? }
? ? }
publicStringgetProcessName(Context context){
if(context ==null)returnnull;
? ? ? ? ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
for(ActivityManager.RunningAppProcessInfo processInfo : manager.getRunningAppProcesses()) {
if(processInfo.pid == android.os.Process.myPid()) {
returnprocessInfo.processName;
? ? ? ? ? ? }
? ? ? ? }
returnnull;
? ? }