Android Deep Link配置是否必须为所有URL指定android:host属性
问题结论
Android 没有强制要求所有 Deep Link 必须指定 android:host 属性,仅指定 android:scheme 即可匹配无 host 的 schema 链接(比如你用到的 native://)。你当前配置不生效的核心原因是 Intent Filter 中 标签的组合匹配规则问题。
不生效原因
当同一个 Intent Filter 下同时存在仅配置 scheme 的 和同时配置 scheme + host 的 时,系统会默认将属性做交叉组合,相当于要求 scheme 为 native 的链接也必须匹配 host 规则,导致 native:// 无法命中。
正确配置方案
有两种可行的配置方式:
方式1:拆分不同规则到独立的 Intent Filter 节点
把两个不同的匹配规则分别放在两个独立的 <intent-filter> 标签中,避免属性交叉合并,是最稳妥的实现方案:
<!-- 匹配 appdemo://deeplink --> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:scheme="appdemo" android:host="deeplink" /> </intent-filter> <!-- 匹配 native:// 所有该scheme下的链接 --> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:scheme="native" /> </intent-filter>
方式2:同个 Intent Filter 下显式声明空 host 规则
如果要放在同一个 Intent Filter 下维护,给 native 对应的 显式配置 android:host="" 声明匹配空 host 即可:
<intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:scheme="appdemo" android:host="deeplink" /> <data android:scheme="native" android:host="" /> </intent-filter>
验证方法
配置完成后可以通过 adb 命令直接测试跳转是否生效,替换命令中的你的应用包名为实际应用包名即可:
- 测试
appdemo://deeplink匹配:adb shell am start -W -a android.intent.action.VIEW -d "appdemo://deeplink" 你的应用包名 - 测试
native://匹配:adb shell am start -W -a android.intent.action.VIEW -d "native://" 你的应用包名
内容的提问来源于stack exchange,提问作者jerald jacob
相关产品推荐
相关产品推荐

