Android programmatically update application when a new version is available

Within my application, I want to check if there is any updated version of my application is in the app store. If there is any, then have to inform the user through an alert message and if he/she opt for upgrade I want to update the new version.I want to do all this through my application. Is this possible?


I have the same issue but it resolved by JSOUP library. Here is the library download link: http://jsoup.org/download

String newVersion = Jsoup
                        .connect(
                                "https://play.google.com/store/apps/details?id="
                                        + "Package Name" + "&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();

                Log.e("new Version", newVersion);

Nevertheless , you can make an http request on the web version of the playstore (https://play.google.com/store/apps/details?id=your.namespace)

To make the request you can use DefaultHttpClient

Once you get the page content you should parse it (jsoup is a good solution) and search for :

<div class="content" itemprop="softwareVersion"> 2.2.0  </div>

Once you find this part of the page , you can extract the version number and compare it with the one available in your app :

try
{
    String version = this.getPackageManager().getPackageInfo(this.getPackageName(), 0).versionName;
    if( ! version.equals(versionFromHTML))
    {
        Toast.makeText(this, "New version available on play store", Toast.LENGTH_SHORT);
    }
 
}
catch (NameNotFoundException e)
{
    //No version do something
}

For the HTML parsing part , have a look here

Keep in mind that everybody won't see the new version in the same time. It could take time to be propagated (probably because of cache).

Edit:

Google introduced In-app updates lib. It works on Lollipop+ and gives you the ability to ask the user for an update with a nice dialog (FLEXIBLE) or with the mandatory full-screen message (IMMEDIATE).


You can use the Play Core Library In-app updates to tackle this. You can check for update availability and install them if available seamlessly.

Note that, In-app updates works only with devices running Android 5.0 (API level 21) or higher, and requires you to use Play Core library 1.5.0 or higher.

In-app updates are not compatible with apps that use APK expansion files (.obb files). You can either go for flexible downloads or immediate updates which Google Play takes care of downloading and installing the update for you.

dependencies {
    implementation 'com.google.android.play:core:1.5.0'
    ...
}


EDIT

They have recently changed Google play website and now this code is broken. Avoid this solution or be ready to patch your app whenever Google Play pages change.

Ahmad Arlan's answer is the best answer so far. But if you got here and you try to cut and paste his code you'll go through the same issues I just had, so I might as well just post it here to help others like me.


  1. Make sure you have INTERNET permission on your AndroidManifest.xml file.

    <uses-permission android:name="android.permission.INTERNET"/>
    
  2. Add JSOUP dependency to your module build.gradle.

    dependencies {
        compile 'org.jsoup:jsoup:1.10.2'
    }
    
  3. Surround the snippet with try and catch and don't run it on the main thread.

    public class MainActivity extends AppCompatActivity {
    
        String newVersion;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            new FetchAppVersionFromGooglePlayStore().execute();
        }
    
        class FetchAppVersionFromGooglePlayStore extends AsyncTask<String, Void, String> {
    
            protected String doInBackground(String... urls) {
                try {
                    return
                            Jsoup.connect("https://play.google.com/store/apps/details?id=" + "com.directed.android.smartstart" + "&hl=en")
                            .timeout(10000)
                            .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 (Exception e) {
                    return "";
                }
            }
    
            protected void onPostExecute(String string) {
                newVersion = string;
                Log.d("new Version", newVersion);
            }
        }
    }
    

I posted a copy here of the project on github.