Smart Way to Integrate Deep Linking in Android
11 April 2020
MOriginally published on Medium ↗Are you new to deep link integration? Here are some tips and tricks to integrate deep links into your Android app.
In this article I explain how you can write deep-linking code in an easy, smart way, and walk through some of the problems I solved my own way.
We’ll start with basic deep linking. You can also read the official guide: Create deep links to app content.
Let’s start
Add intent filters for incoming links. To create a link to your app content, add an intent filter with these elements and attribute values in your manifest.
Add the scheme, host and path prefix as per the URI. You can add more intent filters in a specific activity as per your conditions — intent filters always differ by the <data> element.
Example:
<data android:scheme="http"
android:host="www.example.com"
android:pathPrefix="/Kartik" />
Full example with intent filters:
<activity
android:name="com.example.android.KartikActivity"
android:label="@string/title_Kartik">
<intent-filter android:label="@string/filter_view_http_Kartik">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<!-- Accepts URIs that begin with "http://www.kartiksetia.com/Kartik" -->
<data android:scheme="http"
android:host="www.kartiksetia.com"
android:pathPrefix="/Kartik" />
<!-- note that the leading "/" is required for pathPrefix -->
</intent-filter>
</activity>
After this, whenever http://www.kartiksetia.com/Kartik is hit, the app opens KartikActivity.
Case 1 — only the app, only verified domains
When you don’t want the user to choose between the browser and the app, and you want your app to open only via verified domains, App Links are the answer.
Read Verify Android App Links to integrate them. You just need to add one attribute in your Android code:
<intent-filter android:autoVerify="true">
and add a JSON file to your domain’s server, for example:
https://kartiksetia.com/.well-known/assetlinks.json
You can test the backend with Google’s tool, which checks that every domain in your assetlinks.json file is accessible: Statement List Generator and Tester.
Case 2 — no web app, only your app
When you don’t have a web app, or you don’t want your deep link to open any web page but only your app.
The use case: if the app isn’t installed on the device, the link redirects to the Play Store with the parameters you want to pass, and the app reads them once it’s installed.
The workflow: on the server side, create a redirect rule to Google Play. For example,
https://www.kartiksetia.com/kartik/BlahBlah
redirects to
https://play.google.com/store/apps/details?id=com.bar.kartiksetia&referrer=BlahBlah
Here BlahBlah is your parameter. You can use the Play Install Referrer library to fetch it in the app.
Hope this helps. Happy coding :)
This post first appeared on Medium. Read it there and leave a clap ↗
