Deep Linking in Android: Best Practices and Common Pitfalls

What is Deep Linking?


Deep linking is a crucial feature that allows apps to be opened directly to specific content or actions from external sources such as websites, emails, or social media. In Android, deep linking can enhance user experience by providing seamless navigation to specific screens within the app, even when it is not currently running. We’ll explore the best practices for implementing deep links in Android apps and tools like
Appsflyer while also addressing common pitfalls you might encounter.
At its core, a deep link is a hyperlink that navigates users to specific content within a mobile app, rather than a generic homepage or landing page. This allows for precise targeting, bringing users to a particular screen, product, or action within the app. But how do these links work under the hood? Let’s break it down.

Deep links can be divided into the following types:

  1. Basic Deep Links: These are URLs that can open an app if it’s installed, but will fallback to a web page if the app isn’t installed.
  2. Deferred Deep Links: These URLs not only open the app but also direct users to specific content even if the app isn’t installed yet (they are prompted to install the app first).
  3. Universal Links (App Links): Supported by Android 6.0 and above, these links can open either the app or fallback to the web, but they work without user prompts and improve security by requiring website verification.

 

Deep Linking Mechanism: How It Works

When a user taps on a deep link, the following process occurs:

  • URL Parsing: The deep link is typically a URL, and when it’s clicked, the operating system (in this case, Android) parses the URL to identify its components like the scheme (https), host (www.example.com), and path (/content/id).
  • Intent Resolution: Android then checks whether any apps are capable of handling this URL. Apps declare their ability to handle specific URLs in the AndroidManifest.xml file using intent-filters.
  • App Launch/Resume: If the user has the app installed and it matches the deep link, Android launches (or resumes) the app and passes the URL data to the app. This allows the app to navigate to the specific content or action represented by the link.
  • Fallback to Web: If no app is capable of handling the deep link, the system either falls back to a web browser and opens the link in a browser (for basic deep links) or prompts the user to install the app (in the case of deferred deep links or Universal Links).
Example of a Deep Link

In this example, when clicked, the deep link triggers an Android system intent. If your app has registered for this URL (via intent-filter), it will open your app and extract id=123 from the query parameters, guiding the user directly to a product details page.

How Android Resolves Deep Links

Deep linking works with the Android Intent system. When a user clicks on a deep link, Android checks the following:

  • Intent Filters: The system looks for any apps that can handle the link based on the intent filter defined in the app’s AndroidManifest.xml. If your app has registered an intent filter matching the URL scheme, it will be invoked.
  • Scheme Matching: Android uses the scheme, host, and path defined in your intent-filter to match the incoming URL. For example, if your app declares a scheme like https and host www.yourdomain.com, Android will route any URLs with that structure to your app.
Here’s an example of how Android resolves a deep link based on an intent filter
  • Intent Delivery: If a match is found, the system sends an implicit intent to the app, which delivers the data (URL) to the activity that handles the deep link. In your app, you can access the deep link data using getIntent().getData() or directly in the onCreate() method of your activity.

Implementing Deep Linking in Android

Step 1: Defining Deep Links in the Manifest
The first step in enabling deep linking is to define the intent filters in your AndroidManifest.xml file for the activity that will handle the deep links.
Step 2: Handling the Deep Link in Activity
Next, you’ll need to handle the deep link intent within your activity by extracting the data and navigating to the appropriate screen or content.Step 3: Using Appsflyer for Deferred Deep Linking
To handle deferred deep linking (when the app is not installed), you can integrate Appsflyer. It tracks deep links even when the app is not installed, guiding users to install it first.
Step 3.1: Add the Appsflyer SDK
First, add the Appsflyer SDK to your project:

Step 3.2: Initialize Appsflyer in the Application Class
Initialize Appsflyer in the Application class to capture install events and deferred deep linking:

Step 3.3: Handling Deferred Deep Links
To handle deferred deep links, use the data passed through Appsflyer’s listener:Step 4: Testing Deep Links
You can test deep linking in your app using the following methods:

ADB Commands: You can simulate deep links using ADB.

Step 5: Appsflyer Dashboard: You can create custom deep linking campaigns in the Appsflyer dashboard and test deferred deep links by clicking on the links from different devices.

Common Pitfalls

1. Missing Intent Filters
One common mistake is failing to define the correct intent-filter in your AndroidManifest.xml. Without proper configuration, the deep link will not be triggered.
2. Ignoring Security Concerns
When using deep links, it’s important to ensure your app is protected against unwanted intent injections. Always validate and sanitize the data received from the deep link.
3. Incorrect Path Matching
Make sure that the path defined in the intent-filter matches the actual deep link. Using incorrect path prefixes or schemes can prevent your deep link from working.
4. Deferred Deep Linking Issues
Sometimes, deferred deep links may not work correctly if the Appsflyer SDK is not properly initialized, or if conversion data is not handled correctly.
5. Handling Background and Killed States
Ensure you handle cases where the app is in the background or killed state when a deep link is opened. This can affect user experience and navigation if not managed properly.

Conclusion


Deep linking is a powerful tool for enhancing user experience by driving users to specific content within your Android app. By following the best practices outlined here and avoiding common pitfalls, you can ensure seamless deep linking for both basic and deferred cases. Additionally, using tools like Appsflyer simplifies the implementation of deferred deep linking and attribution, providing insights into how users are navigating your app.

Leave a Reply