To get the current Android WebView
implementation and version I've created this method which should be valid for every API level.
@SuppressLint("PrivateApi")
@SuppressWarnings({"unchecked", "JavaReflectionInvocation"})
public @Nullable PackageInfo getCurrentWebViewPackageInfo() {
PackageInfo pInfo = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//starting with Android O (API 26) they added a new method specific for this
pInfo = WebView.getCurrentWebViewPackage();
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//with Android Lollipop (API 21) they started to update the WebView
//as a separate APK with the PlayStore and they added the
//getLoadedPackageInfo() method to the WebViewFactory class and this
//should handle the Android 7.0 behaviour changes too
try {
Class webViewFactory = Class.forName("android.webkit.WebViewFactory");
Method method = webViewFactory.getMethod("getLoadedPackageInfo");
pInfo = (PackageInfo) method.invoke(null);
} catch (Exception e) {
e.printStackTrace();
}
} else {
//before Lollipop the WebView was bundled with the
//OS, the fixed versions can be found online, for example:
//Android 4.4 has WebView version 30.0.0.0
//Android 4.4.3 has WebView version 33.0.0.0
//etc...
}
return pInfo;
}
Then you can evaluate the result
if (pInfo != null) {
Log.d("WEBVIEW VERSION", pInfo.packageName + ", " + pInfo.versionName);
}
Remember: Immediately after an app update of WebView, a crash
could appear as described here:
https://stackoverflow.com/a/29809338/2910520, at this moment, this line webViewFactory.getMethod("getLoadedPackageInfo")
of the code above would return null.
Actually there is
nothing you can do to prevent this, (this should not happen if the WebView implementation is taken from Chrome app but is not confirmed).