Handling SSL Certificates in Android WebView

The Problem

During development process if you are accessing a web page with SSL certificate you could either get a “Blank Page” or “Page Not Found” error.

The Solution

To prevent this from happening while developing your app, simply override “onReceivedSslError” of WebViewClient object and continue the execution process.

1
2
3
4
5
6
7
8
webview.setWebViewClient(new WebViewClient() {
    @Override
    public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
        if (BuildConfig.DEBUG) {
            handler.proceed(); // Ignore SSL certificate errors
        }
    }
});

Security Considerations

By doing this, the SSL is being completely ignored and therefore could have a serious result if you forgot to remove the code after development and push the code into production.
To prevent that from happening simply check the build variants using BuildConfig.DEBUG.