Webview data directory for Android 9 (Pie)

Arjun Singh
2 min readJul 6, 2019

From Crashlytics report, i got to know that my app is crashing on Android 9 devices. After checking stacktrace log, it was related to data directory of webview running in multiprocess . Webview data directory store cookies, HTTP caches, and other persistent and temporary storage related to web browsing.

java.lang.IllegalStateException: Can't set data directory suffix: WebView already initialized

After reading Google documentation, It was clear that from android 9 webview running in multiprocess can not share same data directory so we have to make sure that webview in each process must have it’s own data directory which we can set by calling Webview.setDataDirectorySuffix().

To fix this issue, I have updated my application to check if my current process is other then my main process so i will set data directory to webview. By default, applications component run in a single process until and unless you run component in another process. You can specify the process in which your component should run by android:process tag.

Below is the code to check the process and assign data directory to webview.

package com.arjun.webviewdemo;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.os.Build;
import android.webkit.WebView;

public class MyApplication extends Application {

@Override
public void onCreate() {
super.onCreate();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {

String processName = getProcessName(this);
if (!"com.arjun.webviewdemo".equals(processName)) {
WebView.setDataDirectorySuffix(processName);
}
}


}

public 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;
}
}

Cheers !!!

--

--

Arjun Singh

Android, Titanium, Kotlin and React Native developer but still a newbie learner…