简体   繁体   English

如何在构建时使用占位符和导航组件创建深度链接

[英]How can you create a Deeplink using placeholders and nav component at build time

We have an app that utilises Deeplinks.我们有一个使用 Deeplinks 的应用程序。 We also use the Android Navigation component.我们还使用了 Android 导航组件。

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.目前我们在navigation.xml 文件中配置我们的Deeplinks,这工作正常,但我们现在需要能够在构建时根据设置的环境变量添加另一个Deeplink。

  • I have attempted setting String resources in the build.gradle and referenceing these in the navigation.xml.我尝试在 build.gradle 中设置 String 资源并在 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.我还尝试在 navigation.xml 中设置占位符,但无法替换它,因为它已被解析为 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.我还尝试在 Manifest 中使用占位符设置直接意图过滤器,这会起作用,但我们失去了来自导航组件的良好路由。

Currently we configure our Deeplinks in out navigation.xml file in the following form:目前,我们在 out navigation.xml 文件中以以下形式配置我们的 Deeplinks:

 <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.我们现在需要能够在构建时基于一组 Envar 创建额外的 Deeplink。

Example:例子:

DEEPLINK_ENVAR = "replacement.com"

Build.gradle:构建.gradle:

manifestPlaceholders = [deeplink:DEEPLINK_ENVAR]

navigation.xml:导航.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.如果这只是 Manifest 中的一个意图过滤器,我们可以使用 Manifest 占位符来完成此任务并在 app.gradle 中设置它们。 However Deeplinks set in navigation.xml are parsed as URIs and destroy any placeholders before they can be replaced.然而,navigation.xml 中设置的深层链接被解析为 URI,并在替换之前销毁所有占位符。

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.我希望能够在构建时添加额外的深层链接(实际上 4 个到不同的目的地),同时使用 Android 导航组件。

Not sure if I completely understand but... You should be able to add several deepLinks to a single action.不确定我是否完全理解,但是...您应该能够将多个 deepLink 添加到单个操作中。 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.如果您需要它重定向到不同的片段,您可以尝试使用“deepLinkTokenCheckFragment”或其他东西,它接收 deepLink,然后从中提取信息,并可以将用户重定向到您希望他们去的页面。

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.以同样的方式,您可以检查构建或任何需要与之进行比较的内容,而不是检查某些标记。

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.匹配发送到NavController.handleDeepLink(Intent)NavController.navigate(Uri) Uris 将触发导航到此目的地。

https://developer.android.com/reference/androidx/navigation/NavDestination.html#addDeepLink(java.lang.String) 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.我自己没有测试过。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 导航组件Deeplink不执行条件导航 - Nav component deeplink do not do conditional navigation 导航组件隐式深层链接不适用于 res 字符串 - Nav Component implicit deeplink not working with res string 如何使用 Android 导航组件和深度链接打开活动和特定目的地 - How to open an activity and a particular destination using Android navigation component and deeplink 如何使用Deeplink为应用程序创建两个入口点? - How to create two entry points to application using deeplink? 有没有办法使用导航组件在Android多模块项目中创建隐式深层链接 - Is there a way to create implicit deeplink in Android Multi-module project using Navigation Component 如何在 android 中使用深层链接启动应用程序 - How to launch an app using a deeplink in android deeplink 在使用导航组件时发送不完整的序列化参数 - deeplink sends incomplete serialized argument while using navigation component 我们如何使用深度链接信息通过Google API AppReferral收集发件人数据(例如邮件ID) - How can we use the deeplink information to gather sender data like mail id using Google API AppReferral 如何同时处理Firebase DynamicLink和普通DeepLink? - How to handle both Firebase DynamicLink and plain DeepLink at the same time? 如何使用 PathPattern 来创建 DeepLink Apps Android? - How to use PathPattern in order to create DeepLink Apps Android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM