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)
Scroll and find Android System WebView.Tap on it to open the app's info page. Tap "Enable." If you have previously disabled Android System WebView, the leftmost of the two large buttons on the app's info page will read Enable. Click on it to enable Android System WebView once more!
The Android system webview custom cache file has been deprecated and removed in Android 13. New apps and any app updates will now use the operating system default cache location.
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
Alternative solution:val hasWebView: Boolean = kotlin.runCatching { CookieManager.getInstance() }.isSuccess
It works because if WebView is not available then CookieManager.getInstance()
will throw AndroidRuntimeException
.
getPackageManager().hasSystemFeature("android.software.webview")
is not reliable. It will return true if the device is supposed to have a webview, but has disabled it in system settings.
I tested it on a Huawei P20 by going to Settings -> Apps -> Android System WebView -> Disable
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With