multiprocessing – Android Pie (9.0) WebView in multi-process-ThrowExceptions

Exception or error:

Starting Android Pie (API 28), Google isn’t allowing using a single WebView instance in 2 different processes.


As required, I called WebView.setDataDirectorySuffix("dir_name_no_separator") but unfortunately, I get an exception.
I tried to call this method inside the 2nd process Service onCreate().

java.lang.RuntimeException: Unable to create service com.myapp.service.MyService: java.lang.IllegalStateException: Can't set data directory suffix: WebView already initialized
        at android.os.Handler.dispatchMessage(
        at android.os.Looper.loop(
        at java.lang.reflect.Method.invoke(Native Method)
     Caused by: java.lang.IllegalStateException: Can't set data directory suffix: WebView already initialized
        at android.webkit.WebViewFactory.setDataDirectorySuffix(
        at android.webkit.WebView.setDataDirectorySuffix(
        at com.myapp.service.MyService.onCreate(

I couldn’t find any reason for that exception. I didn’t call this method twice nor I called it in my main process. Any ideas?

How to solve:


My project hosts AdMob ads and I call the MobileAds.initialize() method inside my Application class onCreate(). The ads initializer loads a WebView which is now forbidden to do in a new process before you call the WebView.setDataDirectorySuffix("dir_name_no_separator") method.

When the second process is created, it also goes through the same application create flow, meaning it calls the same onCreate() inside the Application class, which calls the MobileAds.initialize() that tries to create a new WebView instance and by that causes the crash.

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

How I solved this?

I get the process name using the below method and check if it’s my main process – call the MobileAds.initialize() method and if it’s my second process, call the
WebView.setDataDirectorySuffix("dir_name_no_separator") method.

Get process name:

public static String getProcessName(Context context) {
    ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    for (ActivityManager.RunningAppProcessInfo processInfo : manager.getRunningAppProcesses()) {
        if ( == android.os.Process.myPid()) {
            return processInfo.processName;

    return null;

Application class onCreate():

if (!Utils.getProcessName(this).equals("YOUR_SECOND_PROCESS_NAME")) {
} else {


To summarize the fix with all the improvements, this is the code in Kotlin:

    if (packageName != Application.getProcessName()) {

Add it to your Application class to onCreate() method.

Note this is will only fix problem with maximum 2 processes. If your app is using more, you have to provide different WebView suffix for each of them.

Leave a Reply

Your email address will not be published. Required fields are marked *