Search code examples
androidwebview

android – How to check if webview is available?


Is there a way to check if webview is available on the device?

Background: If I add <uses-feature android:name="android.software.webview"/> to the Manifest the number of supported devices on Google Play drops from over 12,000 to less than 6,000 devices. So I added android:required="false" to this feature. In case webview is available websites should be displayed inside the app otherwise launched in the default browser:

String mUrl = "http://www.example.com";
if (*** WHAT TO PUT HERE? ***) {
    WebView webview = (WebView) findViewById(R.id.webView);
    if (Build.VERSION.SDK_INT >= 24) {
        webview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                view.loadUrl(request.toString());
                return false;
            }
        });
    } else {
        webview.setWebViewClient(new WebViewClient() {
            @SuppressWarnings("deprecation")
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return false;
            }
        });
    }
    webview.loadUrl(mUrl);
} else {
    Uri uri = Uri.parse(mUrl);
    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
    startActivity(intent);
}

Edit (to make things clear): <uses-permission android:name="android.permission.INTERNET" /> is (and always had been) part of the manifest. It’s just the addition of <uses-feature android:name="android.software.webview" /> which causes the drop of supported devices.

There is someone having the same issue here: https://issuetracker.google.com/issues/37035282 (unfortunately not answered)


Solution

  • Although @vsatkh pointed out that it is not necessarily needed to declare this feature as required, you can check the device’s feature compatibility as follows:

    getPackageManager().hasSystemFeature("android.software.webview")
    

    This method returns true or false.

    Some additional information about Google Play’s filtering: Google Play only filters supported devices based on <uses-feature> elements declared in the manifest. <uses-permission> elements don’t affect Google Play’s filtering unless they imply a feature. android.permission.INTERNET does not imply any feature. Permissions that imply features are listed here: https://developer.android.com/guide/topics/manifest/uses-feature-element.html#permissions