Official Documentation: https://developer.android.com/guide/app-bundle/in-app-updates
Constraint: In-app update works only with devices running Android 5.0 (API level 21) or higher
Step 1: Add dependency:
dependencies {
implementation 'com.google.android.play:core:1.5.0'
...
}
Step 2: Check for update availability and start if it's available
Create an instance of the AppUpdateManagerFactory
appUpdateManager = AppUpdateManagerFactory.create(context);
Register the listener for the response of the FLEXIBLE update Request
appUpdateManager.registerListener(listener);
After that gets the appUpdateInfo
Task<AppUpdateInfo> appUpdateInfo = appUpdateManager.getAppUpdateInfo();
appUpdateInfo.addOnSuccessListener(new OnSuccessListener<AppUpdateInfo>() {
@Override
public void onSuccess(AppUpdateInfo appUpdateInfo) {
if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE) {
if (appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE)) {
Log.d("App update A", "Flexible");
int updateType = FLEXIBLE;
requestUpdate(appUpdateInfo, updateType);
} else if (appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)) {
Log.d("App update B", "IMMEDIATE");
int updateType = IMMEDIATE;
requestUpdate(appUpdateInfo, updateType);
}
}
}
});
After that requestUpdate() method,
private void requestUpdate(AppUpdateInfo appUpdateInfo, int updateType) {
try {
appUpdateManager.startUpdateFlowForResult(appUpdateInfo, updateType, HomeActivity.this, MY_REQUEST_CODE);
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
}
Step 3: Listen to update state
Lastly the listener
InstallStateUpdatedListener listener = new InstallStateUpdatedListener() {
@Override
public void onStateUpdate(InstallState state) {
Log.d("installState", state.toString());
if (state.installStatus() == InstallStatus.DOWNLOADED) {
// After the update is downloaded, show a notification
// and request user confirmation to restart the app.
// SnackBarManager.getSnackBarManagerInstance().showSnackBar(GaanaActivity.this, "An update has just been downloaded.", true);
popupSnackbarForCompleteUpdate();
}
}
};
popupSnackbarForCompleteUpdate() method,
private void popupSnackbarForCompleteUpdate() {
Snackbar snackbar =
Snackbar.make(findViewById(android.R.id.content), "An update has just been downloaded.", Snackbar.LENGTH_INDEFINITE);
snackbar.setAction("Restart", view -> appUpdateManager.completeUpdate());
snackbar.setActionTextColor(getResources().getColor(android.R.color.white));
snackbar.show();
}
Step 4: Get a callback for update status
You can capture the Result as well,
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == MY_REQUEST_CODE) {
System.out.println("App Update = " + resultCode);
if (resultCode != RESULT_OK) {
System.out.println("Update flow failed! Result code: " + resultCode);
// If the update is cancelled or fails,
// you can request to start the update again.
}
}
}
You have to unregister the listener as well
@Override
public void onDestroy() {
super.onDestroy();
Log.d("installState", "destroy");
appUpdateManager.unregisterListener(listener);
}