There are several situations where you may need the current version of your app from google play store to perform certain tasks like comparing it with the app version present in your device and creating an alert dialogue to update the app if the app version is lesser than play store version.
To do this you can use JSOUP library. This library actually provides a very convenient way to parse the HTML and provide you the current version of your app from play store.
To do this first, you need to add the dependency in your app level build.gradle
1 |
compile 'org.jsoup:jsoup:1.10.2' |
After adding the dependency you need to create a helper class which will be used whenever you need to get the version name from play store.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
public class VersionChecker extends AsyncTask<String, String, String> { private String newVersion; @Override protected String doInBackground(String... params) { try { newVersion = Jsoup.connect("https://play.google.com/store/apps/details?id=" + BuildConfig.APPLICATION_ID + "&hl=en") .timeout(30000) .userAgent("Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6") .referrer("http://www.google.com") .get() .select("div[itemprop=softwareVersion]") .first() .ownText(); } catch (IOException e) { e.printStackTrace(); } return newVersion; } } |
Now you are ready to get the current version of your app and to get that you need to create an object of your VersionChecker class.
1 2 3 4 5 6 |
VersionChecker versionChecker = new VersionChecker(); try { mLatestVersionName = versionChecker.execute().get(); } catch (InterruptedException | ExecutionException e) { e.printStackTrace(); } |
As you can see in the above code variable mLatestVersionName is initialized by versionChecker.execute().get() which returns a string value which is the version name of your app from google play store.
Now you can compare this version name with your current version of the app in the device and perform the required task. Look at the code segment given below for a little help.
1 2 3 |
if (Double.parseDouble(BuildConfig.VERSION_NAME) < Double.parseDouble(mLatestVersionName)) { //perform your task here like show alert dialogue "Need to upgrade app" } |
That all.
Thank you very much. This is Vedesh Kumar signing off.
Updated Solution –>
Get Current Version of the Application from the playstore