Skip to content

Instantly share code, notes, and snippets.

@thoinv
Created February 10, 2023 01:12
Show Gist options
  • Save thoinv/b6dfaf8c79a30a2e915c89e4b26ebb4c to your computer and use it in GitHub Desktop.
Save thoinv/b6dfaf8c79a30a2e915c89e4b26ebb4c to your computer and use it in GitHub Desktop.
[WebView multi process error Solution] Android 9 prohibit sharing WebView data directory among multiple processes add below code in your main #webview #solution #android
public void onCreate() {
...
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
String processName = getProcessName(this);
String packageName = this.getPackageName();
if (!packageName.equals(processName)) {
WebView.setDataDirectorySuffix(processName);
}
}
}
private String getProcessName(Context context) {
if (context == null) return null;
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningAppProcessInfo processInfo : manager.getRunningAppProcesses()) {
if (processInfo.pid == android.os.Process.myPid()) {
return processInfo.processName;
}
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment