美文网首页
Android P 行为变更对 WebView 的影响

Android P 行为变更对 WebView 的影响

作者: 一点愁 | 来源:发表于2022-02-08 17:14 被阅读0次

年前遇到的这个问题,今天年后第一天上班,在验证别的问题的时候突然想到年前解决的这个问题,一时竟想不起当时怎么解决的了,翻看代码思考了一会儿才想起来,特在此记录下。

统计到的错误日志如下:

Process Name: 'com.xxxx.xxx’
Thread Name: 'main'
Back traces starts.

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxxx.xxxx/com.xuetian.player.player.live.LivePlayDocActivity}: android.view.InflateException: Binary XML file line #7 in com.xxx.xxx:layout/intro_layout: Binary XML file line #7 in com.xxx.xxx:layout/intro_layout: Error inflating class com.xuetian.common.ui.lib.widget.LollipopFixedWebView

at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3504)

at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3643)

at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)

at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:140)

at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:100)

at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2229)

at android.os.Handler.dispatchMessage(Handler.java:107)

at android.os.Looper.loop(Looper.java:238)

at android.app.ActivityThread.main(ActivityThread.java:7798)

at java.lang.reflect.Method.invoke(Native Method)

at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:512)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:995)

Caused by: android.view.InflateException: Binary XML file line #7 in com.xxx.xxx:layout/intro_layout: Binary XML file line #7 in com.xxx.xxx:layout/intro_layout: Error inflating class com.xuetian.common.ui.lib.widget.LollipopFixedWebView

Caused by: android.view.InflateException: Binary XML file line #7 in com.xxx.xxx:layout/intro_layout: Error inflating class com.xuetian.common.ui.lib.widget.LollipopFixedWebView

Caused by: java.lang.reflect.InvocationTargetException

at java.lang.reflect.Constructor.newInstance0(Native Method)

at java.lang.reflect.Constructor.newInstance(Constructor.java:343)

at android.view.LayoutInflater.createView(LayoutInflater.java:855)

at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1008)

at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:963)

at android.view.LayoutInflater.rInflate(LayoutInflater.java:1125)

at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1086)

at android.view.LayoutInflater.inflate(LayoutInflater.java:682)

at android.view.LayoutInflater.inflate(LayoutInflater.java:534)

at com.xuetian.player.player.live.LiveIntroComponent.initIntroView(LiveIntroComponent.java:38)

at com.xuetian.player.player.live.LiveIntroComponent.<init>(LiveIntroComponent.java:28)

at com.xuetian.player.player.live.LivePlayDocActivity.initIntroLayout(LivePlayDocActivity.java:699)

at com.xuetian.player.player.live.LivePlayDocActivity.initComponents(LivePlayDocActivity.java:678)

at com.xuetian.player.player.live.LivePlayDocActivity.initViewPager(LivePlayDocActivity.java:564)

at com.xuetian.player.player.live.LivePlayDocActivity.onCreate(LivePlayDocActivity.java:141)

at android.app.Activity.performCreate(Activity.java:7963)

at android.app.Activity.performCreate(Activity.java:7952)

at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1307)

at com.qiyukf.unicorn.i.a.callActivityOnCreate(Unknown Source:2)

at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3479)

at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3643)

at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)

at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:140)

at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:100)

at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2229)

at android.os.Handler.dispatchMessage(Handler.java:107)

at android.os.Looper.loop(Looper.java:238)

at android.app.ActivityThread.main(ActivityThread.java:7798)

at java.lang.reflect.Method.invoke(Native Method)

at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:512)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:995)

Caused by: java.lang.RuntimeException: Using WebView from more than one process at once with the same data directory is not supported. https://crbug.com/558377

at org.chromium.android_webview.AwBrowserProcess.b(PG:11)

at D5.m(PG:33)

at D5.a(PG:48)

at D5.b(PG:16)

at com.android.webview.chromium.WebViewChromiumFactoryProvider.a(PG:32)

at com.android.webview.chromium.WebViewChromium.init(PG:12)

at android.webkit.WebView.<init>(WebView.java:435)

at android.webkit.WebView.<init>(WebView.java:359)

at android.webkit.WebView.<init>(WebView.java:342)

at android.webkit.WebView.<init>(WebView.java:329)

at com.xuetian.common.ui.lib.widget.LollipopFixedWebView.<init>(LollipopFixedWebView.java:24)

... 31 more

Back traces ends.

1、Android Pie 行为变更——按进程分设基于网络的数据目录

为改善 Android 9 中的应用稳定性和数据完整性,应用无法再让多个进程共用同一 WebView 数据目录。 此类数据目录一般存储 Cookie、HTTP 缓存以及其他与网络浏览有关的持久性和临时性存储。
在大多数情况下,您的应用只应在一个进程中使用 android.webkit 软件包中的类,例如 WebView 和 CookieManager。 例如,您应该将所有使用 WebView 的 Activity 对象移入同一进程。 您可以通过在应用的其他进程中调用 disableWebView(),更严格地执行"仅限一个进程”规则。该调用可防止 WebView 在这些其他进程中被错误地初始化,即使是从依赖内容库进行的调用也能防止。
如果您的应用必须在多个进程中使用 WebView 的实例,则必须先利用 WebView.setDataDirectorySuffix() 函数为每个进程指定唯一的数据目录后缀,然后再在该进程中使用 WebView 的给定实例。 该函数会将每个进程的网络数据放入其在应用数据目录内自己的目录中。
注意:即使您使用 setDataDirectorySuffix(),系统也不会跨应用的进程界限共享 Cookie 以及其他网络数据。 如果应用中的多个进程需要访问同一网络数据,您需要自行在这些进程之间复制数据。 例如,您可以调用 getCookie()setCookie(),在不同进程之间手动传输 Cookie 数据。

这个问题在去年初就遇到解决了一次,解决方式为在Application的oncreat方法里调用如下代码:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
  String processName =getProcessName();                            
  if(processName.equals("com.xxx.xxx")){
    WebView.setDataDirectorySuffix(processName);
  }
}

当时按这种方式解决之后错误日志统计也就没再出现过该错误。直到去年底使用了加固服务之后突然又出现了该错误,错误率飙到了1.9%,不由得龙脊一紧。当时怀疑是加固的问题,但是手上没有手机能复现该问题,使用错误统计的相同机型(机型版本:PCHM10、PCLM50等)都复现不了。后来终于找到一个同事的手机复现了这个问题,而且是毕现(一加手机:OnePlus 9R)。

从再次遇到这个问题到彻底解决这个问题耗费了将近一天的时间,期间甚至怀疑刚购买的加固服务导致的。不过好在解决了,使得错误率降低到了现在可接受的 0.09%,下面大概说一下具体情况。

2、经过多种尝试最终发现以下方式可以解决

开始解决代码:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
  String processName =getProcessName();                            
  if(processName.equals("com.xxx.xxx")){
    WebView.setDataDirectorySuffix(processName);
  }
}

修改后最终解决代码:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
  String processName =getProcessName(); 
  WebView.setDataDirectorySuffix(processName);
}

该设备机型与系统WebView版本见下图:

00e2a34c-feb1-4e1f-add7-bcd1632b52f9.jpg 4fc19753797e61b40c357a2fcbb581cb.jpg

相关文章

  • Android P 行为变更对 WebView 的影响

    年前遇到的这个问题,今天年后第一天上班,在验证别的问题的时候突然想到年前解决的这个问题,一时竟想不起当时怎么解决的...

  • Android P(9.0) 行为变更 适配WebView

    最近在后台发现app报webview的错误,主要原因是Android P的行为变更导致的。 WebView崩溃错误...

  • Android 8.0适配,O系统迁移指南

    最近在做8.0以及P版本的是适配,对安卓的行为变更,对企业项目的影响做了简单的整理 Android 8.0 行为变...

  • Android 12 兼容之行为变更

    行为变更:所有应用 Android 12 平台包含一些行为变更,这些变更可能会影响您的应用。以下行为变更将影响在 ...

  • Android P 行为变更

    概览 Android P 对 Android 系统进行了多项变更。 其中大部分变更会影响所有应用,而不论应用针对的...

  • Android P 行为变更

    Android P 引入一系列有关 Android 系统的变更。不论应用针对的是哪个 Android 版本,其中大...

  • Android P行为变更

    Android P引入了若干系统行为变更。少数变更会区分App是否适用于Android P,大部分则不区分(译者注...

  • Android各版本变更中文文档

    Android 各个版本概要及功能变更 Android9行为变更-只影响28+的变化

  • Android各个版本行为变更与适配

    Android9.0 Android 9.0 行为变更Android P版本应用兼容性适配技术指导Android ...

  • Android P 安全行为变更

    安全行为变更 Android P 引入了若干可提升应用和运行应用的设备安全性的行为变更。 本页面介绍对第三方应用开...

网友评论

      本文标题:Android P 行为变更对 WebView 的影响

      本文链接:https://www.haomeiwen.com/subject/bvgbkrtx.html