Java – Get the WebView implementation programmatically

Get the WebView implementation programmatically… here is a solution to the problem.

Get the WebView implementation programmatically

The user can of course choose which WebView implementation to use, as explained here Is it possible to programmatically determine which implementation they chose? I know it’s possible Get the User-Agent string programmatically, but this is not exactly the same information.

enter image description here

enter image description here

In the example above, I was wondering if “Chrome Stable” had been chosen for the WebView implementation. On some user devices, there is obviously also an option to select “Android WebView” from the list, even Chrome is also an option, even if Android WebView is disabled in the list of system apps.

This is troublesome, because presumably WebView will not receive further updates for Android (it will stay in the past), while Chrome will receive updates and is a more suitable choice for WebView implementation. Even more troubling, the choice of WebView implementation is completely hidden from the average user.

So I specifically wondered if “Android WebView” was chosen, even in the case of Android 7.0+ more appropriate, instead chose Chrome.

Edit… Here’s a selection screen for one of my users on Android 7.0 that says “Android WebView” and “Chrome” are both options… And users are 99% sure that “Android WebView” is the default (powered by the venerable Chrome 51.0.2704.91)… If we don’t track it down, it will be stuck in this position indefinitely :

enter image description here

Solution

Introduced a new API starting with SDK 26 – WebView #getCurrentWebViewPackage() :

If WebView has already been loaded into the current process this method will return the package that was used to load it. Otherwise, the package that would be used if the WebView was loaded right now will be returned; this does not cause WebView to be loaded, so this information may become outdated at any time. The WebView package changes either when the current WebView package is updated, disabled, or uninstalled. It can also be changed through a Developer Setting. If the WebView package changes, any app process that has loaded WebView will be killed. The next time the app starts and loads WebView it will use the new WebView package instead.

Starting with API 25, it is not possible Select “Android System WebView”:

API 25

API 26

About your question:

So I want to know particularly if “Android System WebView” is selected, even for Android 7.0+

I don’t see how to select “Android System WebView” for Android 7.0+.
For earlier versions, obviously, you can’t use the getCurrentWebViewPackage() API, which works with API 26 and above. I looked at the implementation of the method and came up with this code, which will provide the desired output:


    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"
    }

Related Problems and Solutions