使用Navigation组件时如何阻止manifest merger给intent-filter添加path数据
解决方案
方案1:拆分声明deeplink属性(最优)
不要用app:uri整串声明deeplink,分开指定scheme和host属性,导航组件就不会自动生成多余的android:path="/"规则:
<deepLink app:scheme="example" app:host="foo" />
生成的intent-filter只会包含scheme和host配置,完全符合intent-filter匹配规则,example://foo、example://foo/、example://foo/任意路径都能正常匹配。
方案2:通过manifest合并规则移除多余path
如果你需要保留app:uri的写法,可以在AndroidManifest.xml中给承载导航图的Activity添加合并规则,移除自动生成的path属性:
- 首先确保Manifest根节点已声明tools命名空间:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" ...> - 在对应Activity节点下添加如下配置:
<activity android:name=".你的NavHostActivity类名" ...> <intent-filter tools:node="merge"> <data android:scheme="example" android:host="foo" tools:remove="android:path" /> </intent-filter> </activity>
方案3:添加通配符兼容多种格式
如果需要同时兼容无斜杠、带斜杠、带后续路径的所有场景,也可以直接在uri末尾加通配符:
<deepLink app:uri="example://foo.*" />
内容的提问来源于stack exchange,提问作者osrl
相关产品推荐
相关产品推荐

