How to Add Android App Links With Redirects

Learn how to configure Android App Links to handle redirects and deep link │ users directly to your app's content.

Intermediate

Android App Links provide a seamless user experience by allowing HTTP/HTTPS URLs to open directly into your app, bypassing the browser's disambiguation dialog. They also gracefully fall back to the browser if your app isn't installed. Implementing them, especially with redirects, involves several key steps.

Here's how to add Android App Links with redirects:

1. Configure Your AndroidManifest.xml

You need to declare an intent filter in your AndroidManifest.xml file for the activity that will handle the incoming links. This intent filter tells the Android system which URLs your app can open.

  • android:autoVerify="true": This crucial attribute enables automatic verification of your app as the default handler for the specified URL patterns.
  • <action android:name="android.intent.action.VIEW" />: This specifies that the intent filter can be reached from Google Search and handles viewing actions.
  • <category android:name="android.intent.category.DEFAULT" />: This allows your app to respond to implicit intents.
  • <category android:name="android.intent.category.BROWSABLE" />: This is required for the intent filter to be accessible from a web browser, allowing users to open your app from a deep link in a browser.
  • <data> tag: This defines the URI format your app can handle. You'll specify the scheme (e.g., https), host (your domain), and optionally a path or path prefix.

Example AndroidManifest.xml entry:

<activity android:name=".DeepLinkActivity">
    <intent-filter android:autoVerify="true">
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="https"
              android:host="www.example.com"
              android:pathPrefix="/products" />
    </intent-filter>
</activity>

This configuration means your app can handle URLs like https://www.example.com/products/item123.

2. Declare Website Association with assetlinks.json

To verify that your app is the legitimate owner of the URLs it claims to handle, you must host a Digital Asset Links JSON file on your website. This file establishes a secure association between your website and your Android app.

  1. Create assetlinks.json: The file should contain your app's package name and SHA256 certificate fingerprint.

    json [{ "relation": ["delegate_permission/common.handle_all_urls"], "target": { "namespace": "android_app", "package_name": "com.your.packagename", "sha256_cert_fingerprints": ["YOUR_SHA256_FINGERPRINT"] } }] * package_name: Your Android application ID. * sha256_cert_fingerprints: The SHA256 fingerprint of the signing key used to build your APK. You can obtain this from your Google Play Console (if using Play App Signing) or by using the keytool command for local keystores.

  2. Host the file: Upload this assetlinks.json file to your web server at https://your.domain.com/.well-known/assetlinks.json. Ensure it's accessible via HTTPS.

3. Handle the Incoming Intent in Your Activity

In the Android activity specified in your AndroidManifest.xml, you'll retrieve the incoming Intent data to determine what content to display.

class DeepLinkActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_deep_link)

        intent?.data?.let { uri ->
            // uri will be something like https://www.example.com/products/item123
            val productId = uri.lastPathSegment // Extracts "item123"
            // Use productId to fetch and display relevant content
            findViewById<TextView>(R.id.textView).text = "Product ID: $productId"
        }
    }

    // Handle new intents if the activity is already running
    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        setIntent(intent) // Update the activity's intent
        // Process the new intent data here, similar to onCreate
        intent?.data?.let { uri ->
            val productId = uri.lastPathSegment
            findViewById<TextView>(R.id.textView).text = "New Product ID: $productId"
        }
    }
}

4. How Redirects Work with Android App Links

Android App Links are designed to work seamlessly with HTTP/HTTPS redirects. When a user clicks a URL that redirects, the Android system follows the redirect chain. The crucial point is that the final URL after all redirects must match the intent-filter configuration in your AndroidManifest.xml and be covered by your assetlinks.json file.

  • Server-Side Redirects: If your web server performs a 301 (permanent) or 302 (temporary) redirect, the Android system will follow it. For example, if https://www.example.com/old-product redirects to https://www.example.com/products/item123, and your app is configured to handle https://www.example.com/products/, the app will open with item123.
  • Fallback Behavior: If the app is not installed on the user's device, or if the domain verification fails, the link will open in a web browser.