# DeepLinkDispatch **Repository Path**: jfcoder/DeepLinkDispatch ## Basic Information - **Project Name**: DeepLinkDispatch - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-05-29 - **Last Updated**: 2024-11-27 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # DeepLinkDispatch [![Build Status](https://travis-ci.org/airbnb/DeepLinkDispatch.svg)](https://travis-ci.org/airbnb/DeepLinkDispatch) DeepLinkDispatch provides a declarative, annotation-based API to define application deep links. You can register an `Activity` to handle specific deep links by annotating it with `@DeepLink` and a URI. DeepLinkDispatch will parse the URI and dispatch the deep link to the appropriate `Activity`, along with any parameters specified in the URI. ### Example Here's an example where we register `SampleActivity` to pull out an ID from a deep link like `example://example.com/deepLink/123`. We annotated with `@DeepLink` and specify there will be a parameter that we'll identify with `id`. ```java @DeepLink("example://example.com/deepLink/{id}") public class SampleActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent intent = getIntent(); if (intent.getBooleanExtra(DeepLink.IS_DEEP_LINK, false)) { Bundle parameters = intent.getExtras(); String idString = parameters.getString("id"); // Do something with idString } } } ``` ### Multiple Deep Links Sometimes you'll have an Activity that handles several kinds of deep links: ```java @DeepLink({"foo://example.com/deepLink/{id}", "foo://example.com/anotherDeepLink"}) public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent intent = getIntent(); if (intent.getBooleanExtra(DeepLink.IS_DEEP_LINK, false)) { Bundle parameters = intent.getExtras(); String idString = parameters.getString("id"); // Do something with idString } } } ``` ### Method Annotations You can also annotate any `public static` method with `@DeepLink`. DeepLinkDispatch will call that method to create the `Intent` and will use it when starting your `Activity` via that registered deep link: ```java @DeepLink("foo://example.com/methodDeepLink/{param1}") public static Intent intentForDeepLinkMethod(Context context) { return new Intent(context, MainActivity.class) .setAction(ACTION_DEEP_LINK_METHOD); } ``` If you need access to the `Intent` extras, just add a `Bundle` parameter to your method, for example: ```java @DeepLink("foo://example.com/methodDeepLink/{param1}") public static Intent intentForDeepLinkMethod(Context context, Bundle extras) { Uri.Builder uri = Uri.parse(extras.getString(DeepLink.URI)).buildUpon(); return new Intent(context, MainActivity.class) .setData(uri.appendQueryParameter("bar", "baz").build()) .setAction(ACTION_DEEP_LINK_METHOD); } ``` If you're using Kotlin, make sure you also annotate your method with `@JvmStatic`. `companion objects` will *not work*, so you can use an `object declaration` instead: ```kotlin object DeeplinkIntents { @JvmStatic @DeepLink("https://example.com") fun defaultIntent(context: Context, extras: Bundle): Intent { return Intent(context, MyActivity::class.java) } } ``` If you need to customize your `Activity` backstack, you can return a `TaskStackBuilder` instead of an `Intent`. DeepLinkDispatch will call that method to create the `Intent` from the `TaskStackBuilder` last `Intent` and use it when starting your `Activity` via that registered deep link: ```java @DeepLink("http://example.com/deepLink/{id}/{name}") public static TaskStackBuilder intentForTaskStackBuilderMethods(Context context) { Intent detailsIntent = new Intent(context, SecondActivity.class).setAction(ACTION_DEEP_LINK_COMPLEX); Intent parentIntent = new Intent(context, MainActivity.class).setAction(ACTION_DEEP_LINK_COMPLEX); TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(context); taskStackBuilder.addNextIntent(parentIntent); taskStackBuilder.addNextIntent(detailsIntent); return taskStackBuilder; } ``` ### Query Parameters Query parameters are parsed and passed along automatically, and are retrievable like any other parameter. For example, we could retrieve the query parameter passed along in the URI `foo://example.com/deepLink?qp=123`: ```java @DeepLink("foo://example.com/deepLink") public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent intent = getIntent(); if (intent.getBooleanExtra(DeepLink.IS_DEEP_LINK, false)) { Bundle parameters = intent.getExtras(); if (parameters != null && parameters.getString("qp") != null) { String queryParameter = parameters.getString("qp"); // Do something with the query parameter... } } } } ``` ### Configurable path segment placeholders Configurable path segment placeholders allow your to change configured elements of the URL path at runtime without changing the source of the library where the deeplink is defined. That way a library can be used in multiple apps that are still uniquely addressable via deeplinks. They are defined by encapsulating an id like this `` and are only allowed as a path segment (between two slashes. `/`: ```java @DeepLink("foo://cereal.com//nutritional_info") public static Intent intentForNutritionalDeepLinkMethod(Context context) { return new Intent(context, MainActivity.class) .setAction(ACTION_DEEP_LINK_METHOD); } ``` If you do this you do have to provide a mapping (at runtime) for which values are allowed for creating a match. This is done when you new the `DeeplinkDelegate` class like: ```java @DeepLinkHandler({ AppDeepLinkModule.class, LibraryDeepLinkModule.class }) public class DeepLinkActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Configure a map for configurable placeholders if you are using any. If you do a mapping // has to be provided for that are used Map configurablePlaceholdersMap = new HashMap(); configurablePlaceholdersMap.put("type_of_cereal", "obamaos"); // DeepLinkDelegate, LibraryDeepLinkModuleRegistry and AppDeepLinkModuleRegistry // are generated at compile-time. DeepLinkDelegate deepLinkDelegate = new DeepLinkDelegate(new AppDeepLinkModuleRegistry(), new LibraryDeepLinkModuleRegistry(), configurablePlaceholdersMap); // Delegate the deep link handling to DeepLinkDispatch. // It will start the correct Activity based on the incoming Intent URI deepLinkDelegate.dispatchFrom(this); // Finish this Activity since the correct one has been just started finish(); } } ``` This app will now match the Url `foo://cereal.com/obamaos/nutritional_info` to the `intentForNutritionalDeepLinkMethod` method for that app. If you build another app and set `type_of_cereal` to `captnmaccains` that apps version of the `intentForNutritionalDeepLinkMethod` would be called when when opening `foo://cereal.com/captnmaccains/nutritional_info` If you are using configurable path segment placeholders, a mapping has to be provided for every placeholder used. If you are missing one the app will crash at runtime. #### Empty configurable path segment placeholders mapping A mapping can be to an empty string, in that case the element is just ignored. In the above example if `configurablePlaceholdersMap.put("type_of_cereal", "");` is defined `foo://cereal.com/nutritional_info` would map to calling the `intentForNutritionalDeepLinkMethod` method. An empty configurable path segment placeholder is not allowed as the last path element in an URL! ### Callbacks You can optionally register a `BroadcastReceiver` to be called on any incoming deep link into your app. DeepLinkDispatch will use `LocalBroadcastManager` to broadcast an `Intent` with any success or failure when deep linking. The intent will be populated with these extras: * `DeepLinkHandler.EXTRA_URI`: The URI of the deep link. * `DeepLinkHandler.EXTRA_SUCCESSFUL`: Whether the deep link was fired successfully. * `DeepLinkHandler.EXTRA_ERROR_MESSAGE`: If there was an error, the appropriate error message. You can register a receiver to receive this intent. An example of such a use is below: ```java public class DeepLinkReceiver extends BroadcastReceiver {  private static final String TAG = "DeepLinkReceiver"; @Override public void onReceive(Context context, Intent intent) { String deepLinkUri = intent.getStringExtra(DeepLinkHandler.EXTRA_URI); if (intent.getBooleanExtra(DeepLinkHandler.EXTRA_SUCCESSFUL, false)) { Log.i(TAG, "Success deep linking: " + deepLinkUri); } else { String errorMessage = intent.getStringExtra(DeepLinkHandler.EXTRA_ERROR_MESSAGE); Log.e(TAG, "Error deep linking: " + deepLinkUri + " with error message +" + errorMessage); } } } public class YourApplication extends Application { @Override public void onCreate() { super.onCreate(); IntentFilter intentFilter = new IntentFilter(DeepLinkHandler.ACTION); LocalBroadcastManager.getInstance(this).registerReceiver(new DeepLinkReceiver(), intentFilter); } } ``` ### Custom Annotations You can reduce the repetition in your deep links by creating custom annotations that provide common prefixes that are automatically applied to every class or method annotated with your custom annotation. A popular use case for this is with web versus app deep links: ```java // Prefix all app deep link URIs with "app://airbnb" @DeepLinkSpec(prefix = { "app://airbnb" }) @Retention(RetentionPolicy.CLASS) public @interface AppDeepLink { String[] value(); } ``` ```java // Prefix all web deep links with "http://airbnb.com" and "https://airbnb.com" @DeepLinkSpec(prefix = { "http://airbnb.com", "https://airbnb.com" }) @Retention(RetentionPolicy.CLASS) public @interface WebDeepLink { String[] value(); } ``` ```java // This activity is gonna handle the following deep links: // "app://airbnb/view_users" // "http://airbnb.com/users" // "http://airbnb.com/user/{id}" // "https://airbnb.com/users" // "https://airbnb.com/user/{id}" @AppDeepLink({ "/view_users" }) @WebDeepLink({ "/users", "/user/{id}" }) public class CustomPrefixesActivity extends AppCompatActivity { //... } ``` ## Usage Add to your project `build.gradle` file (Latest version is [![DeeplinkDispatch version](https://badge.fury.io/gh/airbnb%2FDeepLinkDispatch.svg)](https://badge.fury.io/gh/airbnb%2FDeepLinkDispatch) ): ```groovy dependencies { implementation 'com.airbnb:deeplinkdispatch:x.x.x' annotationProcessor 'com.airbnb:deeplinkdispatch-processor:x.x.x' } ``` _For **Kotlin** you should use_ `kapt` _instead of_ `annotationProcessor` Create your deep link module(s) (**new on DeepLinkDispatch v3**). For every class you annotate with `@DeepLinkModule`, DeepLinkDispatch will generate a "Registry" class, which contains a registry of all your `@DeepLink` annotations. ```java /** This will generate a AppDeepLinkModuleRegistry class */ @DeepLinkModule public class AppDeepLinkModule { } ``` **Optional**: If your Android application contains multiple modules (eg. separated Android library projects), you'll want to add one `@DeepLinkModule` class for every module in your application, so that DeepLinkDispatch can collect all your annotations in one "Registry" class per module: ```java /** This will generate a LibraryDeepLinkModuleRegistry class */ @DeepLinkModule public class LibraryDeepLinkModule { } ``` Create any `Activity` (eg. `DeepLinkActivity`) with the scheme you'd like to handle in your `AndroidManifest.xml` file (using `foo` as an example): ```xml ``` Annotate your `DeepLinkActivity` with `@DeepLinkHandler` and provide it a list of `@DeepLinkModule` annotated class(es): ```java @DeepLinkHandler({ AppDeepLinkModule.class, LibraryDeepLinkModule.class }) public class DeepLinkActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // DeepLinkDelegate, LibraryDeepLinkModuleRegistry and AppDeepLinkModuleRegistry // are generated at compile-time. DeepLinkDelegate deepLinkDelegate = new DeepLinkDelegate(new AppDeepLinkModuleRegistry(), new LibraryDeepLinkModuleRegistry()); // Delegate the deep link handling to DeepLinkDispatch. // It will start the correct Activity based on the incoming Intent URI deepLinkDelegate.dispatchFrom(this); // Finish this Activity since the correct one has been just started finish(); } } ``` of ```java @DeepLinkHandler({ AppDeepLinkModule.class, LibraryDeepLinkModule.class }) public class DeepLinkActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Configure a map for configurable placeholders if you are using any. If you do a mapping // has to be provided for that are used Map configurablePlaceholdersMap = new HashMap(); configurablePlaceholdersMap.put("your_values", "what should match"); // DeepLinkDelegate, LibraryDeepLinkModuleRegistry and AppDeepLinkModuleRegistry // are generated at compile-time. DeepLinkDelegate deepLinkDelegate = new DeepLinkDelegate(new AppDeepLinkModuleRegistry(), new LibraryDeepLinkModuleRegistry(), configurablePlaceholdersMap); // Delegate the deep link handling to DeepLinkDispatch. // It will start the correct Activity based on the incoming Intent URI deepLinkDelegate.dispatchFrom(this); // Finish this Activity since the correct one has been just started finish(); } } ``` if you use configurable path segments ### Upgrading When upgrading to 5.x+ you may experience some breaking API changes. Read about them [here](UPGRADING.md). ### Incremental annotation processing You must update your build.gradle to opt into incremental annotation processing. When enabled, all custom deep link annotations must be registered in the build.gradle (comma separated), otherwise they will be silently ignored. Examples of this configuration are as follows: **Standard** ```groovy javaCompileOptions { annotationProcessorOptions { arguments = [ 'deepLink.incremental': 'true', 'deepLink.customAnnotations': 'com.airbnb.AppDeepLink,com.airbnb.WebDeepLink' ] } } ``` **Kotlin kapt** ```groovy kapt { arguments { arg("deepLink.incremental", "true") arg("deepLink.customAnnotations", "com.airbnb.AppDeepLink,com.airbnb.WebDeepLink") } } ``` ### Performance Starting with v5 DeeplinkDispatch is designed to be very fast in resolving deep links even if there are a lot of them. To ensure we do not regress from this benchmark tests using [`androidx.benchmark`](https://developer.android.com/studio/profile/benchmark#top_of_page) were added. It is testing the `ScaleTestActivity` in the `sample-benchmarkable-library` which has 2000 deep links. For those on a Pixel 2 running Android 10 we expect the following results: ```text Started running tests Timed out waiting for process to appear on google-pixel_2-FA7AW1A04466. benchmark: 11,520 ns DeeplinkBenchmarks.match1 benchmark: 241,406 ns DeeplinkBenchmarks.match500 benchmark: 12,067,970 ns DeeplinkBenchmarks.newRegistry benchmark: 12,076 ns DeeplinkBenchmarks.match1000 benchmark: 140,000 ns DeeplinkBenchmarks.match1500 benchmark: 273,230 ns DeeplinkBenchmarks.match2000 benchmark: 148,750 ns DeeplinkBenchmarks.createResultDeeplink1 benchmark: 11,375 ns DeeplinkBenchmarks.parseDeeplinkUrl Tests ran to completion. ``` As you can see it taes us about 11ms to create the registry with 2000 entries. A lookup can be done in sub 1ms usually and `createResult`, which includes the lookup for the `match1` case plus actually calling the method that was annotated, can be done in 0.2ms. The performance tests can be run from Android Studio or via gradle by running `./gradlew sample-benchmark:connectedCheck` (with a device connected). The outoput can be found in `sample-benchmark/build/outputs/connected_android_test_additional_output/`. ### Notes * Starting with DeepLinkDispatch v3, you have to **always** provide your own `Activity` class and annotate it with `@DeepLinkHandler`. It's no longer automatically generated by the annotation processor. * Intent filters may only contain a single data element for a URI pattern. Create separate intent filters to capture additional URI patterns. * Please refer to the [sample app](https://github.com/airbnb/DeepLinkDispatch/blob/master/sample/src/main/java/com/airbnb/deeplinkdispatch/sample/DeepLinkActivity.java) for an example of how to use the library. Snapshots of the development version are available in [Sonatype's `snapshots` repository](https://oss.sonatype.org/content/repositories/snapshots/). ### Generated deep links Documentation You can tell DeepLinkDispatch to generate text a document with all your deep link annotations, which you can use for further processing and/or reference. In order to do that, add to your `build.gradle` file: ```groovy tasks.withType(JavaCompile) { options.compilerArgs << "-AdeepLinkDoc.output=${buildDir}/doc/deeplinks.txt" } ``` If you are using Kotlin this is how you can enable it for kapt ```groovy kapt { arguments { arg("deepLinkDoc.output", "${buildDir}/doc/deeplinks.txt") } } ``` The documentation will be generated in the following format: ``` * {DeepLink1}\n|#|\n[Description part of javadoc]\n|#|\n{ClassName}#[MethodName]\n|##|\n * {DeepLink2}\n|#|\n[Description part of javadoc]\n|#|\n{ClassName}#[MethodName]\n|##|\n ``` You can also generate the output in a much more readable Markdown format by naming the output file `*.md` (e.g. `deeplinks.md`). Make sure that your Markdown viewer understands tables. ### Matching and edge cases Deeplink Dispatchs matching algo is designed to match non ambiguous structured URI style data very fast but because of the supported featureset it comes with some edge cases. We organize the URI data (of all URIs that are in your app) in a tree structure that is created per module. The URI is dissolved into that tree structure and inserted into that graph at build time. We do not allow duplicates inside the tree at built time and having them will fail the build. However this is currently only guaranteed for each module not across modules.) ![Example of a DeeplinkDispagch match graph](images/dld_graph.png) At runtime we traverse the graph for each module to find the correct action to undertake. The algo just walks the input URI until the last element and *never* backtracks inside the graph. The children of each element are checked for matches in alphabetic order: `elements without any variable element -> elements containing placeholders -> elements that are a configurable path segment` #### Edge cases * Duplicates can exist between modules. Only the first one found will be reported as a match. Modules are processed in the order the module registries are listed in your `DeepLinkDelegate` creation. * Placeholders can lead to duplications at runtime e.g. `dld://airbnb/dontdupeme` will match both `@Deeplink('dld://airbnb/{qualifier}dupeme')` and `@Deeplink('dld://airbnb/dontdupeme')`. They can both be defined in the same module as they are not identical. If they are defined in the same module the algo will match `@Deeplink('dld://airbnb/dontdupeme')` as it would check litereal matches before looking at elements containing placeholders. If they are not defined in the same module the one defined in the registry listed first in `DeeplinkDelegate` will be matched. * Configurable path segments can lead to duplicates. e.g. `dld://airbnb/obamaos/cereal` will match both `@Deeplink('dld://airbnb/obamaos/cereal/')` and `@Deeplink('dld://airbnb//cereal')` if `` is configured to be `obamaos`. The same match rules as mentioned before apply here. * Configurable path segments can have empty values e.g. `` can be set to `""` in the previous example. Which would then match `dld://airbnb/cereal`. If a deeplink like that is defined already somewhere else the same match rules as mentioned before apply to which match actually gets found. * Because of limitations of the algo the last path element (the item behind the last slash) cannot be a configurable path segment with it's value set to `""`. Currently the system will allow you to do this but will not correctly match in that case. ## Proguard Rules ``` -keep @interface com.airbnb.deeplinkdispatch.DeepLink -keepclasseswithmembers class * { @com.airbnb.deeplinkdispatch.DeepLink ; } ``` **Note:** remember to include Proguard rules to keep Custom annotations you have used, for example by package: ``` -keep @interface your.package.path.deeplink. -keepclasseswithmembers class * { @your.package.path.deeplink. ; } ``` ## Testing the sample app Use adb to launch deep links (in the terminal type: `adb shell`). This fires a standard deep link. Source annotation `@DeepLink("dld://example.com/deepLink")` `am start -W -a android.intent.action.VIEW -d "dld://example.com/deepLink" com.airbnb.deeplinkdispatch.sample` This fires a deep link associated with a method, and also passes along a path parameter. Source annotation `@DeepLink("dld://methodDeepLink/{param1}")` `am start -W -a android.intent.action.VIEW -d "dld://methodDeepLink/abc123" com.airbnb.deeplinkdispatch.sample` You can include multiple path parameters (also you don't have to include the sample app's package name). Source annotation `@DeepLink("http://example.com/deepLink/{id}/{name}")` `am start -W -a android.intent.action.VIEW -d "http://example.com/deepLink/123abc/myname"` Note it is possible to call directly `adb shell am ...` however this seems to miss the URI sometimes so better to call from within shell. ## License ``` Copyright 2015-2020 Airbnb, Inc. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ```