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

查看:49
本文介绍了多进程中的 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.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(),它调用MobileAds.initialize() 尝试创建一个新的 WebView 实例并由此导致崩溃.

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天全站免登陆