In-Game App Updates via Deep Links

Overview

Ensuring that users are directed back to the original app store for updates is crucial for maintaining a consistent and expected user experience.

This guide provides developers with the essential information and code snippets needed to implement in-game app updates through deep links effectively. By accurately identifying the origin of the app installer and confirming its presence on the device, we can ensure users are redirected to the correct store for updates. This process guarantees that the app version being updated preserves the original billing agreements and settings, maintaining consistency and trust in the user experience.

Step 1: Determine the App Installer Origin

To ensure we redirect the user to the correct app store, we first need to identify the installer's package name. Use the following function to retrieve this information:

fun PackageManager.getInstallerInfo(YOUR_APP_PACKAGE_NAME: String): String? {
  return try {
    val installerPackageName = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
      getInstallSourceInfo(YOUR_APP_PACKAGE_NAME).installingPackageName
    } else {
      getInstallerPackageName(YOUR_APP_PACKAGE_NAME)
    }

    installerPackageName?.takeIf { isAppInstalled(it) }
  } catch (e: Exception) {
    e.printStackTrace()
    null
  }
}

fun PackageManager.isAppInstalled(packageName: String): Boolean {
  return try {
    getPackageInfo(packageName, PackageManager.GET_META_DATA)
    true
  } catch (e: PackageManager.NameNotFoundException) {
    false
  }
}

Note: Replace YOUR_APP_PACKAGE_NAME with your application's package name.

Step 2: Retrieve the Deep Link URL

With the installer package name, make a request to your backend endpoint. Substitute {app-package} with your app's package name and {store-package} with the installer's package name retrieved from Step 1.

GET store-link-mapper.aptoide.com/deeplink/{app-package}?store-package={store-package}

The endpoint will return a URL containing the correct deep link to the app store page where users can update the app.

Step 3: Launch the Deep Link

Once you have the deep link URL, use the following function to launch it, directing the user to the app store for an update:

fun launchDeeplink(deeplink: String, context: Context) {  
  val deeplinkIntent = Intent(Intent.ACTION_VIEW, Uri.parse(deeplink)).apply {  
    addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)  
    addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)  
    context.packageManager.getInstallerInfo()?.let {  
      `package` = it  
    }  
  }

  try {  
    context.startActivity(deeplinkIntent)  
  } catch (e: ActivityNotFoundException) {  
    e.printStackTrace()  
  }  
}

Note: To make sure the intent is directly handle by the correct store, set the store package for the intent

Best Practices

Always check for network connectivity before making backend calls.
Handle exceptions and errors gracefully, providing fallback options or user guidance as necessary.
Test the deep link implementation across different devices and installer sources to ensure compatibility and reliability.