8000 Android WebView Optimization · Issue #41 · yunshuipiao/Potato · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Android WebView Optimization #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
yunshuipiao opened this issue May 25, 2019 · 0 comments
Open

Android WebView Optimization #41

yunshuipiao opened this issue May 25, 2019 · 0 comments
Labels

Comments

@yunshuipiao
Copy link
Owner

Android WebView Optimization

[TOC]

以前项目做过相关的专项,优化 webview 的加载。

Html 加载流程

2362769-a8b56d5088dc393b

加载流程各节点耗时分析优化

  • WebView 创建初始化:由于首次初始化涉及到浏览器内核的加载初始化,明显会比第二次初始化慢很多。初始化后,即使 WebView 释放,但部分共用的全局服务/资源对象未释放,而第二次初始化不需要重新生成,因此会快很多。
    • 方案:提前初始化全局 webview。或者维护一个 webview pool 进行复用。
  • 资源文件下载耗时:弱网情况下,下载网页资源耗时,白屏时间过程。
    • 网页资源压缩,CND 加速处理,缩短请求耗时。
    • 服务端下发填充好数据的网页,首屏展示。
    • App 闲置时,下载离线包到本地,优先加载其内容。
  • Html ,CSS,js 文件加载前端优化

客户端优化

合理使用 WebView 提供的几种缓存模式

  • 浏览器缓存,内置实现

  • webSettings.setAppCacheEnabled(true)
    webSettings.setAppCacheMaxSize(yourCacheSize)
    webSettings.setAppCachePath(yourCacheDirPath)

  • shouldInterceptRequest: 拦截静态资源进行缓存

  • DOM Storage 缓存:setDomStorageEnabled(true)

  • Web SQL Database 缓存:

    webSettings.setDatabaseEnabled(true)
    webSettings.setDatabasePath(yourCacheDirPath)

  • Indexed Database 缓存:webSettings.setJavaScriptEnabled(true)

总结

  • WebView 在 Application 中提前初始化,并加载首屏资源进行缓存
  • 实现 WebView 复用,或者另开 WebView 进程
  • 离线预推,使用离线包,增量更新
  • okhttp 自定义实现图片资源缓存
  • 本地资源拦截替换
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant
0