多Host配置后Android DeepLink部分命令失效原因咨询
为何添加多Host的DeepLink配置后,无斜杠的链接会失效?
当在AndroidManifest.xml中定义单个DeepLink的intent-filter时:
<intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <data android:host="example-host" android:scheme="my-app" /> </intent-filter>
使用命令 adb shell am start -d my-app://example-host 可以正常触发DeepLink。
但添加第二个带有pathPattern的<data>节点后:
<intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <data android:host="example-host" android:scheme="my-app" /> <data android:host="example-host-2" android:pathPattern="/" android:scheme="my-app" /> </intent-filter>
出现以下现象:
adb shell am start -d my-app://example-host命令失效adb shell am start -d my-app://example-host-2/命令正常生效adb shell am start -d my-app://example-host/命令正常生效
这不是Android的Bug,而是intent-filter匹配逻辑的特性:
当同一个intent-filter中存在多个<data>节点时,只要其中任何一个<data>指定了path相关属性(path/pathPattern/pathPrefix),整个intent-filter的匹配规则会隐含要求URI必须包含非空的path部分。
对于my-app://example-host这个URI,它的path为空,不符合隐含的path非空要求,因此无法匹配该intent-filter;而my-app://example-host/的path为/,满足要求,所以可以正常匹配。
解决方法
要解决这个问题,需要将两个<data>规则拆分为独立的intent-filter,让每个规则的匹配逻辑互不干扰:
<!-- 匹配example-host的所有URI(含空path) --> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <data android:host="example-host" android:scheme="my-app" /> </intent-filter> <!-- 匹配example-host-2且path为/的URI --> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <data android:host="example-host-2" android:pathPattern="/" android:scheme="my-app" /> </intent-filter>
拆分后,my-app://example-host、my-app://example-host/、my-app://example-host-2/三个链接都能正常触发对应的DeepLink。
内容的提问来源于stack exchange,提问作者Elye
相关产品推荐
相关产品推荐

