Determine WebView Implementation (System WebView or Chrome)

16.4k views Asked by At

Android 7.0 allows users (via developer options) to choose the implementation of their WebView. The user can choose the standalone WebView or use the Chrome APK to render WebViews. Reference

Since this potentially means 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 what WebView implementation is selected in Android 7?

5

There are 5 answers

1
DataDino On BEST ANSWER

Looks like this now available in Android O Preview:

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

Starting in 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 fetching information related to the package that is displaying web content in your app. This API is especially useful when analyzing errors that occur only when your app tries to display web content using a particular package's implementation of WebView.

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()

0
azizbekian On

As a supplementary info to the answer of DataDino, for APIs below 26 here's a chunk of code that would give 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"
    }

3
MatPag On

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).

0
Shahrad Elahi On
dependencies {

    // ...
    implementation 'androidx.webkit:webkit:1.4.0' // Add to your gradle

}
public static String WebViewPackageName(Context context) {
    PackageInfo webViewPackageInfo = WebViewCompat.getCurrentWebViewPackage(context);
    return webViewPackageInfo.packageName;
}
0
tonyo.dev On

There is an appCompat version:

WebViewCompat.getCurrentWebViewPackage(context)