Android 两个应用能否共用同一域名但使用不同路径前缀?
结论
支持。只要 assetlinks.json 里为每个应用分别写了 target 声明,并且两个应用各自在 intent-filter 中用不同的 pathPrefix(或 pathPattern)限定可处理的 URL,Android App Links 就可以让两个不同应用关联同一个域名。域名验证按“应用包名 + 签名证书”逐个进行,同一个域名可以同时关联多个应用。设置里的“支持的网址”只列出已通过验证且满足 App Links 条件的域名;如果那里只看到一个应用,通常说明另一个应用没有验证成功,或者它的 intent-filter 不满足条件。
配置方式
两个应用共用同一个 https://example.com/.well-known/assetlinks.json,但文件里要同时包含两个应用的包名和各自签名证书的 SHA-256 指纹。例如:
[
{
"relation": ["delegate_permission/common.handle_all_urls"],
"target": {
"namespace": "android_app",
"package_name": "com.example.app1",
"sha256_cert_fingerprints": [
"AA:BB:CC:...:APP1"
]
}
},
{
"relation": ["delegate_permission/common.handle_all_urls"],
"target": {
"namespace": "android_app",
"package_name": "com.example.app2",
"sha256_cert_fingerprints": [
"DD:EE:FF:...:APP2"
]
}
}
]
两个应用的 intent-filter 分别限定自己的路径范围,例如:
<!-- App 1 -->
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:scheme="https"
android:host="example.com"
android:pathPrefix="/app1" />
</intent-filter>
<!-- App 2 -->
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:scheme="https"
android:host="example.com"
android:pathPrefix="/app2" />
</intent-filter>
pathPrefix 按前缀匹配,/app1 也会命中 /app1abc。如果只想匹配路径段,可以用 /app1/ 或 pathPattern,比如 android:pathPattern="/app1/.*"。两个应用的路径范围尽量不要重叠。
验证和“默认打开”行为
每个声明了 android:autoVerify="true" 的应用都会单独进行域名验证。系统下载 assetlinks.json,检查其中有没有与当前应用包名和签名证书匹配的 target。所以同一个域名可以同时被多个应用验证通过。具体到一个 URL 时,系统会在所有能匹配它的 intent-filter 里选择已验证的应用;只要路径前缀互不重叠,/app1/... 就只会进入 App 1,/app2/... 只会进入 App 2。
“设置 → 应用 → 默认打开 → 支持的网址”是每个应用各自的页面。两个应用都验证成功后,需要分别到 App 1 和 App 2 的页面里查看,不要期待这里出现一个合并列表。如果同一个域名只出现在一个应用下面,常见原因有:
- 另一个应用的包名或 SHA-256 指纹没有正确写入
assetlinks.json。 - 使用了调试签名,或 Play App Signing 证书不一致。
intent-filter缺少VIEW、DEFAULT、BROWSABLE、autoVerify,或scheme/host写错。assetlinks.json无法通过 HTTPS 直接访问,或返回了重定向、错误 Content-Type。- 两个应用的路径过滤器重叠,系统行为或设置展示可能不符合预期。
可以用下面的命令辅助检查(不同 Android 版本对命令的支持可能不同):
adb shell pm get-app-links com.example.app1
adb shell pm get-app-links com.example.app2
也可以直接测试跳转:
adb shell am start -a android.intent.action.VIEW -d "https://example.com/app1/test"
adb shell am start -a android.intent.action.VIEW -d "https://example.com/app2/test"
注意事项
assetlinks.json必须能通过公开的 HTTPS 地址访问,通常是https://example.com/.well-known/assetlinks.json。- 每个应用的
target要填实际安装包签名证书的 SHA-256 指纹;如果应用通过 Google Play 安装,注意使用 Play App Signing 的证书指纹。 - 两个应用的路径范围不要完全一样。路径一旦重叠,用户可能得在两个应用之间选择,或者由默认设置决定。
- 验证成功后,有些系统或 OEM 的设置界面只显示域名、不显示具体路径,这是正常现象。判断时以实际跳转和
pm get-app-links的结果为准。
备注:内容仅供参考。
