35

I have Lollipop, and see that we have a separate app for "android system webview". Is there any way to get its version number from my own app that uses a WebView instance?

I'd like to report some stats on which version my users are using.

Thanks

2
  • 3
    That's an interesting idea. I don't know of a way to get one from WebView itself, though there ideally would be an option for that. Worst-case, if you can figure out the application ID of that "android system webview" app, you can get a versionCode and versionName for it from PackageManager. Commented Mar 19, 2015 at 13:33
  • The updateable WebView component is play.google.com/store/apps/… and its versionName seems to roughly follow the Chrome version naming scheme. Commented Mar 23, 2015 at 19:30

4 Answers 4

48
+250

How about checking the user-agent string?

Log.i("WebViewActivity", "UA: " + mWebView.getSettings().getUserAgentString());

For me, this outputs:

User-agent string: Mozilla/5.0 (Linux; Android 5.0; Nexus 4 Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/37.0.0.0 Mobile Safari/537.36

More info: WebView on Android

In case you override UA string with your own:

String getWebviewVersionInfo() {
    // Overridden UA string
    String alreadySetUA = mWebView.getSettings().getUserAgentString();

    // Next call to getUserAgentString() will get us the default
    mWebView.getSettings().setUserAgentString(null);

    // Devise a method for parsing the UA string
    String webViewVersion = 
           parseUAForVersion(mWebView.getSettings().getUserAgentString());

    // Revert to overriden UA string
    mWebView.getSettings().setUserAgentString(alreadySetUA);

    return webViewVersion;
}
5
  • 3
    Interesting. My original reaction was that the user agent must be different than the WebView version, as you're only reporting 37, but they just announced 40. But, I see that my Nexus 4 has version 37 of the Android System WebView app, suggesting that my user agent would also report 37. I'd prefer something more... definitive, but if the user agent tracks with the system app update, this is a bit easier than the PackageManager approach of examining the system app's version itself. Commented Mar 23, 2015 at 19:48
  • 4
    Very cool! I'm going to leave the bounty open another couple of days, in case somebody has a better answer, but yours seems solid enough for production use, given the right regex. Of course, the OP can also chime in and accept answers as the OP sees fit. Thanks! Commented Mar 23, 2015 at 20:16
  • Maybe pointing about the obvious, but this will not work if you override the user agent with your own custom user agent.
    – Randy
    Commented Mar 24, 2015 at 16:05
  • 1
    @Randy Since this is a logging aid, you can create a utility method that resets the UA string to default (by setUserAgentString(null)), retrieves the default UA string using getUserAgentString(), and finally - reverts back to the custom UA string using setUserAgentString(customUAString). I have added this as an edit to my answer.
    – Vikram
    Commented Mar 24, 2015 at 16:47
  • regex to extract the version: Matcher m = Pattern.compile("Chrome/([\\d.]+)").matcher(ua); if (m.find()) version = matcher.group(1); Commented Feb 3 at 3:02
28

UPDATE: Apparently this will not always accurately give the actual WebView client being used on the target device. As of Android 7.0 users can select preferred client (h/t @Greg Dan).


First, we get the package name from Google Play Store:

https://play.google.com/store/apps/details?id=com.google.android.webview

Then this

PackageManager pm = getPackageManager();
try {
    PackageInfo pi = pm.getPackageInfo("com.google.android.webview", 0);
    Log.d(TAG, "version name: " + pi.versionName);
    Log.d(TAG, "version code: " + pi.versionCode);
} catch (PackageManager.NameNotFoundException e) {
    Log.e(TAG, "Android System WebView is not found");
}

gives

D/WebViewDetails﹕ version name: 39 (1743759-arm)
D/WebViewDetails﹕ version code: 320201

Hope this helps.

4
  • 3
    Things changed in Android 7 and this method is no longer correct developer.android.com/about/versions/nougat/…
    – Greg Dan
    Commented Oct 31, 2017 at 8:36
  • Thanks for the heads up @GregDan! Added an update above.
    – ozbek
    Commented Oct 31, 2017 at 11:05
  • 1
    however on Android 8 you can call the method getCurrentWebViewPackage to get the package, rather than hard-coding it Commented Sep 21, 2021 at 14:20
  • On my Android 5 device, the package is called "com.android.webview" not "com.google.android.webview". Recommend to check both package names. Commented Sep 27, 2021 at 8:14
8

In Android O and newer you can use WebView.getCurrentWebViewPackage();

import android.webkit.WebView;

...
...


if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    PackageInfo info = WebView.getCurrentWebViewPackage();
    return info.versionName;
}
1
  • 2
    This was backported in Jetpack Webkit, you can use it like this: val webViewPackage: PackageInfo? = WebViewCompat.getCurrentWebViewPackage(this@MainActivity)
    – bompf
    Commented Jun 10, 2021 at 16:20
0

This approach is compatible with most devices:

val packageInfo = WebViewCompat.getCurrentWebViewPackage(this@MainActivity)
return packageInfo.versionName

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.