12

We have an app that utilises Deeplinks. We also use the Android Navigation component.

Currently we configure our Deeplinks in out navigation.xml file and this works fine but we now have the requirement to be able to add another Deeplink at build time based on a set Environment Variable.

  • I have attempted setting String resources in the build.gradle and referenceing these in the navigation.xml.

  • I have also attempted setting a placeholder in the navigation.xml but cannot replace it as it has already been parsed as a URI.

  • I have also attempted setting direct intent filters in the Manifest with placeholders, this will work but we lose the nice routing from the navigation component.

Currently we configure our Deeplinks in out navigation.xml file in the following form:

 <deepLink
            android:autoVerify="true"
            app:uri="foo.bar.baz/pull/{quxArg}/{quuxArg}" />

We now have the requirement to be able to create an additional Deeplink at build time based on a set Envar.

Example:

DEEPLINK_ENVAR = "replacement.com"

Build.gradle:

manifestPlaceholders = [deeplink:DEEPLINK_ENVAR]

navigation.xml:

<deepLink
            android:autoVerify="true"
            app:uri="${deeplink}/pull/{quxArg}/{quuxArg}" />

Please note the above does not work.

If this was just an intent-filter in the Manifest we could use Manifest placeholders to achieve this task and set them in the app.gradle. However Deeplinks set in navigation.xml are parsed as URIs and destroy any placeholders before they can be replaced.

Has anyone attempted anything similar? I am trying to avoid having to run a pre-build script to template the navigation file directly.

Desired outcome:

I am looking to be able to add an additional deeplink (4 actually to different destinations) at build time whilst making use of Android Navigation component.

Flint
  • 249
  • 2
  • 9
  • 3
    were you able to resolve this issue? I am stuck in the same situation. I want to have dynamic scheme for deeplinks which I can add programmatically. – Wahib Ul Haq Aug 30 '19 at 13:29
  • Did you resolve this problem?? I have the same issue – José Carlos Apr 15 '20 at 14:02
  • @Flint were you able to fix this issue? – SoH Aug 26 '20 at 13:58
  • @Flint, any update here? My "backup" solution would be to duplicate the entire graph into different product flavor / build type folders, but that would be terrible to maintain... – deRonbrown Nov 10 '20 at 13:37
  • Sorry for the horribly slow reply. We ended up creating a template navigation file and a gradle task to interpolate the deep links based on Environment Variables. Unfortunately I am no longer at the same company so do not have access to the code. – Flint Aug 10 '22 at 13:40

2 Answers2

0

Not sure if I completely understand but... You should be able to add several deepLinks to a single action. If you require it to redirect to a different fragment, you could try have a "deepLinkTokenCheckFragment" or something, which receives the deepLink, then extracts the information from it, and can redirect the user to the page that you want them to go to.

I have an application that does something like this

    private fun extractAction() {
        if (ACTION_VIEW == parent.intent.action) {
            // Collect information to know where to redirect here.....
            val actionType = parent.intent.data
                ?.toString()
                ?.substringBefore('?')
                ?.substringAfterLast('/')
            action = get information or token from the url here //?.substringBefore('?') ?.substringAfterLast('/')
            when (action) {
                "change_password" -> go to change password screen
                "change email" -> go to change email screen
                "go to other" -> go to other screen
            }
        }
    }

This is just an idea of how I did it.

In the same way, instead of checking some token, you could check the build or whatever you need to compare it to.

Dylan Ang
  • 175
  • 7
0

NavDestination:

public final void addDeepLink (String uriPattern)

Add a deep link to this destination. Matching Uris sent to NavController.handleDeepLink(Intent) or NavController.navigate(Uri) will trigger navigating to this destination.

https://developer.android.com/reference/androidx/navigation/NavDestination.html#addDeepLink(java.lang.String)

This sounds like it could help you. I have not tested it myself.