問題描述:
因公司業(yè)務需要,現(xiàn)在 Android 存在的方式基本都是原生與 h5 頁面共存的情況。近日發(fā)現(xiàn),當應用中存在多個 WebView 實例去渲染 h5 頁面的時候,就有可能會出現(xiàn)程序異常崩潰的情況,在低端機中表現(xiàn)較為明顯。
出現(xiàn)的原因:
通過日志排查發(fā)現(xiàn),在 log 控制臺除了打印一堆 dump 日志之外,還有一行格外鮮艷的話:
Render process (11786)'s crash wasn't handled by all associated webviews, triggering application crash.
這句話大致的意思就是,由于相關聯(lián)的所有 WebView 沒有處理渲染進程的 crash,進而觸發(fā)了我們 application 的 crash。
查閱官方文檔,發(fā)現(xiàn) WebViewClient 中有一個回調(diào)方法 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 被移除的時候就會回調(diào)該方法,再繼續(xù)跟到里面看具體的實現(xiàn),我這用的調(diào)試機是榮耀的,所以這里的實現(xiàn)是一個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 不為空的時候就會調(diào)用進來。
再回到上一步,發(fā)現(xiàn) onRenderProcessGone 是虛類 WebViewRenderProcess 里面的一個方法。
/**
* WebViewRenderProcess provides an opaque handle to a {@link WebView} renderer.
*/
public abstract class WebViewRenderProcess {
/**
* Cause this renderer to terminate.
*
* <p>Calling this on a not yet started, or an already terminated renderer will have no effect.
*
* <p>Terminating a renderer process may have an effect on multiple {@link WebView} instances.
*
* <p>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() {
}
}
再看看這個類在哪有引用到,接著繼續(xù)往下跟發(fā)現(xiàn)在 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 進程的事件回調(diào),然后做一些判斷處理,回調(diào)相應抑或是不響應。
/**
* 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.
*
* <p>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.
*
* <p>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.
*
* <p>The minimum interval between successive calls to {@code onRenderProcessUnresponsive} is 5
* seconds.
*
* <p>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 進程長時間無響應的時候,就會觸發(fā)這個方法。比如因為 JavaScript 長時間無響應、輸入相應事件長時間無響應抑或是導航至新的 url 無響應等。
到這里,基本就比較明晰了。也就是說,當 WebView 的任務過重,如 JavaScrip 長時間無響應,抑或是其他導致響應事件過長的情況,都有可能會觸發(fā) render 進程移除,如果不加以處理,就會導致我們的應用進程被強制停止。
解決辦法:
既然當系統(tǒng) WebView 的 Render 進程被移除時會給到我們相應的回調(diào),因此我們直接重寫 onRenderProcessGone 方法。
simple代碼大致如下,根據(jù)實際業(yè)務進行處理,僅供參考。
@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();
// 重新創(chuàng)建新的 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);
}
以上。