多进程Android Pie(9.0)WebView [英] Android Pie (9.0) WebView in multi-process

查看:1588
本文介绍了多进程Android Pie(9.0)WebView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从Android Pie(API 28)开始,Google不允许在2个不同的过程中使用单个WebView实例.

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

文档: https://developer .android.com/reference/android/webkit/WebView.html#setDataDirectorySuffix(java.lang.String)

根据需要,我致电了WebView.setDataDirectorySuffix("dir_name_no_separator"),但不幸的是,我遇到了异常. 我试图在第二个进程Service onCreate()中调用此方法.

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.app.ActivityThread.handleCreateService(ActivityThread.java:3544)
        at android.app.ActivityThread.access$1300(ActivityThread.java:199)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1666)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: java.lang.IllegalStateException: Can't set data directory suffix: WebView already initialized
        at android.webkit.WebViewFactory.setDataDirectorySuffix(WebViewFactory.java:136)
        at android.webkit.WebView.setDataDirectorySuffix(WebView.java:2165)
        at com.myapp.service.MyService.onCreate(MyService.java:134)

我找不到该异常的任何原因.我没有两次调用此方法,也没有在主要过程中调用它.有什么想法吗?

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?

推荐答案

已解决.

我的项目托管AdMob广告,我在ApplicationonCreate()中调用了MobileAds.initialize()方法.广告初始化程序会加载一个WebView,现在您必须在调用WebView.setDataDirectorySuffix("dir_name_no_separator")方法之前在新进程中禁止该WebView.

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.

创建第二个进程时,它还会经历相同的应用程序创建流程,这意味着它在Application类内调用了相同的onCreate(),该类调用了试图创建新WebViewMobileAds.initialize().实例,然后导致崩溃.

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

我如何解决此问题?

我使用以下方法获取进程名称,并检查它是否是我的主进程-调用MobileAds.initialize()方法,如果这是我的第二个进程,则调用 WebView.setDataDirectorySuffix("dir_name_no_separator")方法.

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.

获取进程名称:

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

应用程序类onCreate():

Application class onCreate():

if (!Utils.getProcessName(this).equals("YOUR_SECOND_PROCESS_NAME")) {
    MobileAds.initialize(this);
} else {
    WebView.setDataDirectorySuffix("dir_name_no_separator")
}

这篇关于多进程Android Pie(9.0)WebView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆