Solved.
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 (processInfo.pid == android.os.Process.myPid()) {
return processInfo.processName;
}
}
return null;
}
Application class onCreate():
if (!Utils.getProcessName(this).equals("YOUR_SECOND_PROCESS_NAME")) {
MobileAds.initialize(this);
} else {
WebView.setDataDirectorySuffix("dir_name_no_separator")
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…