How to check if the android app’s new version has been released in Google Play Store

Hey there ! Today we are going to learn how to check if the new version of our android app has been released or not. If it is, then we will notify our users that the new version has been released and redirect them to the playstore. 

Note : We will use Java for that purpose.

1- To get our app’s new version we will use the jsoup  library. 

This library allows us to parse html elements. First and foremost start your Android Studio IDE and create or open a project.Go to Gradle Scripts -> build.gradle(Module : appName) . Add the jsoup library into the dependencies section :

dependencies {
    ……
    implementation 'org.jsoup:jsoup:1.11.3'
    ……
}

2- Now we will create a java class file called UrlResponse.java .

Once you’ve add the java class the project source tree will look like this :

1.0.0 UrlResponse

Copy the below codes and paste it into UrlResponse.java file :

public abstract class UrlResponse {
    public abstract void onReceived(String responseStr);
}

3- Now we are gonna create another java class file  called CheckIsUpdateReady.java

When you added this file the source tree will look like this :

1.0.1 CheckIsUpdateReady

Copy all the codes below an paste it into CheckIsUpdateReady.java :

import android.os.AsyncTask;
import android.util.Log;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;

public class CheckIsUpdateReady extends AsyncTask<Void, String, String> {
    String appURL="";
    private UrlResponse mUrlResponse;
    public CheckIsUpdateReady(String appURL, UrlResponse callback) {
        this.appURL=appURL;
        mUrlResponse = callback;
    }
    @Override
    protected String doInBackground(Void... voids) {
        String newVersion = null;
        try {
            Document document = Jsoup.connect(appURL)
                    .timeout(20000)
                    .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();
            if (document != null) {
                Elements element = document.getElementsContainingOwnText("Current Version");
                for (Element ele : element) {
                    if (ele.siblingElements() != null) {
                        Elements sibElemets = ele.siblingElements();
                        for (Element sibElemet : sibElemets) {
                            newVersion = sibElemet.text();
                        }
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return newVersion;
    }
    @Override
    protected void onPostExecute(String onlineVersion) {
        super.onPostExecute(onlineVersion);
        if (onlineVersion != null && !onlineVersion.isEmpty()) {
            mUrlResponse.onReceived(onlineVersion);
        }
        Log.d("update", " playstore App version " + onlineVersion);
    }
}


We will use this Class to get the recent version of the app from the https://play.google.com/store/apps/  url. 

Now we are ready to do some things in our main java file.

4-  Our final step will be creating a function called CheckUpdateVersion() in our main java file. 

The function will be like this :

public void CheckUpdateVersion() {
    String currentVersion = ""
    try {
        currentVersion = getPackageManager().getPackageInfo(getPackageName(), 0).versionName; // getting our app's package name
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    String finalCurrentVersion = currentVersion;
    new CheckIsUpdateReady("https://play.google.com/store/apps/details?id=" + getPackageName() + "&hl=en", new UrlResponce() {
        @Override
        public void onReceived(String responseStr) {
            if (finalCurrentVersion != ""){
                if (!responseStr.equals(finalCurrentVersion)){
                    Toast.makeText(getBaseContext(),responseStr + " version has been released,please update your app",Toast.LENGTH_SHORT).show();
                try{
                    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id="+getPackageName())));
                    int pid=android.os.Process.myPid();
                    android.os.Process.killProcess(pid);
                }catch(android.content.ActivityNotFoundException anfe){
                    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id="+getPackageName())));
                    int pid=android.os.Process.myPid();
                    android.os.Process.killProcess(pid);
                }
                }
            }
        }
    }).execute();
}

Alright. Whenever we call the CheckUpdateVersion function it will check the recent version of the app in the play store and compare that version with the version of the app that is installed in your phone. If two versions are not the same,  then it will close the app and redirect the user to the play store. 

You can also download the source code from github : https://github.com/vedat73/android-new-version-controller

That’s it for this post guys. I hope it will be useful for you too, see you soon…

You may also like...