In-App Updates: Getting Started

Learn how to make an in-app mechanism to notify users of a new version of your app. By Carlos Mota.

4.1 (9) · 2 Reviews

Download materials
Save for later
Share

When you create an app, you want to reach the maximum number of users. You want everyone to have the latest version installed, and you want them to use the latest features you’ve developed.

So…how long is this gonna take?

Potentially a while. Many choke points exist between users and the latest version of your app. For example, automatic updates may only validate if the device has a Wi-Fi connection and is not 3G, or the user may have disabled the option.

Both cases require them to go directly to the Play Store and search for updates. Let’s be honest: How many users do you think will take the time?

Thankfully, there’s a better way. You can overcome potential choke points by implementing an in-app update mechanism. This mechanism will notify users when a new version becomes available, so they can download it without any hassle.

Note: This tutorial assumes familiarity with Kotlin. If you’re new to Kotlin development, first check out our tutorial Kotlin For Android: An Introduction.

Getting Started

To download the project materials, click the Download Materials button at the top or bottom of this tutorial. Open the start project in Android Studio 3.5 or later.

In this tutorial, you’ll build an app to display the weather information for your town. More importantly, the app will feature an option to help users maintain the latest version.

screen_store_flexible

Examining the Project’s Structure

Open the project with Android Studio. After it synchronizes, you’ll see a screen like the one below:

screen-android-studio-begin

Compile and run. The app works but will also be mum on key information such as temperature and wind speed.

screen_no_key

Next, survey the different folders and files of the project. Browsing through the explorer on the left, you’ll see a set of subfolders and classes. Starting from the top, they are:

  • api: You’ll use OpenWeatherMap to retrieve the current weather information for your city. These classes provide an abstraction of the entire process.
  • model: You’ll use these classes to parse and hold the information received on the response body.
  • utils: You’ll use a set of functions to get general information — if the location service is enabled or not, formatting measurements, etc.
  • MainActivity.kt: You’ll use this to check if conditions are met to make a weather request. It also updates all the views and is where you’ll write the in-app update logic.

Configuring OpenWeatherMap

You’ll need an API key to request the location to OpenWeatherMap. Don’t worry. It’s free and simple to use.

Follow these steps to create an account:

  1. Go to the OpenWeatherMap API page.
  2. Click the Sign up link in the description.
  3. Create an account.

Then go to API Keys. On this page, you’ll be provided a key. Its value is unique and stored on the account you just created.

screen-openweathermap

Copy the key, and then head back to Android Studio. Open the class OpenWeatherAPI located in the api folder. You’ll see an empty constant called API_KEY. Paste the key there.

private const val API_KEY = "" //Your API key from OpenWeatherMap

The documentation on the openweathermap API can be found at the OpenWeatherMap API section.

Retrieving Weather Data

Enable the location services to retrieve the weather data. Go to the Settings app, and click on Location. Toggle the button to turn it on. It may take a while before your location data is available to the app. So ensure that the app has access to the device location first; otherwise, you will see one of the following messages:

  • Location permissions not granted.
  • Location not available, please enable it on settings.

Build and run. If the previous conditions have been met and your device is connected to the internet, you’ll see a screen similar to this one:

screen_begin

What’s the weather like in your city? :]

Implementing In-App Updates

In-app updates are a complimentary feature of Google Play’s auto-update option. They automatically download and install the latest version of your app.

But since the user defines this behavior, scenarios may arise where in-app updates may not be enough. As such, you need to provide support at the app level to inform users when a new update becomes available.

To implement this feature, your app needs to meet the following requirements:

  • The device needs to run at least Lollipop (API 21+).
  • You need to use Play Core Library 1.5.0 or newer.

At the time of writing, the current version is 1.6.4, so you’ll add this library to the dependencies section of the app’s build.gradle:

implementation 'com.google.android.play:core:1.6.4'

Click Sync Now to download the Google Play Core library.

Time to start using it.

Checking for Updates

Before diving into the different types of updates, you need to find out if one is available.

Add the following to MainActivity after onCreate.

private fun checkForUpdates() {
  //1
  val appUpdateManager = AppUpdateManagerFactory.create(baseContext)
  val appUpdateInfo = appUpdateManager.appUpdateInfo
  appUpdateInfo.addOnSuccessListener {
    //2
    handleUpdate(appUpdateManager, appUpdateInfo)
  }
}

Here’s what’s happening in that code:

  1. To know that a new update is available, you need to create an instance of AppUpdateManager and then register a listener that will be triggered when the app communicates with the Play Store.
  2. Once executed, you’re going to call handleUpdate which will check if the update can be done or not. You will add that shortly.
Note: Press option + enter on a Mac or alt + enter on a PC to import the classes after pasting the code in your IDE if they are not imported automatically. Where asked to choose from more than one package, choose the one from the com.google.android.play.core.* package.

Add the checkForUpdates() to onCreate function. It should be the last instruction in this function. If you call it in onResume, your app will ask the user to update every time it goes back to the foreground. Not exactly the user-friendly experience you’re looking for.

In-app updates can be divided into two types: immediate and flexible. Each one has its distinct update flow, so select the one that corresponds to your app’s requirements.

Currently, the Google Play Console offers no support to define the type of update for a new release. As such, this step has to be done on the app side.

In this tutorial, you’ll practice implementing both types of in-app updates. You can easily choose between one or the other. Start by adding the following before the class declaration in MainActivity:

private const val APP_UPDATE_TYPE_SUPPORTED = AppUpdateType.IMMEDIATE

Then after onCreate, add the following:

private fun handleUpdate(manager: AppUpdateManager, info: Task<AppUpdateInfo>) {
  if (APP_UPDATE_TYPE_SUPPORTED == AppUpdateType.IMMEDIATE) {
    handleImmediateUpdate(manager, info)
  } else if (APP_UPDATE_TYPE_SUPPORTED == AppUpdateType.FLEXIBLE) {
    handleFlexibleUpdate(manager, info)
  }
}

This will verify the type of update that should be done based on that previously defined constant.