Android P 行为变更对 WebView 的影响

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

统计到的错误日志如下:

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值