In the last article, I introduced an effective solution to the multi-process crash of WebView, but two versions later, the problem has resurfaced. What is going on here?

“Liangzhou Ci” grape wine luminous cup, lute to drink immediately urge. Drunkenly lying on the battlefield jun Mo smile, ancient war a few people back? – Wang Han

Question why

The solution provided in the previous article has been implemented and achieved good results. However, the problem appears again recently. Using the debug package to check the WebView data directory, it is found that the system has added the process name suffix by default. Use huawei mate20X test call WebView. SetDataDirectorySuffix custom suffix is not effective, will specify the default mandatory suffix for the process name, In addition, it is found that some Huawei mobile phones directly change the webview directory name app_webview to app_hWS_webview.

Oppo PHONE Oppo PDNM00

Huawei mate30pro

Honor 10 Honor HRY AL00A

The solution

To sum up, we need to traverse the possible file paths for different mobile systems. The latest solution code is as follows:

private static void handleWebviewDir(Context context) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) { return; } try { Set<String> pathSet = new HashSet<>(); String suffix = ""; String dataPath = context.getDataDir().getAbsolutePath(); String webViewDir = "/app_webview"; String huaweiWebViewDir = "/app_hws_webview"; String lockFile = "/webview_data.lock"; String processName = getProcessName(context); if (! Suffix = textutils. equals(context.getPackagename (), processName)) {suffix = textutils.isEmpty (processName)? context.getPackageName() : processName; WebView.setDataDirectorySuffix(suffix); suffix = "_" + suffix; pathSet.add(dataPath + webViewDir + suffix + lockFile); if (RomUtils.INSTANCE.checkIsHuaweiRom()) { pathSet.add(dataPath + huaweiWebViewDir + suffix + lockFile); }}else{// Suffix = "_" + processName; pathSet.add(dataPath + webViewDir + lockFile); Add (dataPath + webViewDir + suffix + lockFile); / / the system automatically adds the process name suffix if (RomUtils. INSTANCE. CheckIsHuaweiRom ()) {/ / part huawei mobile phone changed the webview directory name pathSet. The add (+ huaweiWebViewDir dataPath + lockFile); pathSet.add(dataPath + huaweiWebViewDir + suffix + lockFile); } } for (String path : pathSet) { File file = new File(path); if (file.exists()) { tryLockOrRecreateFile(file); break; } } } catch (Exception e) { e.printStackTrace(); } } @TargetApi(Build.VERSION_CODES.P) private static void tryLockOrRecreateFile(File file) { try { FileLock tryLock = new RandomAccessFile(file, "rw").getChannel().tryLock(); if (tryLock ! = null) { tryLock.close(); } else { createFile(file, file.delete()); } } catch (Exception e) { e.printStackTrace(); boolean deleted = false; if (file.exists()) { deleted = file.delete(); } createFile(file, deleted); } } private static void createFile(File file, boolean deleted){ try { if (deleted && ! file.exists()) { file.createNewFile(); } } catch (Exception e) { e.printStackTrace(); }}Copy the code

Search “Yu Wei Guo” on wechat to follow me