Define a WebView implementation (System WebView or Chrome)

Android 7.0 allows users (through developer options) to choose the implementation of their WebView. The user can select a standalone WebView or use the APK to render a WebView. Link

Since this potentially means that those who use WebViews now have two different code bases to worry about, it would be useful to know which implementation is currently selected.

Is there a way to determine which version of WebView is selected in Android 7?

+8
source share
4 answers

It looks like it's now available in Android O Preview:

Link: https://developer.android.com/preview/features/managing-webview.html

Starting with Android 7.0 (API level 24), users can choose among several different packages for displaying web content in a WebView object. Android O includes an API for retrieving information related to a package that displays web content in your application . This API is especially useful when analyzing errors that occur only when your application tries to display web content using a specific WebView implementation package.

To use this API, add the logic shown in the following code snippet:

PackageInfo webViewPackageInfo = WebView.getCurrentWebViewPackage(); Log.d(TAG, "WebView version: " + webViewPackageInfo.versionName); 

WebView.getCurrentWebViewPackage Documentation: https://developer.android.com/reference/android/webkit/WebView.html#getCurrentWebViewPackage ()

+2
source

To get the current implementation and version of Android WebView , I created this method, which must be valid for each level of the API.

 @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 updating the WebView application, a crash may appear as described here: fooobar.com/questions/68546 / ... , at this moment this line webViewFactory.getMethod("getLoadedPackageInfo") the above code will return zero. Actually there is nothing you can do to prevent this (this should not happen if the WebView implementation is taken from a Chrome application but not confirmed).

+3
source

As additional information for the DataDino answer for API below 26, here is a code snippet that will give the desired result:

 Class webViewFactory = Class.forName("android.webkit.WebViewFactory"); Method method = webViewFactory.getMethod("getLoadedPackageInfo"); PackageInfo packageInfo = (PackageInfo) method.invoke(null, null); if ("com.android.webview".equals(packageInfo.packageName)) { // "Android System WebView" is selected } else { // something else selected // in case of chrome it would be "com.android.chrome" }
Class webViewFactory = Class.forName("android.webkit.WebViewFactory"); Method method = webViewFactory.getMethod("getLoadedPackageInfo"); PackageInfo packageInfo = (PackageInfo) method.invoke(null, null); if ("com.android.webview".equals(packageInfo.packageName)) { // "Android System WebView" is selected } else { // something else selected // in case of chrome it would be "com.android.chrome" } 
+2
source

There is a version of appCompat:

WebViewCompat.getCurrentWebViewPackage(context)

0
source

Source: https://habr.com/ru/post/1261539/


All Articles