A deep link is a hypertext link to a page on a Website other than its homepage. In the context of mobile apps, deep linking consists of using a uniform resource identifier (URI) that links to a specific location within a mobile app rather than simply launching the app. In this post, we are going to implement Deep Linking in Android App using Branch’s Contextual Deep Links.
But Why Branch?
To understand the advantages of using Branch Deep Links, first of all, we have to understand how Traditional Mobile Deep Links work.
Traditional Deep Links:
Mobile Deep Linking make use of Custom URI Schemes. Custom URI schemes were the original form of deep linking for mobile apps. They are like creating a “private internet” for your app, with links that look like myapp://path/to/content. The advantage of custom URI schemes is they are easy to set up and most apps already have one. The disadvantage is a user’s device only knows about this “private internet” if the corresponding app is already installed, and there is no graceful fallback option by default.
Traditional Deep Links make use of these URI Schemes and thus Traditional deep links can route users to app content as long as the app is already installed when the link is opened. This means traditional deep links don’t work if the user doesn’t have the app, and will show either an error or a fallback page.
Branch Deep Links:
To counter the disadvantages of Traditional Deep Links, Deferred Deep Links came into the picture. Deferred deep links can route users to content even if the app is not installed when the link is opened. The link will first redirect to the App Store or Play Store to download the app, and then take the user to the specific “deferred” content immediately after the first launch.
Branch Deep Links uses Contextual Deep Links. Contextual deep links have all the functionality of deferred deep links, plus much more. Contextual deep links store information about where a user wants to go, where the link was clicked, who originally shared the link, and an almost unlimited amount of custom data.
I must now assume that you have understood the difference between Traditional and Branch Deep Links. And thus why we are going to use Branch to implement Deep Linking in our Android App?
To know more about Traditional Deep Links and Branch Deep Links, you can visit below-given URLs.
Sign Up on Branch.io
First of all. sign up for an account on Branch.io.
Then log in to Branch.io account using above credentials.
Understanding Branch Keys
- Go to Account Settings on the Branch Dashboard
- There you will find Branch Keys. Branch Keys allow you to interact with your Branch SDKs and create deep links. These keys are unique to your Branch app.
- Never expose your Branch Secret as it can be maliciously used.
- One more thing there are two Branch Keys for your app. One for the live environment and other for the testing environment i.e if your app is available on Play Store then use Live Branch Key otherwise use Test Branch key.
- You can toggle between each environment using button given on top-left corner of your Branch Dashboard.
- Similarly, you can find App Id and App Name for respective environments.
I will be using Branch’s Testing environment credentials for this tutorial. But you can choose as per your requirements.
Configure Link Settings
I am going to explain all the necessary steps to implement Branch Deep Links in your Android app. You can configure extra settings as per your requirements.
- In this step, we are going to configure default behavior for our Deep Links and various other Link Settings.
- Go to Link Settings on your Branch Dashboard.
- Under Link Settings, you can configure link behavior and settings for different platform Like iOS, Android, Desktop etc. But we are going to configure only for Android platform. You can configure for other platforms also if you wish.
- Under Android settings, enable I have an Android App and Enable App Links.
- Now choose a URI Scheme for your app in given format. And keep in mind that your App’s URI scheme should be unique so that it can be uniquely identified on Android platform.
- Fill the URL and package name for your app if it is available on Play Store.
- Fill SHA256 Cert Fingerprint to enable App Links. The process to find SHA256 fingerprint is explained in STEP 4.
- Note that Android App Links only work on Android 6+. You can read more about Android App Links on the internet.
- Now go to the bottom of Link Settings page and Choose a link domain which will be used for all your links.
- The link domain is the website which hosts your deep links.
Generate signing certificate fingerprint
Follow the steps given below to generate your signing certificate fingerprint.
- Navigate to your keystore file. This is the file that you use to build the debug and the production version of your APK file before deploying it.
- For the app still in development, go to Debug Keystore file. Otherwise, go to Release Keystore file. And then copy the path of that file.
- Now open the command prompt (cmd) and change directory to above path.
- Run this command on it to generate the fingerprint: Change ****** with your file name.
- You’ll see a value like come out the other end. Copy this.
- Paste the copied fingerprint value into the SHA256 Cert Fingerprints field that appears in Link Settings under Android.
Configure your Android App
Now we are going to set up Branch Deep Links in your Android App. Open your App and follow the points given below.
Import the Branch SDK to your build.gradle by adding below-given dependencies to your app-level build.gradle file.
Update AndroidManifest.xml File
Update your App’s AndroidManifest file as given below. Some points to consider are as follows.
- Make a custom Application Class if you have not already and replace your custom application class with its name.
- We will need custom Application class in upcoming points as you will see.
- We have defined android:launchMode=”singleTask” on our App’s MainActivity. Read about it on the internet if you don’t know.
Replace the following with values from your Branch Dashboard
- your app package name : Your Android App’s package name
- your custom application class : Your Custom Application class name
- your URI scheme : Your Android URI Scheme
- your link domain : Your Link Domain
- your branch live key : Your Branch Live Key from Step 1
- your branch test key : Your Branch Test Key from Step 1
In your Custom Application class load branch as given below.
Add Branch to your MainActivity.java
- Only initialize Branch in the Launcher/Main activity. Because app will open through the Launcher activity, where Branch will initialize and retrieve the deep link data from the link click.
- Always initialize Branch in onStart(). As initializing Branch in other Android lifecycle methods, like onResume(), will lead to unintended behavior. onStart() is what makes the activity visible to the user, as the app prepares for the activity to enter the foreground and become interactive
- The below-given code is just to get you started. Here you can retrieve information from Deep Links and proceed as per your requirements i.e launch intended activity.
Create Branch Links
You can create Branch Deep Links from within your App as described below or from Branch Dashboard. Follow the below-given code to create links from within App.
- I have created a separate class for creating Branch Links. Inside that class, I have defined a method which takes context and other parameters necessary to create Branch Deep Link. Inside that method
- First of all, we will create a content reference using Branch Universal Object.
- Then we will define Link properties.
- Then we will finally create the Deep Link. Here I have used Android’s Share Bottom Sheet but you can also use Branch’s ShareSheetStyle if you like. Read Branch Docs for more info.
Now I have given you a brief tour on “How to create Branch Deep Links for your Android App”. It covers all the necessary steps to get you started. But it is just the tip of all that can be achieved with Branch. To explore more about Branch Links, read through Branch Developer Docs below.
If you face any problem in the tutorial above, you can always leave a reply or reach me through the chat option given in bottom right corner of the window.