In App Update Android Studio Kotlin Code

In today’s fast-paced environment, keeping mobile apps up to date is critical to guaranteeing customer pleasure and app functionality. Google Play offers developers a fantastic feature called In-App updates that allows them to handle updates right within their apps. Users can update their apps using this capability without ever leaving the app. We will demonstrate how to use Kotlin to implement In-App Updates in Android Studio in this guide.

What is an in-app update?

Google Play offers developers the option to notify users to update their apps while they are still being used, a function known as In-App update. By ensuring that users are constantly running the most recent version of the software, it enhances the user experience in general.

Immediate Update: Requires the user to update the application in order to use it further. This kind is appropriate for important updates.

Flexible Update: Updates that are flexible let consumers keep using the app while the update is being downloaded in the background. The user is prompted to restart the application in order to apply the update after the download is finished.

“Build.gradle” Dependencies:

implementation("com.google.android.play:app-update:2.1.0")
implementation("com.google.android.play:app-update-ktx:2.1.0")

Now let me explain the coding part for the In-app-update on Kotlin.

MainActivity.kt

We need to declare two variables on our “MainActivity.kt” Those variables are as follows below

private lateinit var appUpdateManager: AppUpdateManager
private lateinit var activityResultLauncher: ActivityResultLauncher<IntentSenderRequest>

After declaring the variables create a function and yeah you can name it anything you want but I am naming the function “checkForInAppUpdate“. I’ve given the code down here.

    private fun checkForInAppUpdate() {

        appUpdateManager = AppUpdateManagerFactory.create(this)
        activityResultLauncher =
            registerForActivityResult(ActivityResultContracts.StartIntentSenderForResult()) { result: ActivityResult ->
                if (result.resultCode != RESULT_OK) {
                    //If the update is canceled or fails execute particular code? here you can do that
                    Log.d("Update flow failed!", "Result Code:" + result.resultCode)
                }
            }

        //Registering Listener
        appUpdateManager.registerListener(listener)

        val appUpdateInfoTask = appUpdateManager.appUpdateInfo

        appUpdateInfoTask.addOnSuccessListener { appUpdateInfo ->
            if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE && appUpdateInfo.isUpdateTypeAllowed(
                    AppUpdateType.FLEXIBLE
                )
            ) {
                //Request the update here
                appUpdateManager.startUpdateFlowForResult(
                    appUpdateInfo,
                    activityResultLauncher,
                    AppUpdateOptions.newBuilder(AppUpdateType.FLEXIBLE).build()
                )
            }
        }

    }

Below this function you need to create a “Listener” and another function” popupSnackbarForCompleteUpdate” that shows the “Popup SnackBar” to restart the application and Install the downloaded update.

Listener:-

    private val listener = InstallStateUpdatedListener { state ->
        if (state.installStatus() == InstallStatus.DOWNLOADED) {
            popupSnackbarForCompleteUpdate()
        }
    }

popupSnackbarForCompleteUpdate:-

    private fun popupSnackbarForCompleteUpdate() {
        Snackbar.make(
            binding.root, "An update has just been downloaded.", Snackbar.LENGTH_INDEFINITE
        ).apply {
            setAction("RESTART") { appUpdateManager.completeUpdate() }
            setActionTextColor(ContextCompat.getColor(context, R.color.white))
            show()
        }
    }

To make the app more optimized and running effectively we need to use the following lines of code below this function we need to create it. Which are the “onStop()” and “onResume” methods.

onStop() Method:

    override fun onStop() {
        super.onStop()
        appUpdateManager.unregisterListener(listener)
    }

OnResume() Method:

    override fun onResume() {
        super.onResume()
        appUpdateManager.appUpdateInfo.addOnSuccessListener { appUpdateinfo ->
                if (appUpdateinfo.installStatus() == InstallStatus.DOWNLOADED) {
                    popupSnackbarForCompleteUpdate()
                }
            }
    }

Testing the In-App Update

  • Upload the app: You must first upload your app to the Google Play Store. This feature works only for apps hosted on Google Play.
  • Increase the version code: Each time you release an update, increment the versionCode in your build.gradle file.
  • Deploy a new version: Release the updated APK or bundle to Google Play.
  • Install the old version: Download the previous version from Google Play to simulate an update.

Conclusion

Google Play offers a useful feature called in-app updates that makes sure consumers are always using the most recent version of your program. Adding this functionality to your Android app with Kotlin is easy and offers a smooth update experience for the app’s users. With the help of this tutorial, you can quickly add in-app updates to your Android app, giving users a simple and unobtrusive way to get updates without ever having to exit the program.

For more tutorials and in-depth guides, stay tuned to our website! Happy coding!

Reference Linkhttps://developer.android.com/guide/playcore/in-app-updates/kotlin-java
Date – 21/09/2024

Related Posts

Leave a Reply