Android — Deep link and App link

Deepak Goyal
5 min readFeb 21, 2021

In product development, we want that our users visit a certain screen of the application directly via links. This will have a better user experience and save the time of users.

Deep links are the links that take users to specific content in the application. In this article, I will try to explain how to achieve that in Android.

With the help of Intent Filters, we can set up the deep links in the Android application.

Firebase Dynamic Links

I have created 2 dynamic links in Firebase, Screen 1 and Screen 2 that will open the respective screens in the Android app.

Intent Filters in App

Now let’s set up the intent filter in the app manifest for the domain “deepakdroid.page.link”

By adding the Intent filter for the host deepakdroid.page.link in the app, means that the app can handle the URLs with the domain deepakdroid.page.link

Open the Links in App

Added the Intent to open a certain link by clicking on the button.

Now Let’s run the app.

As we can see in the above video when clicked on buttons “Open Screen 1” and “Open Screen 2”, a dialog is opened to choose between Chrome and the application. This is an Intent chooser dialog to choose one of the listed applications by which you want to handle this action. Our app is listed here because in the manifest we added an Intent Filter for the domain “deepakdroid.page.link” which tells the system that our app can handle this data.

But we want that the URLs from the domain “deepakdroid.page.link” must be handled by our app as default if our app is already installed. We don’t want this disambiguation dialog. For that, we have to set our app as the default handler in the System.

There are 2 ways, one is manual and the other is automatic.

Manual setting the default handler for domain

The better and recommended way is automatic as users don’t have to perform this action. In an automatic way, we have to allow the system to verify that the domain “deepakdroid.page.link” belongs to the app itself.

The deep link that has been verified, is called the Android App link.

Verification of Deep Links:

  • Add the android:autoVerify=”true” to the Intent Filter.
[{
"relation": ["delegate_permission/common.handle_all_urls"],
"target": {
"namespace": "android_app",
"package_name": "com.sample.sample_deep_app_link",
"sha256_cert_fingerprints": ["BE:B1:86:E8:D6:B9:EF:71:C4:28:1E:94:FB:C1:AF:03:72:C7:69:20:51:F3:33:78:64:6C:96:06:47:30:B1:29"]
}
}]

In the sample app, the domain is from Firebase dynamic link (FDL). Firebase automatically add the above information for verification at required path. To read about the FDL in details, check the documentation at https://firebase.google.com/docs/dynamic-links

For detailed information about the verification of deep link, check the documentation at https://developer.android.com/training/app-links/verify-site-associations

After that, you have to run the app and wait for some seconds to allow the system to verify the ownership of the URL.

Check link policies

We can check what is the current system settings for handling the link by the app. Run the below command in the terminal, before and after setting up the verification process.

adb shell dumpsys package domain-preferred-apps

Before Link Verification:

Package: com.sample.sample_deep_app_link
Domains: deepakdroid.page.link
Status: ask

After Link Verification:

Package: com.sample.sample_deep_app_link
Domains: deepakdroid.page.link
Status: always

ask = The system will show the Intent chooser dialog

always = The system will always use this app to handle the listed domains.

Now if you run the app you will see that there is no Intent chooser (“disambiguation”) dialog. By clicking on the buttons “Open Screen 1” and “Open Screen 2”, App Main Activity is opened again and again. Check the below video:

To avoid the opening of Main Activity again and again, you need to set up launch modes for the activity in the manifest and handle the code in onNewIntent accordingly. Read more at https://developer.android.com/guide/components/activities/tasks-and-back-stack

In the video, we saw that our app is getting opened to handle the links, great :), but the specific screen is not getting opened. This is because we are not reading the data from coming intents.

Read Intent Data

To read the data from Intent, we have to write the following code in the MainActivity.

Intent intent = getIntent();
String action = intent.getAction();
Uri data = intent.getData();

Final Working of the App

Important Points:

  1. App links only work for the scheme HTTP or HTTPS
  2. App links require Android 6.0 and higher
  3. For App links, the Intent action must be android.intent.action.VIEW in Intent Filter
  4. For App links, the Intent category must be an android.intent.category.BROWSABLE and android.intent.category.DEFAULT in Intent Filter
  5. If you have more than one domain/host added in the Intent Filter that meets the App links criteria then the app will try to verify all the domains/hosts. If any one of the domains/hosts is not verified then the status to handle domain will not be set to “always”.
  6. After link verification, If the user doesn’t want the app to be the default handler, they can override this behavior from their device’s system settings.

Source Code

Hope you have learned something from this article. If you have any query please let me know. I will be more than happy to help you.

Happy Coding :)

--

--

Deepak Goyal

I’m a Software Engineer. I love programming. #java #android #kotlin #dart #flutter #firebase