轉(zhuǎn)自:https://blog.csdn.net/lvshuchangyin/article/details/89446629
Android P 以及之后版本不支持同時(shí)從多個(gè)進(jìn)程使用具有相同數(shù)據(jù)目錄的WebView"
以上為官方給出的說(shuō)明,用白話來(lái)說(shuō)就是,一個(gè)app中,如果有多個(gè)進(jìn)程A、B、C,并且在A、B、C進(jìn)程都是用到WebView的話,在Android P 以及之后的版本中運(yùn)行會(huì)報(bào)錯(cuò):
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
這行代碼翻譯過(guò)來(lái)的意思就是:不支持同時(shí)使用多個(gè)進(jìn)程中具有相同數(shù)據(jù)目錄的WebView。
針對(duì)這個(gè)問(wèn)題,谷歌也給出了解決方案,代碼很簡(jiǎn)單:在初始化的時(shí)候,需要為其它進(jìn)程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)進(jì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;
? ? }