Force your users to update your Android App

You shouldn’t stop supporting an older version as soon as a new version comes out. This will result in terrible user experience. I don’t know of any software vendor in existence that does this, for good reason.
What happens if the user can’t update or doesn’t want to at that time? They simply can’t use your app, which is bad.
Google doesn’t provide any option for version tracking like that so you would have to roll your own. In my previous article, I solved the same updating problem but using Firebase Remote Config you can check out too but in my opinion that is a bit tricky and lengthy if you are not familiar with Firebase.
But in this method, we will be forcing users to update the app by comparing the current version of the app and the version of the app that is live on Google Play Store. We will be reading the live version of code right from the app store page, we will not be using any API or anything complication. We will be using the scraping technique.
So will be creating something called pattern matching. If you view the source code of any app page on Google Play Store and search it’s app version code there, you will realize that it is located inside a specific tag having a class named “hAyfc”. So we will read all the data and search for this specific span and get our version code.

Disclaimer: This method will work until Google made some changes into the Google Play Store
To match the latest pattern from Google Playstore ie <div class="BgcNfc">Current Version</div><span class="htlgb"><div><span class="htlgb">X.X.X</span></div>
we first have to match the above node sequence and then from the above sequence get the version value. Below is the code snippet for same:
private String getAppVersion(String patternString, String inputString) {
try{
//Create a pattern
Pattern pattern = Pattern.compile(patternString);
if (null == pattern) {
return null;
} //Match the pattern string in provided string
Matcher matcher = pattern.matcher(inputString);
if (null != matcher && matcher.find()) {
return matcher.group(1);
} }catch (PatternSyntaxException ex) { ex.printStackTrace();
} return null;
}
private String getPlayStoreAppVersion(String appUrlString) {
final String currentVersion_PatternSeq = "<div[^>]*?>Current\\sVersion</div><span[^>]*?>(.*?)><div[^>]*?>(.*?)><span[^>]*?>(.*?)</span>";
final String appVersion_PatternSeq = "htlgb\">([^<]*)</s";
String playStoreAppVersion = null; BufferedReader inReader = null;
URLConnection uc = null;
StringBuilder urlData = new StringBuilder(); final URL url = new URL(appUrlString);
uc = url.openConnection();
if(uc == null) {
return null;
}
uc.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
inReader = new BufferedReader(new InputStreamReader(uc.getInputStream()));
if (null != inReader) {
String str = "";
while ((str = inReader.readLine()) != null) {
urlData.append(str);
}
} // Get the current version pattern sequence
String versionString = getAppVersion (currentVersion_PatternSeq, urlData.toString());
if(null == versionString){
return null;
}else{
// get version from "htlgb">X.X.X</span>
playStoreAppVersion = getAppVersion (appVersion_PatternSeq, versionString);
} return playStoreAppVersion;
}
Remember that we are making a network request and which can possibly take a long time. So we must do it on a background thread. You can easily do that using Async-Task
We pass the App Page URL to the method getPlayStoreAppVersion and it will return the version code in String, we can now convert the string to float and compare it with the current version code of the app.
You can get the current version of the app by following code snippet:
int versionCode = BuildConfig.VERSION_CODE;
I hope this article might help you out. For more interesting tutorials please check out my blog FirebaseTutorials