In this post and the accompanying video, I will show you how to implement an “auto-app update” feature directly within your application’s code. This ensures that whenever you release an update on the Google Play Store, it automatically reaches users, prompting them to update the app to continue using it. You can easily implement this method in your application using Android Studio.
First, we will create the project. Then, we will go to the dependencies section and implement the auto-update dependency. After that, we will implement the logic in Java. For now, you need to add the dependency to your project; the required dependency is provided below, so simply copy it and paste it into your project.
// Update Play Core API Dependency
implementation("com.google.android.play:app-update:2.1.0")
Now, navigate to your project’s home activity—the main screen users see when they open the app. Here, we will implement specific variables to facilitate a smooth auto-update process for the application. These variables will assist in enabling the auto-update functionality.
private AppUpdateManager mAppUpdateManager;
private static final int RC_APP_UPDATE = 100;
Next, we are going to implement some code within the onCreate method of our project to support the update method. Simply copy the code provided below and paste it directly into the onCreate method.
mAppUpdateManager = AppUpdateManagerFactory.create(this);
mAppUpdateManager.getAppUpdateInfo().addOnSuccessListener(appUpdateInfo -> {
if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE &&
appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)) {
try {
mAppUpdateManager.startUpdateFlowForResult(appUpdateInfo, AppUpdateType.IMMEDIATE, MainActivity.this
, RC_APP_UPDATE);
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
}
});
Now, regarding the remaining code—which we will implement outside the onCreate method—you need to copy the entire block of code I have provided below and paste it outside the onCreate method within your AppCompatActivity. Afterward, we will resolve any errors that arise and demonstrate how to implement this in the project.
private final InstallStateUpdatedListener installStateUpdatedListener = installState -> {
if (installState.installStatus() == InstallStatus.DOWNLOADED) {
showCompleteUpdate();
}
};
private void showCompleteUpdate() {
Snackbar snackbar = Snackbar.make(findViewById(android.R.id.content), "New App is Ready!",
Snackbar.LENGTH_INDEFINITE);
snackbar.setAction("INSTALL", v -> mAppUpdateManager.completeUpdate());
snackbar.show();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if (requestCode == RC_APP_UPDATE && resultCode != RESULT_OK) {
Toast.makeText(this, "CANCEL", Toast.LENGTH_SHORT).show();
}
super.onActivityResult(requestCode, resultCode, data);
}
@Override
protected void onStop() {
super.onStop();
}
@Override
protected void onResume() {
super.onResume();
mAppUpdateManager.getAppUpdateInfo().addOnSuccessListener(appUpdateInfo -> {
if (appUpdateInfo.updateAvailability() == UpdateAvailability.DEVELOPER_TRIGGERED_UPDATE_IN_PROGRESS) {
try {
mAppUpdateManager.startUpdateFlowForResult(appUpdateInfo, AppUpdateType.IMMEDIATE, MainActivity.this
, RC_APP_UPDATE);
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
}
});
}
We have now made the code implemented in our product completely error-free. You can use it for your project, update your application via the Google Play Console, and notify your users about the update.