问题描述:
因公司业务需要,现在 Android 存在的方式基本都是原生与 h5 页面共存的情况。近日发现,当应用中存在多个 WebView 实例去渲染 h5 页面的时候,就有可能会出现程序异常崩溃的情况,在低端机中表现较为明显。
出现的原因:
通过日志排查发现,在 log 控制台除了打印一堆 dump 日志之外,还有一行格外鲜艳的话:
Render process (11786)'s crash wasn't handled by all associated webviews, triggering application crash.
这句话大致的意思就是,由于相关联的所有 WebView 没有处理渲染进程的 crash,进而触发了我们 application 的 crash。
查阅官方文档,发现 WebViewClient 中有一个回调方法 onRenderProcessGone
/**
* Notify host application that the given WebView's render process has exited.
*
* Multiple WebView instances may be associated with a single render process;
* onRenderProcessGone will be called for each WebView that was affected.
* The application's implementation of this callback should only attempt to
* clean up the specific WebView given as a parameter, and should not assume
* that other WebView instances are affected.
*
* The given WebView can't be used, and should be removed from the view hierarchy,
* all references to it should be cleaned up, e.g any references in the Activity
* or other classes saved using {@link android.view.View#findViewById} and similar calls, etc.
*
* To cause an render process crash for test purpose, the application can
* call {@code loadUrl("chrome://crash")} on the WebView. Note that multiple WebView
* instances may be affected if they share a render process, not just the
* specific WebView which loaded chrome://crash.
*
* @param view The WebView which needs to be cleaned up.
* @param detail the reason why it exited.
* @return {@code true} if the host application handled the situation that process has
* exited, otherwise, application will crash if render process crashed,
* or be killed if render process was killed by the system.
*/
public boolean onRenderProcessGone(WebView view, RenderProcessGoneDetail detail) {
return false;
}
从官方的注释中可以看到,当WebView 的 Render Process 被移除的时候就会回调该方法,再继续跟到里面看具体的实现,我这用的调试机是荣耀的,所以这里的实现是一个SaveWebView 的类。
@SuppressLint({"NewApi"})
public boolean onRenderProcessGone(WebView var1, RenderProcessGoneDetail var2) {
return this.O != null ? this.O.onRenderProcessGone(var1, var2) : super.onRenderProcessGone(var1, var2);
}
一目了然,这里的 O 就是 WebViewClient,当 WebViewClient 不为空的时候就会调用进来。
再回到上一步,发现 onRenderProcessGone 是虚类 WebViewRenderProcess 里面的一个方法。
/**
* WebViewRenderProcess provides an opaque handle to a {@link WebView} renderer.
*/
public abstract class WebViewRenderProcess {
/**
* Cause this renderer to terminate.
*
* Calling this on a not yet started, or an already terminated renderer will have no effect.
*
*
Terminating a renderer process may have an effect on multiple {@link WebView} instances.
*
*
Renderer termination must be handled by properly overriding
* {@link WebViewClient#onRenderProcessGone} for every WebView that shares this
* renderer. If termination is not handled by all associated WebViews, then the application
* process will also be terminated.
*
* @return {@code true} if it was possible to terminate this renderer, {@code false} otherwise.
*/
public abstract boolean terminate();
public WebViewRenderProcess() {
}
}
再看看这个类在哪有引用到,接着继续往下跟发现在 WebViewRenderProcessClient 里面有个 onRenderProcessUnresponsive 方法。
关于 WebViewRenderProcessClient 有这么一段注释:
/**
* Used to receive callbacks on {@link WebView} renderer events.
*
* WebViewRenderProcessClient instances may be set or retrieved via {@link
* WebView#setWebViewRenderProcessClient(WebViewRenderProcessClient)} and {@link
* WebView#getWebViewRenderProcessClient()}.
*
* Instances may be attached to multiple WebViews, and thus a single renderer event may cause
* a callback to be called multiple times with different WebView parameters.
*/
大致意思就是说,这个类会接收 render 进程的事件回调,然后做一些判断处理,回调相应抑或是不响应。
/**
* Called when the renderer currently associated with {@code view} becomes unresponsive as a
* result of a long running blocking task such as the execution of JavaScript.
*
* If a WebView fails to process an input event, or successfully navigate to a new URL within
* a reasonable time frame, the renderer is considered to be unresponsive, and this callback
* will be called.
*
*
This callback will continue to be called at regular intervals as long as the renderer
* remains unresponsive. If the renderer becomes responsive again, {@link
* WebViewRenderProcessClient#onRenderProcessResponsive} will be called once, and this method
* will not subsequently be called unless another period of unresponsiveness is detected.
*
*
The minimum interval between successive calls to {@code onRenderProcessUnresponsive} is 5
* seconds.
*
*
No action is taken by WebView as a result of this method call. Applications may
* choose to terminate the associated renderer via the object that is passed to this callback,
* if in multiprocess mode, however this must be accompanied by correctly handling
* {@link WebViewClient#onRenderProcessGone} for this WebView, and all other WebViews associated
* with the same renderer. Failure to do so will result in application termination.
*
* @param view The {@link WebView} for which unresponsiveness was detected.
* @param renderer The {@link WebViewRenderProcess} that has become unresponsive,
* or {@code null} if WebView is running in single process mode.
*/
public abstract void onRenderProcessUnresponsive(
@NonNull WebView view, @Nullable WebViewRenderProcess renderer);
从注释中我们可以看到,当 Render 进程长时间无响应的时候,就会触发这个方法。比如因为 JavaScript 长时间无响应、输入相应事件长时间无响应抑或是导航至新的 url 无响应等。
到这里,基本就比较明晰了。也就是说,当 WebView 的任务过重,如 JavaScrip 长时间无响应,抑或是其他导致响应事件过长的情况,都有可能会触发 render 进程移除,如果不加以处理,就会导致我们的应用进程被强制停止。
解决办法:
既然当系统 WebView 的 Render 进程被移除时会给到我们相应的回调,因此我们直接重写 onRenderProcessGone 方法。
simple代码大致如下,根据实际业务进行处理,仅供参考。
@Override
public boolean onRenderProcessGone(WebView view, RenderProcessGoneDetail detail) {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
return false;
}
// 仅对我们自己的 webview 做处理
if ( view == mWebView) {
// 获取 webview 所在父布局
ViewGroup parent = (ViewGroup) mWebView.getParent();
ViewGroup.LayoutParams params = mWebView.getLayoutParams();
// 把无效不可用的 webview 从布局中移除
destroyWebView();
// 重新创建新的 webview
WebView newWebView = new WebView(getActivity());
newWebView.setId(R.id.webView);
parent.addView(newWebView, 0, params);
Bundle bundle = getArguments();
// 重走初始化流程,渲染 UI,加载 url
initView(root);
return true;
}
return super.onRenderProcessGone(view, detail);
}
以上。