How can you create a Deeplink using placeholders and nav component at build time
Asked Answered
F

2

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.

Forego answered 14/8, 2019 at 14:4 Comment(5)
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.Ormuz
Did you resolve this problem?? I have the same issueBritton
@Forego were you able to fix this issue?Steamer
@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...Perihelion
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.Forego
O
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.

Onaonager answered 27/8, 2019 at 6:3 Comment(0)
M
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.

Markham answered 27/8, 2019 at 14:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.