Multi-Process WebView crash in Android 9 (Pie)


My App recently started crashing in Android 9.  A quick investigation revealed Google made a change to prevent multiple processes from sharing a WebView data directory with the default behavior being throwing an exception.  This effectively put my App into a launch / crash loop – thanks Google.

To solve the problem I sub-classed the MultiDexApplication and added this code:

import android.app.Application;
import android.os.Build;
import android.webkit.WebView;

import androidx.multidex.MultiDexApplication;

public class MyApplication extends MultiDexApplication
{

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

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
        {
            try
            {
                WebView.setDataDirectorySuffix(Application.getProcessName());
            }
            catch (Exception ignored)
            {

            }
        }

    }
}

Leave a comment